Chapter 17 Relay & Motor
In this chapter, we will learn a kind of special switch module, relay module.
Project 17.1 Relay & Motor
In this project, we will use a push button switch indirectly to control the motor via a relay.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
Resistor 10kΩ x2
|
Resistor 220Ω x1
|
NPN transistor x1
|
Resistor 1kΩ x1
|
Relay x1
|
Diode x1
|
Push button x1
|
LED x1
|
Motor x1
|
Jumper M/M
|
||
9V battery (prepared by yourself) & battery line
|
|||
Component knowledge
Relay
A relay is a safe switch which can use low power circuit to control high power circuit. It consists of electromagnet and contacts. The electromagnet is controlled by low power circuit and contacts are used in high power circuit. When the electromagnet is energized, it will attract contacts.
The following is a schematic diagram of a common relay and the feature and circuit symbol of a 5V relay used in this project:
Diagram |
Feature |
Symbol |
|
|
|
Pin 5 and pin 6 are connected to each other inside. When the coil pins 3 and 4 get connected to 5V power supply, pin 1 will be disconnected to pin 5&6 and pin 2 will be connected to pin 5&6. So pin 1 is called close end, pin 2 is called open end.
Inductor
The symbol of Inductance is “L” and the unit of inductance is the “Henry” (H). Here is an example of how this can be encountered: 1H=1000mH, 1mH=1000μH.
An inductor is an energy storage device that converts electrical energy into magnetic energy. Generally, it consists of winding coil, with a certain amount of inductance. Inductors hinder the change of current passing through it. When the current passing through it increases, it will attempt to hinder the increasing trend of current; and when the current passing through it decreases, it will attempt to hinder the decreasing trend of current. So the current passing through inductor is not transient.
The reference circuit for relay is as follows. The coil of relays can be equivalent to that of inductors, when the transistor disconnects power supply of the relay, the current in the coil of the relay can’t stop immediately, causing an impact on power supply. So a parallel diode will get connected to both ends of relay coil pin in reversing direction, then the current will pass through diode, avoiding the impact on power supply.
Motor
A motor is a device that converts electrical energy into mechanical energy. Motor consists of two parts: stator and rotor. When motor works, the stationary part is stator, and the rotating part is rotor. Stator is usually the outer case of motor, and it has terminals to connect to the power. Rotor is usually the shaft of motor, and can drive other mechanical devices to run. Diagram below is a small DC motor with two pins.
When a motor gets connected to the power supply, it will rotate in one direction. Reverse the polarity of power supply, then the motor rotates in opposite direction.
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Note
The motor circuit uses A large current, about 0.2-0.3A without load.We recommend that you use a 9V battery to power the extension board.
Code
Use buttons to control the relays and motors.
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “17.1_Relay_And_Motor” and double click “Relay_And_Motor.py”.
17.1_Relay_And_Motor
Click “Run current script”. When the DC Motor is connected to a power supply, it will rotate in one direction. If you reverse the polarity of the power supply, the DC Motor will rotate in opposite direction.
The following is the program code:
1import time
2from machine import Pin
3
4relay = Pin(13, Pin.OUT)
5button = Pin(15, Pin.IN,Pin.PULL_UP)
6
7def reverseRelay():
8 if relay.value():
9 relay.value(0)
10 else:
11 relay.value(1)
12
13while True:
14 if not button.value():
15 time.sleep_ms(20)
16 if not button.value():
17 reverseRelay()
18 while not button.value():
19 time.sleep_ms(20)
This section of code is basically the same as that of project Tablelamp. If you don’t understand the program, you can click here to go back to the Tablelamp and study again.
Project 17.2 Control Motor with Potentiometer
Control the direction and speed of the motor with a potentiometer.
ESP32-WROVER x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
Motor x1
|
9V battery (prepared by yourself) & battery line
|
||
Jumper M/M
|
L293D
|
Rotary potentiometer x1 |
|
Component knowledge
L293D
L293D is an IC chip (Integrated Circuit Chip) with a 4-channel motor drive. You can drive a unidirectional DC motor with 4 ports or a bi-directional DC motor with 2 ports or a stepper motor (stepper motors are covered later in this Tutorial).
Port description of L293D module is as follows:
Pin name |
Pin number |
Description |
|---|---|---|
In x |
2, 7, 10, 15 |
Channel x digital signal input pin |
Out x |
3, 6, 11, 14 |
Channel x output pin, input high or low level according to In x pin, get connected to +Vmotor or 0V |
Enable1 |
1 |
Channel 1 and channel 2 enable pin, high level enable |
Enable2 |
9 |
Channel 3 and channel 4 enable pin, high level enable |
0V |
4, 5, 12, 13 |
Power cathode (GND) |
+V |
16 |
Positive electrode (VCC) of power supply, supply voltage 3.0~36V |
+Vmotor |
8 |
Positive electrode of load power supply, provide power supply for the Out pin x, the supply voltage is +V~36V |
For more detail, please refer to the datasheet for this IC Chip.
When using L293D to drive DC motor, there are usually two connection options.
The following connection option uses one channel of the L239D, which can control motor speed through the PWM, However the motor then can only rotate in one direction.
The following connection uses two channels of the L239D: one channel outputs the PWM wave, and the other channel connects to GND, therefore you can control the speed of the motor. When these two channel signals are exchanged, not only controls the speed of motor, but also can control the steering of the motor.
In practical use the motor is usually connected to channel 1 and 2 by outputting different levels to in1 and in2 to control the rotational direction of the motor, and output to the PWM wave to Enable1 port to control the motor’s rotational speed. If the motor is connected to channel 3 and 4 by outputting different levels to in3 and in4 to control the motor’s rotation direction, and output to the PWM wave to Enable2 pin to control the motor’s rotational speed.
Circuit
Use caution when connecting this circuit, because the DC motor is a high-power component, do not use the power provided by the ESP32 to power the motor directly, which may cause permanent damage to your ESP32! The logic circuit can be powered by the ESP32 power or an external power supply, which should share a common ground with ESP32.
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Note
The motor circuit uses A large current, about 0.2-0.3A without load.We recommend that you use a 9V battery to power the extension board.
Code
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “17.2_Motor_And_Driver” and double click “Motor_And_Driver.py”.
17.2_Motor_And_Driver
Click “Run current script”, rotate the potentiometer in one direction and the motor speeds up slowly in one direction. Rotate the potentiometer in the other direction and the motor will slow down to stop. And then rotate it in the original direction to accelerate the motor.
The following is the Code:
1from machine import ADC,Pin,PWM
2import time
3import math
4
5in1Pin=Pin(12, Pin.OUT)
6in2Pin=Pin(14, Pin.OUT)
7
8enablePin=Pin(13, Pin.OUT)
9pwm=PWM(enablePin,10000)
10
11adc=ADC(Pin(36))
12adc.atten(ADC.ATTN_11DB)
13adc.width(ADC.WIDTH_12BIT)
14
15def driveMotor(dir,spd):
16 if dir:
17 in1Pin.value(1)
18 in2Pin.value(0)
19 else :
20 in1Pin.value(0)
21 in2Pin.value(1)
22 pwm.duty(spd)
23
24try:
25 while True:
26 potenVal = adc.read()
27 rotationSpeed = potenVal - 2048
28 if (potenVal > 2048):
29 rotationDir = 1;
30 else:
31 rotationDir = 0;
32 rotationSpeed=int(math.fabs((potenVal-2047)//2)-1)
33 driveMotor(rotationDir,rotationSpeed)
34 time.sleep_ms(10)
35except:
36 adc.deinit()
37 pwm.deinit()
The ADC of ESP32 has a 12-bit accuracy, corresponding to a range from 0 to 4095. In this program, set the number 2048 as the midpoint. If the value of ADC is less than 2048, make the motor rotate in one direction. If the value of ADC is greater than 2048, make the motor rotate in the other direction. Subtract 2048 from the ADC value and take the absolute value, and then divide this result by 2 to be the speed of the motor.
1potenVal = adc.read()
2rotationSpeed = potenVal - 2048
3if (potenVal > 2048):
4 rotationDir = 1;
5else:
6 rotationDir = 0;
7rotationSpeed=int(math.fabs((potenVal-2047)//2)-1)
8driveMotor(rotationDir,rotationSpeed)
9time.sleep_ms(10)
Initialize pins of L293D chip.
1in1Pin=Pin(12, Pin.OUT)
2in2Pin=Pin(14, Pin.OUT)
3
4enablePin=Pin(13, Pin.OUT)
5pwm=PWM(enablePin,10000)
Initialize ADC pins, set the range of voltage to 0-3.3V and the acquisition width of data to 0-4095.
1adc=ADC(Pin(36))
2adc.atten(ADC.ATTN_11DB)
3adc.width(ADC.WIDTH_12BIT)
Function driveMotor is used to control the rotation direction and speed of the motor. The dir represents direction while spd refers to speed.
1def driveMotor(dir,spd):
2 if dir:
3 in1Pin.value(1)
4 in2Pin.value(0)
5 else :
6 in1Pin.value(0)
7 in2Pin.value(1)
8 pwm.duty(spd)






















