How to wire a servo to an Arduino Mega
To wire a servo to an Arduino Mega, connect GND to GND, VCC to 5V and PWM to pin 9. The Arduino Mega is a 5 V board, so the servo’s power and signal are both 5 V and nothing needs shifting. The sketch below uses the Servo (Servo.h) library. It sweeps the horn from 0 to 180 degrees and back, printing a line at each end.
- 5 V logic
- Library: Servo.h
- 3 wires
Servo to Arduino Mega wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| Servo pin | Arduino Mega pin | Wire color | Why |
|---|---|---|---|
| Brown (GND) | GND | blue | The signal is measured against this, so it must be shared with the board. |
| Red (V+) | 5V | red | An SG90 runs from 4.8 to 6 V. |
| Orange (signal) | pin 9 | amber | Any digital pin works with the Servo library. |
Power and logic levels for servo and Arduino Mega
Logic level
The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.
Supply
The servo takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.
This pair
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
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.
The working code for the Arduino Mega
Library: Servo (Servo.h). This is the sketch the circuit above runs.
#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");
}
What you should see
The servo horn turns from 0 to 180 degrees in about 1.4 seconds and then back. The serial monitor, at 9600 baud, prints Sweeping once, then at 180 at the top of each sweep and back at 0 at the bottom.
A servo that buzzes or does not move is almost always a missing common ground or a supply that sags under load.
The serial monitor prints this, recorded from the circuit above running for two and a half seconds:
Sweeping
at 180Common mistakes wiring a servo to an 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.
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.