- Run a DC motor using NodeMCU and H-bridge
- Control direction of motor.
- NodeMCU board (quantity: 1 no.)
- Micro USB cable A to B (quantity: 1 no.)
- Breadboard (quantity: 1 no.)
- DC BO motor (quantity: 1 no.)
- DC barrel jack (quantity: 1 no.)
- 12V DC adaptor (quantity: 1 no.)
- Jumper wire – male to male (quantity: 2 no.)
- Jumper wire – male to female (quantity: 2 no.)
- Arduino IDE on your computer
A DC motor is an actuator that rotates when supplied with DC power. When a voltage is applied across its terminals, it rotates. Usually DC motors come in a compact geared set, called BO motors.
BO DC motor
Unlike sensors which can be powered by the NodeMCU itself, a DC motor requires additional circuitry to be powered simply because the NodeMCU on it’s own cannot provide adequate current to run the motor. To overcome this limitation, we use a ‘motor driver’. This device’s main function is to power the motor using an external high current source and to also provide a communication pathway to convey control signals from the NodeMCU to the motor. We use a H-bridge motor driver to perform these functions.
A H-bridge circuit is the name given to a set of electronic switches built into an IC. L293D is an example of such an IC. By controlling which switches are closed/open, we can control the polarity of voltage obtained at the output.
Usually two switches are controlled by one input pin. Since there are four switches, we have two input pins on the IC – IN pin1 and IN pin2.
- When we give HIGH to IN pin1 and LOW to IN pin2, switches S1 and S4 close and switches S2 and S3 open. This completes the circuit and current flows in one direction, causing the motor to rotate in one direction.
- When we give LOW to IN pin1 and HIGH to IN pin2, switches S1 and S4 open and switches S2 and S3 close. This completes the circuit and current flows in the opposite direction, causing the motor to rotate in the opposite direction.
- When we give LOW to both IN pin1 and IN pin2, all switches are open and no current flows. If the motor is rotating, it slows to a stop.
- When we give HIGH to both IN pin1 and IN pin2, all switches are closed and there is no voltage difference between motor terminals. Hence, no current flows. If the motor is rotating, it slows to a stop.
The H-bridge circuit comes as a well packaged module. It can control 2 motors at the same time. Each motor can be controlled by 2 inputs. The inputs are designed such that if Input 1 is given high voltage, S1 becomes closed and S2 becomes open; if Input 1 is given low voltage, S1 becomes open and S2 becomes closed. Similarly, Input 2 controls S3 and S4.
Interfacing a BO DC motor with NodeMCU using H-bridge
To copy the code, right click on view raw at the bottom of the code, click on open link in new tab and then copy the code.