An SG90 servo on Arduino Uno

Steps done: 0 of 5

~9UNOGND5VSG90ATMEGA328P-PU

Guess the answer and press Run

angle90°time, s0.0

Lesson steps

What will the rocker do?

Read the sketch and pick an answer before you run it.

What will the rocker do after you run it?

Sketch

servo_sweep.inoTo board
#include <Servo.h>

Servo servo;         // servo on D9

int minAngle = 0;    // from, degrees
int maxAngle = 180;  // to
int step = 1;        // step, degrees
int wait = 15;       // pause, ms

void setup() {
  servo.attach(9);
  Serial.begin(9600);
}

void loop() {
  for (int a = minAngle; a <= maxAngle; a += step) {
    servo.write(a);
    Serial.println(a);
    delay(wait);
  }
  for (int a = maxAngle; a >= minAngle; a -= step) {
    servo.write(a);
    Serial.println(a);
    delay(wait);
  }
}

Tap a line or an orange number

Serial monitor

Serial, 9600 baudboard time 0.0 s

About the lesson

The Uno sketch runs in an ATmega328P emulator on this page. The numbers in the code change on the fly, and the finished sketch is flashed to your own board. About 10 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.

Build it for real

Parts

  • Arduino Uno R3 or a compatible board
  • SG90 servo
  • 3 male-to-male jumper wires
  • USB-B cable for the board

Wires

FromTo
Brown SG90 wireGND
Red SG90 wire5V
Orange SG90 wireD9

For a test with no load, the servo is fine on the board 5V from USB. If the rocker twitches or the board resets, power the servo from a separate 5 V supply and connect the grounds.

The sketch below goes to the board with the default numbers: minAngle 0, maxAngle 180, step 1, wait 15. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.

Flash it to your own board

The whole sketch

#include <Servo.h>

Servo servo;         // servo on D9

int minAngle = 0;    // from, degrees
int maxAngle = 180;  // to
int step = 1;        // step, degrees
int wait = 15;       // pause, ms

void setup() {
  servo.attach(9);
  Serial.begin(9600);
}

void loop() {
  for (int a = minAngle; a <= maxAngle; a += step) {
    servo.write(a);
    Serial.println(a);
    delay(wait);
  }
  for (int a = maxAngle; a >= minAngle; a -= step) {
    servo.write(a);
    Serial.println(a);
    delay(wait);
  }
}

What to keep in mind on a real board

On the Uno the Servo library takes Timer1. While a servo is attached, analogWrite() on pins 9 and 10 does not work. You can move the servo signal to another digital pin, and then you give its number in attach().

Open the serial monitor in the Arduino IDE at 9600 baud, like in Serial.begin(). At another speed the monitor shows random characters.

The sketch takes 3510 bytes of flash and 237 bytes of RAM.

Licenses of the lesson code

The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.

The lesson firmware is built with the Arduino AVR core and the Servo library (LGPL 2.1), sources: ArduinoCore-avr, Servo. The lesson sketch and scene are made for this site.

MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.