Today we will learn how to control our motors.
Motors can turn clockwise or counter-clockwise by switching the pos(+) and neg(-) pins.
NEVER connect a motor directly to an Arduino pin.
The L293D Integrated Circuit lets the Arduino control the motors without ever risking a current going back into the Arduino.
1. Connect a constant current from the Arduino VCC pin to the VCC1 and VCC2 on the L293D. (Can also use a higher voltage directly from a battery)
2. Connect all the GND pins on the L293D to one of the GND pins on the Arduino. (Connect all the L293D pins together and then use one wire to connect them all back to the Arduino GND)
3. Connect pin 7 on the Arduino to the Right Motor On/Off pin on the L293D. This pin controls whether the right motor gets any power at all.
4. Connect pin 8 on the Arduino to the Right Motor Input 1 pin on the L293D.
5. Connect pin 9 on the Arduino to the Right Motor Input 2 pin on the L293D.
6. Connect one of the motor wires to the Right Motor Output 1 pin on the L293D.
7. Connect the other motor wire to the Right Motor Output 2 pin on the L293D
8. Double check all the wiring is correct and move on to the Arduino code.
The code for this project is simple, we need to set all the Right motor pins to OUTPUT mode.
Then we need to make sure the Right Motor On/Off pin is always set to HIGH.
Finally to make the motor turn we need to set one of the Input pins to HIGH and the other to LOW.
If we reverse the Input pins then the motor will turn the opposite way.
int rMotorOn = 7;
int rMotor1 = 8;
int rMotor2 = 9;
void setup() {
pinMode(rMotorOn, OUTPUT);
pinMode(rMotor1, OUTPUT);
pinMode(rMotor2, OUTPUT);
digitalWrite(rMotorOn, HIGH); //rMotorOn should always be HIGH so we set it in the setup() block
}
void loop() {
digitalWrite(rMotor1, HIGH);
digitalWrite(rMotor2, LOW);
}
Lets design a quick test to make sure our motor works perfectly.
Change the code so that the motor does the following:
1. Turn clockwise for 2 seconds.
2. Turn counter-clockwise for 2 seconds.
3. Stop for 2 seconds.
4. Repeat.