Arduino Mega Servo Simulator: Wiring, Code and a Live Circuit
This is an SG90 hobby servo wired to an Arduino Mega 2560, sweeping from 0 to 180 degrees and back, live in your browser. The horn on the drawing turns as the sketch writes each angle.
Three wires: brown to GND, red to 5V and orange to pin 9. The sketch is the stock Servo library sweep.
- 5 V logic
- Library: Servo.h
- Signal on pin 9
- Powered from 5V
Wiring the Servo to the Arduino Mega
Every connection in the circuit above, with the board’s own pin names.
| Servo pin | Arduino Mega pin | Why |
|---|---|---|
| Brown (GND) | GND | The signal is measured against this, so it must be shared with the board. |
| Red (V+) | 5V | An SG90 runs from 4.8 to 6 V. |
| Orange (signal) | pin 9 | Any digital pin works with the Servo library. |
The Arduino Mega’s 5V pin can run one unloaded SG90. Under load a servo draws spikes of several hundred milliamps that reset the board, so a real project gives it its own 5 V supply with the grounds joined.
Pins to leave alone on the Arduino Mega
Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.
On a real bench
The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The Servo uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#include <Servo.h>
const int SERVO_PIN = 9; // signal, pin 9
Servo servo;
void setup() {
Serial.begin(9600);
servo.attach(SERVO_PIN); // 544 to 2400 us, 50 times a second
Serial.println("Sweeping");
}
void loop() {
for (int angle = 0; angle <= 180; angle += 2) {
servo.write(angle);
delay(15);
}
Serial.println("at 180");
for (int angle = 180; angle >= 0; angle -= 2) {
servo.write(angle);
delay(15);
}
Serial.println("back at 0");
}
Servo.h is the stock Arduino library, and it compiles here as written. attach() takes any pin on the Arduino Mega.
servo.attach(SERVO_PIN) starts a pulse every 20 ms. write(angle) sets its width: 544 microseconds for 0 degrees, 1472 for 90 and 2400 for 180, the library’s defaults and the simulated servo’s too.
The loops step two degrees every 15 ms, about 1.4 seconds for a sweep, which an SG90 can follow; it needs roughly 0.1 s per 60 degrees. Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.
Common mistakes with the Servo on the Arduino Mega
Running a loaded servo from the 5V pin
One servo moving a light arm is fine. Two, or one lifting something, pull the 5 V rail down far enough to reset the board, which looks like a sketch that restarts at random. Give servos their own supply and join the grounds.
Forgetting the common ground
With the servo on a separate supply and only the signal wire to the board, the servo has nothing to measure the pulse against and twitches at random. Connect the supply’s ground to the board’s GND.
Losing PWM on pins 9 and 10
On the ATmega2560 the Servo library takes Timer1, so analogWrite() on pins 11 and 12 stops working once a servo is attached. Move the LED you were dimming to another PWM pin.
Questions and answers
Can I simulate a servo with an Arduino Mega 2560 online?
Yes. The circuit at the top of this page is an Arduino Mega 2560 with a servo, running in your browser on a simulated ATmega2560. The simulated servo reads the real pulse width the board makes, so a wrong angle in the code is a wrong angle on the drawing. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Which Arduino Mega pins can drive a servo?
With the Servo library, any digital pin, the analog pins included. It does not need a pin marked with a tilde.
Why does my servo only turn about 160 degrees?
Many SG90s reach their end stops before 544 or 2400 microseconds. Find your servo’s real limits and pass them to attach(pin, min, max), for example attach(pin, 600, 2300).
How do I control a continuous rotation servo?
Same wiring and the same library: write(90) stops it, smaller angles turn it one way and larger ones the other, faster the further from 90. In the editor, set the servo’s mode property to continuous.
Build your own Servo project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.