Lets control an DC motor with a L293D chip and make it so it will go clockwise or counter clockwise. I received a kit from GearBest that included all the parts I needed for this project and recommend it to you if you are just starting with Arduino, You can check it out here!
The parts you will need are:
- Arduino Board with PWM
- L293D Motor Driver
- Wires
- External Power Supply (not needed but recommended)
- 10 minutes
Lets Begin!
Step 1: Wire Everything!Now we're going to wire everything. I created the schematic to make it easier to understand, then used that for reference when wiring it. It's super simple!
For people that can't see the picture notes: In the schematic the bottom wire from the chip is the one you would connect to the power supply positive, I didn't add it to keep things simple.
Step 2: Code the ArduinoThe code is super simple, we use "for" statements and "analogWrite" to control it.
For Example:
when pin 5 is low and pin 6 is high it will rotate clockwise
or
when pin 5 is high and pin 6 is low it will rotate counter-clockwise
You can download the code from here.
Here is the code also:
void setup() {
}
void loop() {
for(int i = 0; i<255; i++){
analogWrite(5, i);
analogWrite(6, 0);
delay(5);
}
for(int i = 0; i<255; i++){
analogWrite(5, 255-i);
analogWrite(6, 0);
delay(5);
}
for(int i = 0; i<255; i++){
analogWrite(5, 0);
analogWrite(6, i);
delay(5);
}
for(int i = 0; i<255; i++){
analogWrite(5, 0);
analogWrite(6, 255-i);
delay(5);
}
}
Step 3: Test It Out!Your motor should now get increasingly faster then decelerate reverse direction and repeat!
If you have any questions leave them in the comments and I will try to help you out.
Hope you enjoyed:
Comments