Level 2Unit B · Session 7
7

Unit B · Things that move, things that show

An angle you can command

What we are making: a servo that sweeps to three positions and holds each one. Your first moving part, and it works in about ten minutes.

The idea

An ordinary motor spins. You can tell it to go, and you can tell it to stop, but you cannot tell it where to be. A servo is different. You give it an angle — a number between 0 and 180 — and it turns to that angle and stays there, pushing back if you try to move it.

That is why servos are in robot arms, in steering, in anything that has to arrive somewhere exact. You will use one in your project, whatever your project turns out to be.

New words
ServoA motor that goes to an angle you name, and holds it.
DegreesThe angle. 0 is one end, 180 is the other, 90 is the middle.
LibrarySomeone else's code that you borrow. #include is how you borrow it.
attach()Telling the library which pin your servo is plugged into.
Arduino UnoUSB to the laptop5VGND~9SG90 servothe three-wire plugRedBrownOrange5VGNDsignalOrange is always the signal wire. Red and brown are power.
The servo. Three wires, and the only one that carries your instruction is the orange one.
Sweep - your first servo program
1#include <Servo.h>
2 
3Servo arm; // the servo we are controlling
4 
5void setup() {
6 arm.attach(9); // the servo's orange wire goes to pin 9
7}
8 
9void loop() {
10 arm.write(0); // go to 0 degrees
11 delay(1000); // wait one second
12 arm.write(90); // go to 90 degrees
13 delay(1000);
14 arm.write(180); // go all the way over
15 delay(1000);
16}

Do it

  1. Plug the three wires in as the picture shows. Orange to pin 9.
  2. Open the Arduino IDE. Type the program, or open the one your teacher gives you.
  3. Upload it. The servo should move to three positions, over and over.
  4. Put your finger lightly against the horn while it holds 90. Feel it push back.

Change it

  1. Change one of the write() numbers to 45. Upload. Watch what changes.
  2. Change a delay(1000) to delay(300). Now it hurries.
  3. Add a fourth position of your own choosing, with its own delay.
💡 Tip
Do not force the horn round with your fingers. A servo held still is a servo fighting you, and the plastic gears inside are the part that gives up first. If it is buzzing and not moving, unplug it before you investigate.
What you seeWhyWhat to do
Nothing moves at allThe signal wire is on the wrong pin.Move the orange wire to pin 9. The commonest mistake in this session.
It jitters instead of movingNot enough current to go round.Unplug anything else drawing power and upload again.
It moves once, then stopsThe code is in setup(), not loop().Only loop() repeats. Move the four lines down into it.
‘Servo’ does not name a typeThe library was not included.Check the #include <Servo.h> line is there and spelled right.

What you learned

  • A servo takes an angle, not a speed. That is the whole difference.
  • #include borrows somebody else's work so you do not have to write it.
  • delay() is how you give a mechanical thing time to arrive.
Challenge optional — only if you finish early
  • Make the servo sweep slowly and quickly in turn.
  • Find the smallest angle change your servo can actually manage.
6. From an empty file