How To Push Button With Servo Motor

Required Hardware

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

  • Arduino Board
  • Breadboard
  • Push-button
  • Servo motor
  • Resistor 220 ohm
  • Hook-up Wires

Circuit

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

Schematic

Code

                    

1. /*
2. Controlling Push Button with Servo motor
3.
4. This example shows how to use Push Button with Servo motor
5. The circuit:
6. -Servo
7. signal pin connected to digital pin 3.
8. Cathode connected to Ground.
9. anode connected to Vcc.
10. – Push Button
11. Anode connected to VCC.
12. Cathode connected to digital pin 2.
13. Cathode connected to GND through 220 resistor.
14. http://hackerspacekarachi.org/
15. */
16.
17. #include <Servo.h>
18. int position;
19. Servo servo_motor;
20. void setup()
21. {
22. servo_motor.attach(3); //connect servo to digital pin 3
23. pinMode(2, INPUT); // declaration of input
24. }
25. void loop()
26.
27. {
28. int d = digitalRead(2); // read and store input
29.
30. if (d == 1)
31. {
32. for (position = 0; position <= 180; position++) //to make servo move from 0 to 180
degrees
33. {
34. servo_motor.write(position);
35.
36. delay(10);
37.}
38.}
39.}

1. /*