Controlling a DC motor is a fundamental task in the field of electronics and can be used in a wide range of applications, from robotics to home automation. Here, we will look at how to control a DC motor using a NodeMCU 8266 microcontroller and an L293D motor driver with push buttons for direction control and stop.
First, let's take a look at the hardware components required for this project. We will need a NodeMCU ESP8266 microcontroller, an L293D motor driver, a DC motor, and two push buttons. The ESP8266 NodeMCU is a low-cost Wi-Fi enabled microcontroller based on the ESP8266 chip. The L293D motor driver is a popular integrated circuit that can control up to two DC motors with a maximum voltage of 36V.
Interfacing NodeMCU, L293D & DC motor
The following is the circuit diagram for connecting NodeMCU with L293D motor driver, DC motor,potentiometer and push buttons.
The NodeMCU pins D2, D3, D4 are connected to L293D EN1, IN1 and IN2 respectively. The output pins OUT1 and OUT2 are connected to the DC motor terminals. The Vss pin and Vs pins of L293D are connected to +5V and +12V respectively. The pins 4,5,12 and 13 of L293D are grounded to same ground as the NodeMCU ground. Two push buttons are connected to NodeMCU D7 and D8. The push button connected to D7 is used for direction control and the push button connected to D8 is to stop the DC motor. A 10KOhm potentiometer which is used for DC motor speed control is connected to the A0 pin of NodeMCU.The following shows the NodeMCU board with pins.
NodeMCU DC motor control Program
The following is the complete NodeMCU program code for controlling DC motor.
To begin, we will define the motor control pins and the push button pins. The ENA pin controls the speed of the motor, while the IN1 and IN2 pins control the direction of the motor. The DIR_BUTTON and STOP_BUTTON pins are connected to the push buttons that will be used to control the direction and stop the motor.
Then two variables: "motorSpeed" and "motorDirection" are created. The variable "motorSpeed" is used to set the speed of the motor. It is an integer (whole number) variable, and its initial value is set to 0. In the code, the value of "motorSpeed" is determined by reading the input from a potentiometer, which is connected to an analog pin on the NodeMCU. The potentiometer is used to control the speed of the motor by adjusting the voltage level of the input signal. The variable "motorDirection" is used to set the direction of the motor. It is a boolean (true/false) variable, and its initial value is set to true. In the code, the value of "motorDirection" is changed by pressing a push button that is connected to a digital pin on the NodeMCU. If the button is pressed, the direction of the motor is toggled between clockwise (true) and counterclockwise (false).
Next, we will set the motor control pins as outputs and the push button pins as inputs with internal pull-up resistors enabled. The internal pull-up resistors ensure that the inputs are in a known state when the push buttons are not pressed.
In the loop function, we first read the states of the direction button and the stop button:
If the direction button is pressed, we toggle the motor direction:
If the stop button is pressed, we set the motor speed to 0 to stop the motor:
Otherwise, we read the potentiometer value connected to the NodeMCU's analog pin A0 and scale it to a range of 0 to 255:
We then set the motor speed using the analogWrite function. Under the wood, the speed of the DC motor is controlled by the duty cycle of the PWM signal generated by the NodeMCU. See NodeMCU PWM: Control DC motor speed with potentiometer to learn more.
Finally, we will set the direction of the motor using the IN1 and IN2 pins.
This code is a good starting point for building more complex motor control projects. For example, you could add additional buttons to control the motor speed or direction, or you could integrate the motor control with other sensors or devices.
Watch the following video demonstration to learn how to control a DC motor using ESP8266 NodeMCU, L293D motor driver, push buttons for direction control and stopping, potentiometer for speed control.
Another possible modification to this code would be to add feedback mechanisms to ensure that the motor is functioning properly. For example, you could add sensors to detect the motor speed or position, or you could use feedback from the motor driver to monitor the motor current or voltage.
Overall, the ESP8266 NodeMCU is a versatile microcontroller that can be used for a wide variety of projects. By combining it with a motor driver like the L293D, you can easily control DC motors for robotics, automation, or other applications. And by using push buttons to control the motor direction and speed, you can create simple and intuitive interfaces for your projects.
References and Further readings:
simulator
Proteus Professional
can you share with me the link for the library of NODEMCU
you can donwload the NodeMCU proteus library from the following link:https://teraboxapp.com/s/1t5TdTr2SgemqKyKBftZmJA
i want install library not coby