Controlling DC motor using L298N Motor Driver and ESP
ESP interfacing with L298N motor driver
Before we start interfacing esp with motor driver, lets learn why is a motor driver needed. DC motor are high voltage and high current devices which are usually outside the range of a micro controller. Motor driver acts as an interface between micro controller and DC motor isolating both circuits and providing ability to control speed and direction precisely. In L298N motor driver, it has a H bridge which is a switching circuit that can control both directions of a motor precisely.
Having a motor driver helps you to provide instructions about speed and direction of motor from a micro controller and be assured that the responsibility is now owned by the driver. This specific motor driver operates at 12V. Lets see how to run this through ESP. We will turn a toy car motor with a wheel.
Parts Needed
1. Multi Output Power module. (Detailed video here)
2. ESP 01
3. L298N motor driver (Detailed video here)
4. Jumper wires
5. DC motor and wheel
void setup() { pinMode(0,OUTPUT); digitalWrite(0, LOW); pinMode(2, OUTPUT); digitalWrite(2, LOW); } void loop() { // Move motor forward for 5 sec digitalWrite(0, LOW); digitalWrite(2,HIGH); delay(5000); // Stop for 1 sec digitalWrite(0, LOW); digitalWrite(2,LOW); delay(1000); // Move motor reverse for 5 sec digitalWrite(0, HIGH); digitalWrite(2,LOW); delay(5000); // Stop for 1 sec digitalWrite(0, LOW); digitalWrite(2,LOW); delay(1000); }
Code Explanation
We will use IO0 and IO2 on esp to control direction of the motor. In this code we are only controlling the direction of the motor and so we connected a jumper for EN2 pin on motor driver to 5V.
We initialised pin 0 and 2 in setup() and initialised them to LOW to prevent motor from running during setup. Inside the loop, motor will be run forward for 5 seconds, stop for 1 second, reverses direction & runs for 5 seconds, stops for a sec and loop continues.
Use this post to upload code to ESP 01
Hardware Connections
If you are using NodeMCU instead of ESP 01 please use the last column for connections.
Replace IO0 and IO2 to D3 and D4 in code if you are using NodeMCU.
Power Module ESP01 L298N Motor Driver NodeMCU 12V - 12V - GND - GND - 3.3V 3.3V - - GND GND - - - IO0 IN3 D3 - IO2 IN4 D4
Output
Trouble Shooting
1. Power adapter turning on and off frequently - Check if your motor being used is rated below 800mA. If so use a breakout board as described here or alternate power source.
2. L298N not powering up even after connecting jumper wires correctly - Check if your jumper cables are rated for current rating needed. Replace your jumper wires if needed.
3. DC motor not turning - Check by directly connecting the motor to 12V supply and check if motor is running.
Comments
Post a Comment