Skip to content

Servo Sweep

You'll make a two-dollar hobby motor go to the angle you ask for, from a knob, from a sensor and from a saved sequence, and find out why it twitches.

About

A servo is the first thing that moves on command, and an SG90 costs about two dollars. It expects a pulse every 20 milliseconds, and the width of that pulse (roughly 1 to 2 ms) sets the angle. The library hides that, which is fine for tonight, but it helps to know the number is there. The trap is power. A servo pulls a few hundred milliamps the moment it starts moving, and if it shares the board's 5 V pin with everything else, the board resets and you spend an hour blaming your code.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
5
Takes
One evening
Skill level
Beginner (done it a few times)
Cost
Cheap (under $10)
Supplies
You need some
Where
At home
Other people
On your own

Requirements

  1. 1
    Wire one SG90 servo and sweep it from 0 to 180 degrees and back. Brown or black is ground, red is 5 V, and orange or yellow is the signal, which goes to a PWM pin. Load the Servo library's Sweep example first. If the servo buzzes at the ends, your 0 and 180 are past its real travel, so pull the limits in by 10 degrees. Film one full sweep. Video
  2. 2
    Turn a potentiometer into an angle. Read the pot on A0 and map 0 to 1023 onto 0 to 180. The horn will jitter when the knob sits still, because analogRead wanders by a count or two; average ten readings or ignore changes under 2 degrees and the jitter stops. Film the knob driving the horn. Video
  3. 3
    Point the servo at a sensor reading instead of a knob. Use a light sensor, a distance sensor or the room temperature, and make the servo a gauge: a paper arrow glued to the horn over a scale you drew. Take a photo of the gauge pointing at a value, with the sensor in frame. Photo
  4. 4
    Power the servo from a separate supply with a shared ground, and show the difference. Run the servo's red wire from a 4xAA pack or a separate 5 V supply, not the board's 5 V pin, and tie the two grounds together or nothing works. Before you change over, make the servo move fast on the board's 5 V and watch for a reset; the serial monitor restarting is the giveaway. Take a photo of the two-supply wiring with the ground link visible. Photo
  5. 5
    Record five positions with a button and play them back. Turn the knob to a position, press the button, and the current angle goes into an array. After five presses, a second button or a long press plays the sequence back with a pause between each. This is how every cheap robot arm gets taught. Film the playback. Video