Controlling a DC motor using Arduino
In this chapter, we will get to know how to control a DC motor with Arduino.
You can also run the DC motor by using the same code as the LED blink. We can consider the motor as an LED. As discussed earlier, a DC motor is a two-wired motor. One wire is the positive supply and other is ground. If we connect the positive terminal of the motor to the positive terminal of the battery, and the negative terminal of the motor to the negative terminal of the battery, the motor will run in one direction, and if we reverse the connection, the motor will run in the reverse direction.
By connecting the motor to two digital pins of the Arduino, we can control the direction of the motor. In the following basic code, we will run the motor in one direction for five seconds and then we will reverse the direction of the motor. Connect pin 3 and pin 4 of the Arduino with the two wires of the motor:
int motorPos = 3; int motorNeg = 4; void setup() { pinMode(motorPos, OUTPUT...