How To Control DC Motors With Arduino

Required Hardware

For users who do not have Hackerspace Karachi Arduino Kit v1.0

  • Arduino Board
  • DC motors
  • L293 motor driver
  • Hook-up Wires

Circuit

For users who do not have Hackerspace Karachi Arduino KiT v1.

Schematic

Code

                    

1. /*
2. Controlling DC motors with Arduino
3.
4. This example shows how to control DC motors using Arduino.
5.
6. The circuit:
7. – Motor 1 and 2
8. connected to output pins of driver IC
9.
10. -Driver IC
11. input and enable pins connected to digital pins of Arduino
12.
13. http://hackerspacekarachi.org/
14. */
15. int motor1pin1 = 8; //input pin of Driver ic for 1st motor connected to digital pin 8
16. int motor1pin2 = 9; //input pin of Driver ic for 1st motor connected to digital pin 9
17. int enA = 5; //enable pin of Driver ic for 1st motor connected to digital pin 5
18.
19.
20. int motor2pin1 = 10; //input pin of Driver ic for 2nd motor connected to digital pin 10
21. int motor2pin2 = 11;//input pin of Driver ic for 2nd motor connected to digital pin 11
22. int enB = 6; //enable pin of Driver ic for 2nd motor connected to digital pin 6
23.
24.
25. void setup() {
26. pinMode(motor1pin1, OUTPUT); //setting prins to be used as output
27. pinMode(motor1pin2, OUTPUT);
28. pinMode(motor2pin1, OUTPUT);
29. pinMode(motor2pin2, OUTPUT);
30. pinMode(enA, OUTPUT);
31. pinMode(enB, OUTPUT);
32. }
33.
34. void loop() {

35. digitalWrite(motor1pin1, HIGH); //for motor 1 to move in one direction
36. digitalWrite(motor1pin2, LOW);
37. digitalWrite(enA, HIGH);
38.
39. digitalWrite(motor2pin1, HIGH); //for motor 2 to move in one direction
40. digitalWrite(motor2pin2, LOW);
41. digitalWrite(enB, HIGH);
42. delay(1000);
43.
44. }

1. /*