Materials needed for today's activity.
Trying out the Servo
Step 1) Wire up your servo like the picture.
You could put the servo's last cord in any pin, just make sure you change that pin number in myservo.attach(#), where # is what you plugged in. In the example picture, it is plugged into 9. |
Step 2) Type in the code below.
* // are just comments. You do not need to type the // or what comes after them. They are just there to tell you what that line of code does.
* // are just comments. You do not need to type the // or what comes after them. They are just there to tell you what that line of code does.
Tip: The # symbol is made by pushing shift and 3 at the same time
The < > symbols are next to the m key on your keyboard.
(shift and the , key or hold shift and the . key)
Step 3) Validate the code using the check box button the upper left.
Step 4) Did you get an error? If so go through the checkpoints from yesterday. If you get no error, then you can move on.
Step 5) Plug in your board. Upload the code using the right arrow symbol (top left)
*remember that "problem uploading to board" means you have to change your port. To do that go to: tools, port, and select Arduino
Step 6) Is it working? If so, that's cool! Save your work!
*If it is not working, a common thing is that your wires have some glue stuck on the metal ends. Use your finger nails to rub any glue off of the wires you are using.
Once you get it to work, feel free to play with some of the numbers on the code and
re-upload it to the board to see how those numbers effected the servo.
The < > symbols are next to the m key on your keyboard.
(shift and the , key or hold shift and the . key)
Step 3) Validate the code using the check box button the upper left.
Step 4) Did you get an error? If so go through the checkpoints from yesterday. If you get no error, then you can move on.
- Check CaPiTaL letters
- You may have forgotten a ;
- You may have forgotten a parenthesis ( or )
- Did you type the curly brackets? { }
Step 5) Plug in your board. Upload the code using the right arrow symbol (top left)
*remember that "problem uploading to board" means you have to change your port. To do that go to: tools, port, and select Arduino
Step 6) Is it working? If so, that's cool! Save your work!
*If it is not working, a common thing is that your wires have some glue stuck on the metal ends. Use your finger nails to rub any glue off of the wires you are using.
Once you get it to work, feel free to play with some of the numbers on the code and
re-upload it to the board to see how those numbers effected the servo.
How to control more than one Servo.
How to add another servo to your code:
1) At the top of your code, just below “Servo myservo”, type in Servo myservo2;
*The “2” tells your code that you have a second servo now
2) In the void setup section, type the following below the first attach:
myservo2.attach(10); *this attaches the 2nd servo to pin 10
3) In the void loop section, type the following below myservo:
myservo2.write(pos); *this tells the 2nd servo to move to the position
You can follow these steps again to add as many servos as you want later if you have time.
Here is the code for what your 2 servo control code might look like.
1) At the top of your code, just below “Servo myservo”, type in Servo myservo2;
*The “2” tells your code that you have a second servo now
2) In the void setup section, type the following below the first attach:
myservo2.attach(10); *this attaches the 2nd servo to pin 10
3) In the void loop section, type the following below myservo:
myservo2.write(pos); *this tells the 2nd servo to move to the position
You can follow these steps again to add as many servos as you want later if you have time.
Here is the code for what your 2 servo control code might look like.

If you want your second servo to go to different positions than your first, you will need to declare a different number than pos and use that instead. Directions below
Example: Try changing 180 to 90 instead. What does that do?
WARNING: Servos usually do not go more than 180 degrees or 1/2 a circle. If you put in more than 180, you may break your servo.
WARNING: Servos usually do not go more than 180 degrees or 1/2 a circle. If you put in more than 180, you may break your servo.
Controlling servos with Input
What if we want a person to be able to change where the servo goes with a physical input, we need to add something that is called an "input" (inputs you control). A potentiometer is good for this.
Here is a picture of one. Turning it changes its output value. We can tell the computer how to use this data. |
Just to clarify, our potentimeter is fatter than the wiring picture shows. Ours skips a hole between the pins.
Step 4) Did you get an error? If so go through these checkpoints:
1) Are all the colored parts in this picture colored in your code?
2) Check CaPiTaL letters.
3) You may have forgotten a ;
4) You may have forgotten a parenthesis ( or )
5) Did you type the curly brackets? { }
If you get no error, then you can move on.
Step 5) Upload the code using the right arrow symbol in the upper left.
Step 6) Is it working? Move the knob back and forward to see if the knob moves the servo.
If so, that's cool. Make it do a little dance and save your work!
1) Are all the colored parts in this picture colored in your code?
2) Check CaPiTaL letters.
3) You may have forgotten a ;
4) You may have forgotten a parenthesis ( or )
5) Did you type the curly brackets? { }
If you get no error, then you can move on.
Step 5) Upload the code using the right arrow symbol in the upper left.
Step 6) Is it working? Move the knob back and forward to see if the knob moves the servo.
If so, that's cool. Make it do a little dance and save your work!