How To Push Button With DC Motors

Required Hardware
For users who do not have Hackerspace Karachi Arduino Kit v1.0
- Arduino Board
- Breadboard
- Push-button
- DC motors
- Resistor 220 ohm
- Hook-up Wires
Circuit
For users who do not have Hackerspace Karachi Arduino Kit v1.0
Schematic
Code
1. /*
2. Push Button with DC motors
3. This example shows how to use Push Button with DC motors
4.
5. The circuit:
6. – Motor 1 and 2
7. connected to output pins of driver IC
8.
9. -Driver IC
10. input and enable pins connected to digital pins of Arduino
11.
12. – Push Button
13. Anode connected to VCC.
14. Cathode connected to digital pin 2.
15. Cathode connected to GND through 220 resistor.
16.
17.
18. http://hackerspacekarachi.org/
19. */
20. int motor1pin1 = 8; //input pin of Driver ic for 1st motor connected to digital pin 8
21. int motor1pin2 = 9; //input pin of Driver ic for 1st motor connected to digital pin 9
22. int enA = 5; //enable pin of Driver ic for 1st motor connected to digital pin 5
23.
24.
25. int motor2pin1 = 10; //input pin of Driver ic for 2nd motor connected to digital pin 10
26. int motor2pin2 = 11;//input pin of Driver ic for 2nd motor connected to digital pin 11
27. int enB = 6; //enable pin of Driver ic for 2nd motor connected to digital pin 6
28.
29.
30. void setup() {
31. pinMode(2, INPUT); // declaration of input
32. pinMode(motor1pin1, OUTPUT); //setting prins to be used as output
33. pinMode(motor1pin2, OUTPUT);
34. pinMode(motor2pin1, OUTPUT);
35. pinMode(motor2pin2, OUTPUT);
36. pinMode(enA, OUTPUT);
37. pinMode(enB, OUTPUT);
38. }
39.
40. void loop() {
41.
42. int d = digitalRead(2); // read and store input
43.
44. if (d == 1)
45. {
46. digitalWrite(motor1pin1, HIGH); //for motor 1 to move in one direction
47. digitalWrite(motor1pin2, LOW);
48. digitalWrite(enA, HIGH);
49.
50. digitalWrite(motor2pin1, HIGH); //for motor 2 to move in one direction
51. digitalWrite(motor2pin2, LOW);
52. digitalWrite(enB, HIGH);
53. delay(1000);
54. }
55. }