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 pin | Connects to | Notes |
|---|---|---|
| 13 | Pushbutton, one side | Internal pull-up keeps the pin HIGH. Pressed reads LOW (active-low). |
| GND | Pushbutton, other side | On the Uno R3 the GND socket sits right next to pin 13. |
| 10 | Servo signal wire (orange or yellow) | Servo library pulse output. |
| 5V | Servo V+ (red) | Only for one small, lightly loaded micro servo. Otherwise use a separate supply; see the power note. |
| GND | Servo GND (brown or black) | Must be shared with any external servo supply. |
How the pins are arranged
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
The sketch
#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.