Unit B · Things that move, things that show
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.
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.
write() numbers to 45. Upload. Watch what changes.delay(1000) to delay(300). Now it hurries.| What you see | Why | What to do |
|---|---|---|
| Nothing moves at all | The signal wire is on the wrong pin. | Move the orange wire to pin 9. The commonest mistake in this session. |
| It jitters instead of moving | Not enough current to go round. | Unplug anything else drawing power and upload again. |
| It moves once, then stops | The code is in setup(), not loop(). | Only loop() repeats. Move the four lines down into it. |
| ‘Servo’ does not name a type | The library was not included. | Check the #include <Servo.h> line is there and spelled right. |
#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.In this session