NodeMCU PWM: Control DC motor speed with potentiometer

Here it is shown how NodeMCU can control a DC motor speed by generating PWM signal. An external pentiometer is connected to NodeMCU and is used to set the duty cycle of the PWM signal.

ESP8266 NodeMCU

NodeMCU(Node MicroController Unit) is a board which can be either based on ESP8266 SoC or ESP32 SoC. The following shows the 32-bit Lolin type ESP8266 NodeMCU board.

ESP8266 NodeMCU

The ESP8266 SoC(System on Chip) based NodeMCU used here enables to create internet of things(IoT) based applications. NodeMCU enables one to connect devices such as sensors and actuators to a WiFi network and to the internet, it supports various communication protocols such as SPI, I2C, it can sense and acquire external analog signal, can measure external events, can perform normal digital I/O, can generate PWM signals and so on.

Some of the NodeMCU hardware features are below.

- The operating voltage is 3.3V. 

- It has internal 3.3V voltage regulator.

- 4.5V to 10V can be supplied.

- The CPU frequency is 80MHz.

- It has 4MB flash memory and 64KB SRAM memory.

- It has 11 digital pins

- It has 1 analog pin with ADC(Analog to Digital Converter) range from 0.3V to 3.3V.

- It supports 802.11 b/g/n WiFi protocols.

- It is USB interface and uses CH340G USB to serial converter IC.

- It has one SPI, two USART, one I2S and one I2C interface support. 

NodeMCU PWM pins

The Lolin  NodeMCU has 4 PWM pins which are indicated in the picture below.

NodeMCU lolin PWM pins

L298N Motor Driver

Some large motors can requires high current and as such dc motor driver are used to provide more current at the motor voltage rating. Here we will use L298N motor driver IC(Integrated Circuit) to drive a 12V DC motor with current rating below 2A.

The L298N motor driver IC is shown below.

L298N motor driver IC

 The L298N shown above is Multiwatt15 type IC which has 15 pins. The pin diagram of the L298N Multiwatt15 IC is shown below.

L298N motor driver IC pins diagram

The L298N has two full bridge driver inside it. Each full bridge driver can handle upto 2A of current. It has a Power rating of 25W. It has two power supply pins Vs(pin 4) and Vss(pin 9). The Vss is for the logic voltage which can be up to 7V and Vss is for the motor power supply which can be up to 50V. It has two current sensing pins A(pin 1) and B(pin 15) which are used to monitor current in the two full bridge driver. Each full driver has two inputs(pin 5 and 7, pin 10 and 12), two outputs(pin 2 and 3, pin 13 and 14) and enable inputs(6 and 11).

To use one full bridge driver, signal(HIGH or/and LOW) is applied to two inputs. The enable pin controls where the full bridge driver is ON or OFF. If LOW signal is applied to enable pin then the full bridge driver is OFF and there is no output irrespective of the signal input at the output. The output pins are connected to the motor terminal. PWM signal are sent to enable pin to control the speed of the motor. The two inputs controls the rotation direction of the motor. For example, if HIGH is sent to input IN1 and LOW is sent to input IN2 then the motor rotates in clockwise direction provided that the enable pin ENA is HIGH.

NodeMCU PWM DC Motor Circuit Diagram

 The following circuit drawing shows how to wire the L298Ndc motor speed controller to the ESP8266 NodeMCU board.

NodeMCU/ESP8266 L298N DC motor circuit diagram

In the above circuit diagram we have connected pin D5, D6 and D7 of NodeMCU to the ENA, IN1 and IN2 respectively of the L298N motor driver integrated circuit(IC). The output is taken from OUT1 and OUT2 and connected to the DC motor terminals via the diode bridge as shown. The diodes are used to prevent back emf reaching and damaging the NodeMCU pins. The VCC pin of L298N is connected to +5V where the NodeMCU is also connected. The Vs pin of NodeMCU is connected to +12V power supply source.

NodeMCU PWM Control DC motor speed with potentiometer

NodeMCU DC motor PWM Program Code

The following is the program code for the NodeMCU/ESP8266 which turns the DC motor on with variable speed control using the potentiometer.

//source:https://www.ee-diary.com

const int ENA = D5;
const int IN1 = D6;
const int IN2 = D7;


void setup() {
	Serial.begin(9600);
	pinMode(IN1,OUTPUT);
	pinMode(IN2,OUTPUT);
	pinMode(ENA,OUTPUT);
}

void loop(){
	int pot = analogRead(A0);
	int PWMDC = map(pot,0,1023,0,255);
	digitalWrite(IN1,HIGH);
	digitalWrite(IN2,LOW);
	analogWrite(ENA,PWMDC);
	Serial.println(PWMDC);
}

In the above code, we have created some alias name for the pin connection D5,D6 and D7 as ENA, IN1 and IN2 respectively. In the setup() function we have made the pins as output and started the serial communication with baud rate of 9600 in order to diagnosis and view the PWM value generated on the serial monitor. In the main loop we have read in the analog signal due to potentiometer from coming into A0 pin of ESP8266. The analog signal voltage level is converted to digital representation from 0 to 1023 because the internal ADC of NodeMCU is 10-bit analog to digital converter. This value is mapped to PWM value range which is from 0 to 255. Here 0 means 0% duty cycle and 255 means 100% duty cycle. Then we send high signal to the pin IN1 and low signal to the pin IN2 to rotate the DC motor in forward direction. And then we sent out the read PWM value from the potentiometer to the ENA pin. When the duty cycle is 255 or 100% then the motor speed is maximum and when it is 0 or 0% duty cycle the speed is minimum. In this way we can control the speed of the DC motor using a potentiometer. Finally we print out serially the value of the PWM.

Video Demonstration

The following video demonstrates how the NodeMCU controls the DC motor speed by generating PWM signal with potentiometer that set the duty cycle.

 

See previous tutorial NodeMCU ESP8266 PWM: LED brightness with Potentiometer

References:

[1] NodeMCU and L293D DC motor control

Post a Comment

Previous Post Next Post