NodeMCU and L293D DC motor control

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.

NodeMCU and L293D DC motor control
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 PWM pins

NodeMCU DC motor control Program

The following is the complete NodeMCU program code for controlling DC motor.

    
// NodeMCU DC motor control with L293D
//source:https://www.ee-diary.com

    // Define motor control pins
    #define ENA A4
    #define IN1 D2
    #define IN2 D3

    // Define push button pins
    #define DIR_BUTTON D7
    #define STOP_BUTTON D8

    // Define variables for motor speed and direction
    int motorSpeed = 0;
    bool motorDirection = true; // true = clockwise, false = counterclockwise

    void setup() {
    // Set motor control pins as outputs
    pinMode(ENA, OUTPUT);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);

    // Set button pins as inputs with internal pull-up resistors enabled
    pinMode(DIR_BUTTON, INPUT_PULLUP);
    pinMode(STOP_BUTTON, INPUT_PULLUP);
    }

    void loop() {
    // Read button states
    bool dirButtonState = digitalRead(DIR_BUTTON);
    bool stopButtonState = digitalRead(STOP_BUTTON);

    // Change motor direction if direction button is pressed
    if (dirButtonState == LOW) {
        motorDirection = !motorDirection; // toggle direction
    }

    // Stop motor if stop button is pressed
    if (stopButtonState == LOW) {
        motorSpeed = 0;
        analogWrite(ENA, 0); // set motor speed to 0 to stop the motor
    } else {
        // Set motor speed
        motorSpeed = analogRead(A0) / 4; // read potentiometer value and scale to 0-255 range
    }

    // Set motor speed
    analogWrite(ENA, motorSpeed);

    // Set motor direction
    if (motorDirection) {
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
    } else {
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
    }
    }
 

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.

    // Define motor control pins
    #define ENA A4
    #define IN1 D2
    #define IN2 D3

    // Define push button pins
    #define DIR_BUTTON D7
    #define STOP_BUTTON D8

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).

    // Define variables for motor speed and direction
    int motorSpeed = 0;
    bool motorDirection = true; // true = clockwise, false = counterclockwise

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.

    void setup() {
    // Set motor control pins as outputs
    pinMode(ENA, OUTPUT);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);

    // Set button pins as inputs with internal pull-up resistors enabled
    pinMode(DIR_BUTTON, INPUT_PULLUP);
    pinMode(STOP_BUTTON, INPUT_PULLUP);
    }

In the loop function, we first read the states of the direction button and the stop button:

    // Read button states
    bool dirButtonState = digitalRead(DIR_BUTTON);
    bool stopButtonState = digitalRead(STOP_BUTTON);

 If the direction button is pressed, we toggle the motor direction:

    // Change motor direction if direction button is pressed
    if (dirButtonState == LOW) {
        motorDirection = !motorDirection; // toggle direction
    }

If the stop button is pressed, we set the motor speed to 0 to stop the motor:

    // Stop motor if stop button is pressed
    if (stopButtonState == LOW) {
        motorSpeed = 0;
        analogWrite(ENA, 0); // set 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:

else {
        // Set motor speed
        motorSpeed = analogRead(A0) / 4; // read potentiometer value and scale to 0-255 range
    }

 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.

    // Set motor speed
    analogWrite(ENA, motorSpeed);

 Finally, we will set the direction of the motor using the IN1 and IN2 pins. 

    // Set motor direction
    if (motorDirection) {
        digitalWrite(IN1, HIGH);
        digitalWrite(IN2, LOW);
    } else {
        digitalWrite(IN1, LOW);
        digitalWrite(IN2, HIGH);
    }

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:

[1] Nodemcu PWM controlled DC motor

[2]  NodeMCU ESP8266 PWM: LED brightness fading

4 Comments

Previous Post Next Post