How To Controlling Servo Motor With Arduino

Required Hardware

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

  • Arduino Board
  • Servo motor
  • Hook-up Wires

Circuit

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

Schematic

Code

                    

1. /*
2. Controlling servo motor with Arduino
3.
4. This example shows how to control servo motor using Ardunio.
5. The circuit:
6. -Servo
7. signal pin connected to digital pin 3.
8. Cathode connected to Ground.
9. anode connected to Vcc.
10.
11. http://hackerspacekarachi.org/
12. */
13.
14. #include <Servo.h>
15. int position;
16. Servo servo_motor;
17. void setup()
18. {
19. servo_motor.attach(3); //connect servo to digital pin 3
20. }
21. void loop()
22.
23. {
24. for (position = 0; position <= 180; position++) //to make servo move from 0 to 180
degrees
25. {
26. servo_motor.write(position);
27.
28. delay(10);
29. }
30. delay(1000);
31. for (position = 180; position >= 0; position–)//to make servo move from 180 to 0
degrees
32. {
33. servo_motor.write(position);
34. delay(10);
35. }
36. delay (1000);
37. }

1. /*