Bench NotesUno handbook

Custom Servo Angle Sequence

A button steps a servo through a list of angles stored in an array: 90, 45, 135, 0, 180.

BeginnerServoPushbutton

Overview

The servo starts at 90°. Each press moves it to the next angle in a list: 45°, 135°, 0°, 180°, then back to 90°. Changing the list in the code changes the sequence without touching the logic.

You will need

  • Arduino Uno R3 and USB cable
  • Momentary pushbutton
  • Hobby servo such as an SG90
  • Jumper wires

Wiring

Uno pinConnects toNotes
13Pushbutton, one sideInternal pull-up keeps the pin HIGH. Pressed reads LOW (active-low).
GNDPushbutton, other sideOn the Uno R3 the GND socket sits right next to pin 13.
10Servo signal wire (orange or yellow)Servo library pulse output.
5VServo V+ (red)Only for one small, lightly loaded micro servo. Otherwise use a separate supply; see the power note.
GNDServo GND (brown or black)Must be shared with any external servo supply.
Wiring for Custom Servo Angle SequenceArduino Uno D13 (INPUT_PULLUP) to Pushbutton leg 1; Arduino Uno GND to Pushbutton leg 2; Arduino Uno D10 to Servo signal (orange/yellow); Arduino Uno 5V to Servo V+ (red); Arduino Uno GND to Servo GND (brown/black).Connections (each line is one wire)Arduino UnoPushbuttonServoD13 (INPUT_PULLUP)leg 1GNDleg 2D10signal (orange/yellow)5VV+ (red)GNDGND (brown/black)
Original wiring diagram. Each line is one jumper wire; boxes on a line are series resistors.

How the pins are arranged

Read before wiring

The pushbutton goes between pin 13 and the GND socket beside it. INPUT_PULLUP turns on the ATmega328P's internal pull-up resistor (roughly 20–50 kΩ), so pin 13 reads HIGH while the button is released and LOW while it is pressed. The logic is active-low: the code checks for LOW to mean "pressed".

Pin 13 also drives the on-board "L" LED. On the Uno R3 and later that LED is driven through a buffer, so it does not disturb the input, but the LED may glow faintly while the pull-up is active. Very old boards with the LED wired straight to pin 13 could not use it reliably as a pulled-up input; if you have one, move the button to pin 2 and change the 13s in the sketch.

A single micro servo such as an SG90, moving freely with nothing attached to its horn, can usually run from the Uno's 5V pin while the board is on USB. Any bigger servo, more than one servo, or a servo pushing against a load can pull several hundred milliamps in bursts. That causes the board to reset, the USB connection to drop, or the servo to jitter. In those cases power the servo from a separate regulated 5–6 V supply and connect that supply's ground to the Uno's GND. See Powering servos safely.

Diagrams

Header placementA pushbutton between GND and pin 13; the internal pull-up keeps 13 HIGH until pressed. The servo signal wire goes to pin 10.Arduino Uno R3 — parts plugged straight into the headerGND13121110A0A1A2A3A4Pushbutton between GND and 13Servo signal wire to pin 10
Button between GND and 13; servo signal on 10.
Sequence orderSemicircle from 0 to 180 degrees showing servo arm positions in order: 90 degrees, 45 degrees, 135 degrees, 0 degrees, 180 degrees. 0°90°180°12345Order1. 90°2. 45°3. 135°4. 0°5. 180°
Positions in the order the button visits them.

The sketch

button_servo_custom_sequence.ino
#include <Servo.h>
Servo servoMotor;

int angles[5] = {90, 45, 135, 0, 180};
int angleIndex = 0;

void setup() {
  servoMotor.attach(10);
  pinMode(13, INPUT_PULLUP);
  servoMotor.write(angles[angleIndex]);
}

void loop() {
  if (digitalRead(13) == LOW) {
    delay(50);
    if (digitalRead(13) == LOW) {
      angleIndex = angleIndex + 1;
      if (angleIndex >= 5) {
        angleIndex = 0;
      }
      servoMotor.write(angles[angleIndex]);
      while (digitalRead(13) == LOW) {
        delay(10);
      }
      delay(50);
    }
  }
}

How the code works

The index wraps with if (angleIndex >= 5), which keeps it inside the array's valid range of 0–4. If you add or remove angles, change the array size and that 5 together, or compute the length with sizeof(angles) / sizeof(angles[0]) (see Arrays).

Large jumps such as 135° to 0° make the servo move at full speed and draw the most current. If the board resets on those moves, the servo needs its own supply.

Review result: no changes were needed.

What you should observe

  • On power-up the servo goes to 90°.
  • Presses visit 45°, 135°, 0°, 180°, then 90° again.
  • One press equals one move, even if held.