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.
Circuit
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.
Sketch
Use buttons to control the relays and motors.
Sketch_17.1_Control_Motor_by_Relay
Download the code to ESP32-WROVER. 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:
1/**********************************************************************
2 Filename : Control Motor by Relay
3 Description : Use relay to control motor.
4 Auther : www.freenove.com
5 Modification: 2024/06/19
6**********************************************************************/
7int relayPin = 13; // the number of the relay pin
8int buttonPin = 15; // the number of the push button pin
9
10int buttonState = HIGH; // Record button state, and initial the state to high level
11int relayState = LOW; // Record relay state, and initial the state to low level
12int lastButtonState = HIGH; // Record the button state of last detection
13long lastChangeTime = 0; // Record the time point for button state change
14
15void setup() {
16 pinMode(buttonPin, INPUT_PULLUP); // Set push button pin into input mode
17 pinMode(relayPin, OUTPUT); // Set relay pin into output mode
18 digitalWrite(relayPin, relayState); // Set the initial state of relay into "off"
19}
20void loop() {
21 int nowButtonState = digitalRead(buttonPin);// Read current state of button pin
22 // If button pin state has changed, record the time point
23 if (nowButtonState != lastButtonState) {
24 lastChangeTime = millis();
25 }
26 // If button state changes, and stays stable for a while, then it should have skipped the bounce area
27 if (millis() - lastChangeTime > 10) {
28 if (buttonState != nowButtonState) { // Confirm button state has changed
29 buttonState = nowButtonState;
30 if (buttonState == LOW) { // Low level indicates the button is pressed
31 relayState = !relayState; // Reverse relay state
32 digitalWrite(relayPin, relayState); // Update relay state
33 }
34 }
35 }
36 lastButtonState = nowButtonState; // Save the state of last button
37}
In Chapter 2, the pressing and releasing of the button will result in mechanical vibrating. If we don’t solve this problem, some unexpected consequences may happen to the procedure. Click here to return to Chapter 2 Button & LED
To eliminate the vibrating, we record the electrical level of the button with nowButtonState, and the time point for the last change of pin level with lastChangeTime If the state of the button changes, it will record the time point of the change.
1int nowButtonState = digitalRead(buttonPin);// Read current state of button pin
2// If button pin state has changed, record the time point
3if (nowButtonState != lastButtonState) {
4 lastChangeTime = millis();
5}
If the state of the pin changes and keeps stable for a period of time, it can be considered as a valid key state change, update the key state variable buttonState, and determine whether the key is pressed or released according to the current state.
1// If button state changes, and stays stable for a while, then it should have skipped the bounce area
2if (millis() - lastChangeTime > 10) {
3 if (buttonState != nowButtonState) { // Confirm button state has changed
4 buttonState = nowButtonState;
5 if (buttonState == LOW) { // Low level indicates the button is pressed
6 relayState = !relayState; // Reverse relay state
7 digitalWrite(relayPin, relayState); // Update relay state
8 }
9 }
10}
11lastButtonState = nowButtonState; // Save the state of last button


















