Summary of Using Servo Motors with Arduino
This instructable explains what a servo motor is and how to control it using an Arduino and a 555 timer IC. It details the basics of servo operation, including pulse width modulation for positional control. The author provides step-by-step instructions to test the servo with Arduino's example "sweep" code and then introduces using a potentiometer to manually control the servo angle via Arduino code. Connections and programming tips are clearly explained to help beginners get started with servo motor projects.
Parts used in the Servo Motor Control Project:
- Servo motor
- Arduino board
- Potentiometer (10k-100k)
- 555 timer IC (optional, for later steps)
- Solid core jumper wires
- 5V power supply (from Arduino)
Step 1: What is a Servo motor
Step 2: Testing the servo
the first thing that you should do is make sure your servo motor is working. because the servos wires end in a female header, you cannot plug it into the arduino (unless you have a shield. insert solid core wires into the headers, so you can attach it to the pins of the arduino (or anything else). When you downloaded your programming environment for arduino, it should have two examples for the servo. The one we are going to use first is called sweep. Go to the “open” icon next to save near the top of the window on the environment. click on it, and a list of files should come up. go down to the one that says servo, and put your mouse over it. two files should come out of it. one called “sweep” and one called “knob”. click on the one called sweep, compile the code and upload it to your board. if everything is connected correctly, the servo should begin to go back and forth from 0 to 180 degrees. If you cannot find the code, copy this:
// Sweep // by BARRAGAN <http://barraganstudio.com> // This example code is in the public domain. #include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
be sure to plug in the white wire to digital pin 9, the black wire to one of the ground pins on arduino, and the red wire to the 5v pin on the arduino board.
Step 3: New code
Now that you know your servo works, you can begin to incorporate sensors into the mix. the first thing you should do is use a potentiometer. Use anything between 10 and 100k. keep the servo attached as it was using the sweep example. attach the top pin on the pot to 3.3v on the arduino. Connect the bottom pin to ground on the board. Connect the center of wiper pin of the pot to A0 (the first analog pin) on arduino. go to “open” on the IDE again. Go to servo and open “Knob”. compile the code and upload it to your board. When the program is running you will be able to control the position of the servo with a potentiometer. If you cannot find the code, copy this:
// Controlling a servo position using a potentiometer (variable resistor) // by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }
For more detail: Using Servo Motors with Arduino