NODE MCU INTERFACING WITH DC MOTOR
Introduction
DC motor converts electrical energy in the form of Direct Current into mechanical energy in the form of rotational motion of the motor shaft.
The DC motor speed can be controlled by applying varying DC voltage; whereas the direction of rotation of the motor can be changed by reversing the direction of current through it.
For applying varying voltage, we can make use of the PWM technique. For reversing the current, we can make use of H-Bridge circuit or motor driver ICs that employ the H-Bridge technique.
For more information about DC motors and how to use them, H-Bridge circuit configurations, and PWM technique, refer the topic DC Motors in the sensors and modules section.
NodeMCU based ESP8266 can be used to control the speed and rotational direction of DC Motor. NodeMCU has a PWM feature on its GPIO pins using which we can control DC motor.
Interfacing Diagram
NodeMCU interface with DC Motor through L293D driver
Example
Let's control the speed and rotational direction of the DC motor using the NodeMCU Kit.
Here, the potentiometer is used as a means for speed control and input from the tactile switch is used to change the direction of the motor.
We can make use of the ADC feature of NodeMCU to read the potentiometer. And we can make use of the GPIO interrupt feature of NodeMCU to read the switch state.
L293D motor driver IC is used for controlling the direction of the motor. PWM wave generated on the NodeMCUis used to provide variable voltage to the motor through L293D.
We can write program to NodeMCU using ESPlorer IDE (with Lua script) as well as using Arduino IDE (with Arduino sketch)
Arduino Sketch for DC Motor
bool d1 = HIGH;
bool d2 = LOW;
void motor_direction(){
d1 = !d1;
d2 = !d2;
for(inti = 0; i<10000; i++)
for(int j =0; j<10000; j++);
}
void setup() {
Serial.begin(9600);
pinMode(D5, OUTPUT); /* PWM pin for Speed Control */
pinMode(D6, OUTPUT); /* Motor control pin 1 */
pinMode(D7, OUTPUT); /* Motor control pin 2 */
pinMode(D8, INPUT_PULLUP); /* Interrupt pin for direction control */
attachInterrupt(D8, motor_direction, HIGH);
/* call motor direction function on HIGH level at pin 8 */
}
void loop() {
intpwm_adc;
pwm_adc = analogRead(A0); /* Input from Potentiometer for speed control */
digitalWrite(D6,d1);
digitalWrite(D7,d2);
analogWrite(D5,pwm_adc);
delay(100);
}
For more intresting topics please like and share