Control A DC Motor With Arduino and l293d Chip
Control A DC Motor With Arduino and l293d Chip
This is a quick guide with a bit of extra info (pin configurations etc..) that Ive learnt along the way on how to use the L293D with the Arduino, showing that we can: A) Use a supplemental power source to power the DC motor B) Use the L293D chip to drive the motor C) Use a switch to change the direction of the motor
I would consider using a transistor or voltage regulator). The only thing to remember is that the grounding connection must be shared/ common for both supplies. Below you can see the pin layout for the chip and the truth table showing the output logic.
Generally speaking most DC motors require a lot more current than the Arduino board can provide for instance the motor that Im using needs around 5 to 6 Volts. Now I could use a 12 Volt power source for the Arduino, but then its going to drain quickly when it has to power everything, especially if I was to add in another motor and a couple of servos, so instead my Arduino runs off of my 9 Volt power supply.
Youll need a few capacitors in this circuit to smooth out the power load to the motors as much as possible to help avoid any spikes and stabalise the current. Im using a 50 Volt 10 uF capacitor on the power supply I suggest you do this as the bare minimum. You could also add in a capacitor for each motor that you use something like a 220nF multilayer ceramic capacitor should be OK for the small motors.
1. Enables and disables the motor whether it is on or off (high or low) comes from the Arduino digital PWM pin 9 2. Logic pin for the motor (input is either high or low) goes to Arduino digital pin 4 3. Is for one of the motor terminals can be either +/4. Ground 5. Ground 6. Is for the other motor terminal 7. Logic pin for our motor (input is either high or low) goes to Arduino digital PWM pin 3 8. Power supply for the motor, this should be given the rated voltage of your motor, so mine is from a 6V supply 9. Enables and disables the 2nd motor on or off (high or low) 10. Logic pin for the 2nd motor (input is either high or low) 11. Is for one of the 2nd motor terminals can be either +/12. Ground 13. Ground 14. Is for the 2nd motors other terminal 15. Logic pin for the 2nd motor (input is either high or low) 16. Connected to +5V, in this case the power from motor supply You can see from my photos how Ive placed the L293D and wired it according to the above pins. Next I have my switch on Arduino digital pin 2 and I have the GND pin from Arduino connected to the GND rail on my breadboard. I also add the
capacitor in between the power supply making sure that the negative and positive terminals are correctly aligned. Finally I complete the circuit by adding in wires to carry the current from one side of the breadboard to the other and I add in the motor and its power supply.
pinMode(motor1Pin2, OUTPUT); pinMode(enablePin, OUTPUT); // set enablePin high so that motor can turn on: digitalWrite(enablePin, HIGH);
void loop() { // if the switch is high, motor will turn on one direction: if (digitalRead(switchPin) == HIGH) { digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high } // if the switch is low, motor will turn in the opposite direction: else { digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D high digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low } }