In this tutorial I will teach you how to control the speed and direction of the DC motor using the nodemcu WiFi module and the l293d motor driver. Nodemcu will serve a web page in the browser of your mobile, desktop, laptop or notebook. The web page lists the DC motor controls. The user can control the rotation speed of the motor on the web page and change the rotation direction of the DC motor through the controls on the web page. The tutorial also teaches how to interface h bridge l293d ic with nodemcu esp8266 12e WiFi module.
Nodemcu DC Motor and l293d Motor Driver
Two DC motors are used in the design. The power consumption of DC motors is 12 volts at 300 mA current. The Nodemcu esp8266 WiFi module works on 3.3 volt TTL logic. We cannot power the DC motor directly through the nodemcu output pins. We need extra power and an external circuit that can handle the heavy load of the motor. Transistor is the best choice for controlling heavy loads with microcontrollers. The base of the transistor receives 3.3 volt TTL microcontroller output signal and can drive heavy loads on its collector and emitter side. Through the transistor we can also control the speed of the motor. But the problem is in the engine steering control. Using a single transistor, it is not possible to change the direction of rotation of the motor. To change the direction of the motor we need a combination of transistors. This combination of transistors is known as an h-bridge driver. Through the h-bridge circuit we can easily change the direction of rotation of the motor. The H-bridge circuit can also be effectively used to control the rotational speed of the DC motor.
Making an h-bridge circuit creates a very confusing circuit. It is also difficult and time-consuming to select the appropriately rated transistors or mosfets (h-bridge can also be made with fet) for the circuit needs. Pre-assembled and fabricated H-bridge circuits are available in the market and also come in IC's. L293d is a popular half-h bridge IC available in the market.
L293d Driver
L293d has two half H. bridge channels. Each channel can control a single motor independently. We can control two motors with a single l293d motor driver. Each channel can control loads that require 0 – 36 volts and 600 mA of current. Each channel has two input pins and two output pins. Our load (motor) is connected to the channel output pins. Controls are connected to the input pins. Each channel is activated via an enable pin. To learn more about the internal structure of the individual channel, each pin function, and the operation requirements of the L293D, I recommend that you follow the tutorial below.
L293d pinout and operation
|
|
The above tutorial is very important for understanding the project circuit diagram and the code below. Please pick it up before proceeding.
The speed of DC motors can be controlled through several methods. The most popular is to vary the motor input voltage. I will also vary the input voltage of the DC motor to control the speed of the DC motor. The input voltage can be varied using a variable resistor. Just like in houses, we turn the knob on the switch port to control the speed of the ceiling fan. By turning the knob we vary the input power (voltage, current) of the fan.
In our case, we want to control the speed digitally and not physically (in the case of the fan button discussed in the paragraph above). Digital control of DC motor speed is possible by turning the motor control system on and off. Since we are using a nodemcu microcontroller in our project, we must turn the system that controls the motor on and off. We are using l293d motor driver in our project and our motors are connected through the output pins of its channels. If we turn the individual channels of the l293d on and off, we can vary the motor output voltage and therefore we can control the motor speed.
Turning individual channels of the L293D on and off in a controlled manner is an easy task. We can generate a controlled on/off signal from nodemcu which can control the rotational speed of the motor in the desired way. PWM (Pulse Width Modulation) is the name of the technique through which control signals can be generated from any microcontroller. I am also generating pwm signal from nodemcu for motor speed control.
In a pulse width modulation signal output, the duty cycle of the signal is controlled. The duty cycle is the amount of time the signal is high in a given period of the digital signal. On the right side, the work cycles are shown diagrammatically. In the first wave, the duty cycle is 50%. This means that the signal is high for 50% of the period. For example, the period is 2 seconds. At 50% duty cycle, the output remains high for 1 second and low for 1 second. If the duty cycle is 75%. The output remains high for 1.5 seconds and low for 0.5 seconds. I hope it makes sense to you. When the periods are combined, the frequency is formed. And it's usually in kilo or mega Hz.
|
|
Design circuit
As I am using two motors in the project, we need four controls to change the motors' directions. Two controls for each engine. Four digital pins of the Nodemcu are used as controls. To connect
- GPIO-15 with l293d input 1
- GPIO-13 with l293d input 2
- GPIO-12 with l293d input 4
- GPIO-14 with l293d input 3
To control the speed of the motors I am generating a pwm signal on the GPIO-5 and GPIO-4 of the nodemcu esp8266. Each enable pin of the L293D channel receives the PWM signal generated to control the rotation speed of the motor. To connect
- GPIO-5 with ENABLE-1 of l293d
- GPIO-4 with ENABLE-2 of l293d
Vss of l293d is supplied with +5 volts. In the Vs of the l293d the engine operating voltage is supplied +12 volts in our case. The L293D and Nodemcu grounds must be common for the circuit to function correctly.
DC motor speed and direction control with nodemcu esp8266 WiFi module and l293d motor driver
Coming to the code part of the project. The code is written in Arduino IDE. Arduino provides ads for nodemcu esp8266 mainboards and we can write, compile and program nodemcu esp8266 is arduino IDE.
In the code I first included the ESP8288WiFi.h library. This library contains the main nodemcu initialization functions. Then the WiFi network SSID and password are entered. You must enter your SSID and WiFi password before moving the head.
const char* ssid = “Your SSID”;
const char* password = “Your Wifi password”;
Enter the SSID and password in double quotes. After SSID and password. L293d channel nodemcu control pins and pwm pins are initialized. In the configuration function, Arduino IDE serial communication starts at a baud rate of 115200 bps. Then the control pins are declared as output. In the wards, nodemcu asks your WiFi router for an IP distribution. After IP allocation, nodemcu will start its server.
In the code above, some text is out of order because HTML code is present in the project code. The HTML code starts running in the browser.
When nodemcu started its server. Started serving the web page. Just make the circuit and upload the code above to your nodemcu.
After sending the code in nodemcu open the arduino serial monitor from your arduino ide. On the Arduino serial monitor, you will see the nodemcu requesting the WiFi router for IP assignment and then it will start its server. When the server is fully operational, the nodemcu will display the server address on the Arduino serial monitor. This address is the HTML address of the engine controls web page. You must enter this address in your device's browser (mobile, desktop or laptop). You can see the serial monitor snapshot on the right side.
|
|
NOTE: Both the server (nodemcu esp8266) and the client (cell phone, laptop and desktop) must be connected to the same WiFi. To communicate between two modules, they must be on the same network. Therefore, your nodemcu and client (mobile, desktop) must be on the same network. If any of them are on a different network, you won't even be able to see the web page in the client's browser.
As soon as you enter the above address into your browser, a web page will be displayed on the client containing the engine controls.
DC Motor Web Page Nodemcu Controls: Speed and Direction
The above page will be displayed in the customer's browser. You can know how to start, stop and switch direction of DC motor using the web page. You can also change the engine rotation speed. You can select any three speeds. At the bottom of the web page, the engine status is displayed. If it is spinning? in which direction? and the instantaneous rotation speed is displayed.
Motors 1 and 2 will start to rotate clockwise.
It will alternate direction and begin rotating the motors counterclockwise. To return to clockwise, press Start Engine again.
In the loop function, nodemcu is waiting for any request from the client. If the request is made, nodemcu performs the desired function on the client's request and before closing the request, nodemcu responds with the updated status of the DC motor.
Future work
I showed how to control the DC motor speed with the nodemcu WiFi module and the l293d h bridge motor driver. In my project it is possible to select only three work cycles. In the future, you can insert a text field and submit button on the web page and accept the user's custom work cycle via the keyboard. There are many other possibilities to improve the functionality of the project, making the RC car, robot, etc.
Download the project code. The folder contains the arduino ide .ino file. Each instruction in the code is explained thoroughly. If you have any questions and queries, write them below in the comments section.
Code/Files