DC motror (H-Bridge) + RGB LED
We are going to learn :
-
How DC motor works
-
How to control the speed and direction of DC motor.
-
How to control a DC motor using L298N driver.
Video Tutorial
you can watch this video tutorial
Hardware Required
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
1 | × | L298N Motor Driver Module | |
1 | × | DC Motor | |
1 | × | 12V Power Adapter | |
1 | × | RGB LED | |
n | × | Jumper Wires |
Introduction to DC Motor
DC Motor has two wires:
-
Positive wire: usually red
-
Negative wire: usually black
How DC Motor Works
When you buy a DC motor, you need to know what voltage DC motor work. Let's take a 12V DC motor as an example.
When you power the 12V DC motor by a 12V power source:
-
12V and GND to the positive wire and negative wire, respectively: the DC motor rotates at maximum speed in the clockwise direction
-
12V and GND to the negative wire and positive wire, respectively: the DC motor rotates at maximum speed in the anti-clockwise direction
As described above, when the power pole is swapped between two wires of the DC motor, the rotating direction is reversed. This method is used to control the direction of the DC motor. Of course, not by changing manually but by programming.
If we provide power to DC motors below 12V, the motor still rotates but not at maximum speed. It means if we change the voltage of the power supply, we can change the speed of the DC motor. However, this method is not used in practice because of the difficulty in controlling the voltage of the power source. Instead, we fix the voltage of the power source and control the speed of the DC motor via a PWM signal. The more duty cycle the PWM is, the higher speed the DC motor rotates.
How to control DC motor using ESP32
Controlling DC motor includes two factors: speed and direction. ESP32 can generate the PWM signal. However, this PWM signal has low voltage and current, We cannot use it to control the DC motor. We need to use a hardware driver in between ESP32 and DC motor. The driver does two works:
-
Amplify the PWM signal from ESP32 (current and voltage) → for speed control
-
Receive the control signal from ESP32 to swap pole of power supply → for direction control.
※ NOTE THAT:
-
This tutorial can be applied to all DC motors. 12V DC motor is just an example.
-
When you controls 5V DC motor, although ESP32 pin outputs 5V (the same as DC motor voltage), you still needs a driver in between ESP32 and DC motor because the ESP32 pin does not provide enough the current for DC motor.
There are many kinds of the chip, modules (e.g. L293D, L298N) can be used as DC motor drivers. In this tutorial, we will use the L298N driver.
Introduction to L298N Driver
L298N Driver can be used to control DC motor and stepper motor. In this tutorial, we learn how to use it to control the DC motor.
L298N Driver Pinout
L298N Driver can control two DC motor independently at the same time, called motor A and motor B. L298N Driver has 13 pins:
The common pins for both motors:
-
VCC pin: supplies power for the motor. It can be anywhere between 5 to 35V.
-
GND pin: is a common ground pin, needs to be connected to GND (0V).
-
5V pin: supplies power for L298N module. It can be supplied by 5V from ESP32.
Motor A pins (Channel A):
-
ENA pins: are used to control the speed of Motor A. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor A.
-
IN1 & IN2 pins: are used to control the spinning direction of Motor A. When one of them is HIGH and the other is LOW, the Motor A will spin. If both the inputs are either HIGH or LOW the Motor A will stop.
-
OUT1 & OUT2 pins: are connected to Motor A.
Motor B pins (Channel B):
-
ENB pins: are used to control the speed of Motor B. Removing the jumper and connecting this pin to PWM input will let us control the speed of Motor B.
-
IN3 & IN4 pins: are used to control the spinning direction of Motor B. When one of them is HIGH and the other is LOW, Motor B will spin. If both the inputs are either HIGH or LOW the Motor B will stop.
-
OUT3 & OUT4 pins: are connected to Motor B.
As described above, the L298N driver has two input powers:
-
One for DC motor (VCC and GND pins): from 5 to 35V.
-
One for the L298N module's internal operation (5V and GND pins): from 5 to 7V.
The L298N driver also has three jumpers for advanced uses or other purposes. For the sake of simplicity, please remove all jumpers from the L298N driver.
We can control two DC motors independently at the same time by using an ESP32 and an L298N Driver. To control each motor, we need only three pins from ESP32.
How To Control the Speed of DC Motor via L298N Driver
This a list of the most useful functions to use ESP-Now if we want to exchange data between ESP devices:
-
Connect an ESP32 pin to ENA of L298N
-
Generate PWM signal to ENA pin by using ledcWrite() function. L298N Driver amplify PWM signal to DC motor
The speed is a value between 0 and 255. If the speed is 0, the motor stops. If the speed is 255, the motor spins at maximum speed.
How To Control the Direction of DC Motor via L298N Driver
The spinning direction of a motor can be controlled by applying a logic HIGH/LOW to IN1 and IN2 pins. The below table illustrates how to control the direction in both channels.
IN1 pin | IN2 pin | Direction |
---|---|---|
LOW | LOW | Motor A stops |
HIGH | HIGH | Motor A stops |
HIGH | LOW | Motor A spins Clockwise |
LOW | HIGH | Motor A spins Anti-Clockwise |
-
Control motor A spins clockwise
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW);
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH);
※ NOTE THAT:
The spinning direction is inverted if the OUT1 & OUT2 pin connects to two pins of the DC motor in an inverse way. If so, it just needs to swap between OUT1 & OUT2 pin or change the control signal on IN1 and IN2 pin in the code.
How To Stop DC Motor Spinning
There are two ways to stop DC motor
-
Controls the speed to 0
analogWrite(ENA_PIN, 0);
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
or
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH);
ESP32 Code
The below code does:
-
spin DC motor clockwise while the led is red
-
spin DC motor anti-clockwise while the led is bleu
-
stop DC motor while the led is green
// Motor A int motor1Pin1 = 27; int motor1Pin2 = 26; int enable1Pin = 14; // Setting PWM properties const int m_freq = 30000; const int m_pwmChannel = 3; const int m_resolution = 8; int m_dutyCycle = 200; // Red, green, and blue pins for PWM control const int redPin = 13; // 13 corresponds to GPIO13 const int greenPin = 12; // 12 corresponds to GPIO12 const int bluePin = 14; // 14 corresponds to GPIO14 // Setting PWM frequency, channels and bit resolution const int freq = 5000; const int redChannel = 0; const int greenChannel = 1; const int blueChannel = 2; // Bit resolution 2^8 = 256 const int resolution = 8; void setup() { // sets the pins as outputs: pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(enable1Pin, OUTPUT); // configure LED PWM functionalitites ledcSetup(m_pwmChannel, m_freq, m_resolution); // attach the channel to the GPIO to be controlled ledcAttachPin(enable1Pin, m_pwmChannel); // configure LED PWM functionalitites ledcSetup(redChannel, freq, resolution); ledcSetup(greenChannel, freq, resolution); ledcSetup(blueChannel, freq, resolution); // attach the channel to the GPIO to be controlled ledcAttachPin(redPin, redChannel); ledcAttachPin(greenPin, greenChannel); ledcAttachPin(bluePin, blueChannel); } void loop() { // Move the DC motor forward at maximum speed Serial.println("Moving Forward"); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); ledcWrite(redChannel, 0); ledcWrite(greenChannel, 0); ledcWrite(blueChannel, 250); delay(2000); // Stop the DC motor Serial.println("Motor stopped"); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); ledcWrite(redChannel, 0); ledcWrite(greenChannel, 250); ledcWrite(blueChannel, 0); delay(1000); // Move DC motor backwards at maximum speed Serial.println("Moving Backwards"); digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); ledcWrite(redChannel, 250); ledcWrite(greenChannel, 0); ledcWrite(blueChannel, 0); delay(2000); // Stop the DC motor Serial.println("Motor stopped"); digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); ledcWrite(redChannel, 0); ledcWrite(greenChannel, 250); ledcWrite(blueChannel, 0); delay(1000); }
Quick Instructions
-
Remove all three jumpers on the L298N module.
-
Copy the above code and paste it to Arduino IDE.
-
Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
-
You will see:
-
DC motor rotates at the maximum speed 2 second and the led is red
-
DC motor is stoped and the led is green for 2 seconds
-
DC motor rotates at the maximum speed of 2 second in the reverse direction and the led is blue
-
DC motor is stoped and the led is green for 2 seconds
-
The above process is run repeatedly.
References
※ NOTE THAT:
Some components works on 3.3v and others works on 5v!