ELEGOO MEGA · LESSON 9
Servo
Move an unloaded SG90 servo through 90°, 30°, 90° and 150°.
Command servo positions while supplying motor power separately.
Parts List
- ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
- A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
- An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
- An SG90-class micro servo uses internal feedback to move a lightweight horn toward a commanded position. Small blue micro-servo case with mounting ears, splined shaft, screw-on horns and a three-wire lead. Check label and behavior because continuous-rotation variants can share this form.
- Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.
Assembly
- Disconnect USB before wiring. Leave the single servo unloaded with room for its horn to move.
- Connect the servo orange signal lead to Mega D9, red power lead to Mega 5V, and brown ground lead to Mega GND, following the original ELEGOO illustration.
- Reconnect USB and upload the lesson program. A separate supply is optional for this unloaded exercise; the second diagram shows that alternative.
Use the original ELEGOO wiring for the single, unloaded servo exercise. The separate-supply diagram is an optional alternative, not an extra required part.

Reviewed pin-to-pin connections
| From | To | Purpose / notes |
|---|---|---|
| Mega: D9 | SG90: orange signal | Servo control |
| Mega: 5V | SG90: red power | Power for the single unloaded servo |
| Mega: GND | SG90: brown ground | Common ground |
Code
Download complete PlatformIO project
ZIP includes platformio.ini, src/main.cpp, and bundled libraries (Servo).
Download the ZIP: the Servo library is required
Copying main.cpp alone is not enough for this lesson. The program needs the Servo library, which supplies Servo.h and the code that controls the servo. Download the complete PlatformIO project ZIP above before you build.
Use the complete downloaded project (recommended)
- Download lesson-09.zip and extract the entire ZIP first. Do not open or copy just main.cpp.
- In VS Code, open the extracted workspaces/lesson-09 folder: the one containing platformio.ini and src/main.cpp.
- Keep third-party/Servo in the extracted package alongside workspaces, two levels above lesson-09. The included platformio.ini already points to this bundled library; you do not need to move it into lib.
- Choose the mega environment in PlatformIO and run Build. Once the build succeeds and the wiring is checked, use Upload.
extracted ZIP/
workspaces/
lesson-09/ <- open this folder in VS Code
platformio.ini
src/main.cpp
third-party/
Servo/
library.json
src/Servo.hAlready using your own Mega2560 PlatformIO project?
- Download and extract the same ZIP. Copy its entire third-party/Servo folder into your project's lib folder, as shown in the Servo library location image in Assembly.
- The result must be lib/Servo/src/Servo.h, with library.json and the other Servo files kept together. Do not copy only Servo.h, and do not put the library inside your project's src folder.
- In your own project's platformio.ini, declare the local dependency as shown below. If you copied the ZIP's configuration, replace its file://../../third-party/Servo entry with file://lib/Servo for this layout.
- Put the lesson program in your project's src/main.cpp, save the files, and run PlatformIO Build before Upload. The pictured firstlab folder is an example project name; use your own project folder.
your-project/
platformio.ini
lib/
Servo/
library.json
library.properties
LICENSE
src/Servo.h
src/
main.cppLocal library entry in your existing [env:mega] configuration:
lib_deps =
file://lib/ServoPlatformIO configuration
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
lib_deps =
file://../../third-party/Servo
#include <Arduino.h>
// Servo.h provides the Servo class and its attach() and write() methods.
#include <Servo.h>
// Create a Servo object named servo so we can control one motor.
Servo servo;
// Set the signal pin and command the initial center position.
void setup() {
// Attach the servo control signal to Mega digital pin 9.
servo.attach(9);
// A value near 90 requests the center angle; actual travel varies by servo.
servo.write(90);
}
// Cycle through four target angles, pausing at each one.
void loop() {
// Move back to the nominal center.
servo.write(90);
delay(500);
// Move toward one side; do not force the servo horn.
servo.write(30);
delay(500);
servo.write(90);
delay(500);
// Move toward the other side. Power the servo from the reviewed external supply.
servo.write(150);
delay(500);
}
Expected behavior
The unloaded horn moves90°→30°→90°→150°, pausing0.5 s at each command; actual travel varies by servo.
Experiments & Challenges
- Increase the delays to observe each position clearly.
Troubleshooting
- If the servo does not move, check D9, the three servo leads and the required Servo library. If it jitters or the board resets, use the optional separate supply and shared ground.