ESP8266 NodeMCU DC Motor Control

This post shows how to control a 5V DC motor speed and direction of rotation using ESP8266 NodeMCU development board and L293D motor driver chip.
The speed and rotation direction of the DC motor is controlled from a rotary encoder connected to the NodeMCU board.

In this project I used the rotary encoder shown below:

rotary encoder

This rotary encoder has 5 pins: GND, + (+5V or 3.3V), SW (push button), DT (pin B) and CLK (pin A).
As an addition to the rotary encoder there is a push button and three a pull up resistors for pins SW, DT and CLK of 10k ohm. With the three pull-up resistors, the normal state of each terminal is logic high.
The rotary encoder generates (when rotating) two square waves on pins A (CLK) and B (DT) with 90° out of phase as shown in the figure below:

rotary encoder output waveform

Since the normal state of pin A (CLK) and pin B (DT) are logic high we’ve to detect falling (transition from high to low) of one of them, here pin A is used to detect the movement of the rotary encoder in both directions (falling of pin A signal). Direction of rotation can be detected by knowing the status of pin B, if pin B is logic high this means the direction of rotation is clockwise (CW), and if pin B is logic low this means the direction of rotation is counter clockwise (CCW).

Hardware Required:

  • ESP8266 NodeMCU development board
  • Rotary encoder
  • 5V DC motor
  • L293D motor driver
  • Breadboard
  • 5V source
  • Jumper wires

ESP8266 NodeMCU DC motor control circuit:
Project circuit schematic diagram is shown below.

ESP8266 NodeMCU DC motor control circuit

All the grounded terminals are connected together (don’t forget the external 5V source negative terminal).

As shown in the circuit diagram there is an external power source of 5V (5V Power Plug). This source is used to fed the DC motor with sufficient power because the NodeMCU board may not be able to do that.

The L293D quadruple half-H drivers chip allows us to drive 2 motors in both directions. With two PWM outputs from the NodeMCU board we can easily control the speed as well as the rotation direction of one DC motor. (PWM: Pulse Width Modulation).

The L293D is supplied with 5V that comes from the external power source (pins: VCC1, VCC2 and EN1,2).

Pin IN1 and pin IN2 are the control pins where:

IN1 IN2 Function
L H Direction 1
H L Direction 2
L L Fast motor stop
H H Fast motor stop

Pins IN1 and IN2 of the L293D are respectively connected to NodeMCU pins D1 and D2. These pin (D1 & D2) are PWM signal outputs, at any time there is only 1 active PWM, this allows us to control the direction and the speed of the DC motor by varying the duty cycle of the active PWM signal. The active PWM pin decides motor direction of rotation (one at a time, the other output is logic 0).

The rotary encoder has 5 pins, they’re connected to the NodeMCU board as follows:
GND pin of the rotary encoder is connected to NodeMCU GND pin,
+ pin is connected to 3V3 PIN,
SW pin is connected to pin D5,
DT pin is connected to pin D6,
CLK pin is connected to pin D7.

ESP8266 NodeMCU DC motor control Arduino code:
Input pins (connected to the rotary encoder) and output pins (connected to the L293D) are defined in the Arduino code as shown below:

When rotated, the rotary encoder generates a series of pulses on pins DT and CLK which interrupt the NodeMCU microcontroller (ESP8266EX). The two interrupts are initialized with the following lines:

Both interrupts call the function void enc_read() which is used to read the state of the two rotary encoder pin A (CLK) and pin B (DT).

Full code:

The video below shows my simple hardware circuit test:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.