ELEGOO MEGA · LESSON 31
Stepper Motor
Turn a geared stepper about one revolution in each direction.
Sequence a five-wire unipolar stepper through its ULN2003 driver.
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.
- A 28BYJ-48 is a small geared unipolar stepper that advances its output shaft when coil phases are energized in sequence. Small round motor can with mounting tabs, offset geared shaft and a five-wire plug on typical unipolar versions. Read its voltage label; 5 V and other winding variants exist.
- The ULN2003 driver board lets low-power logic switch unipolar stepper coils by sinking their current through a Darlington transistor array. ULN2003-marked IC on a PCB with motor socket, input pins and often phase LEDs. Verify motor socket wiring and the board power/ground labels.
- A breadboard power module distributes regulated low-voltage rails to compatible breadboard power strips. Board spanning breadboard rails with power socket, selector jumpers and pin contacts. Check rail polarity against the breadboard printed marks.
- A source-listed 9 V, 1 A DC adapter supplies a compatible device’s power input; it is not a 5 V logic rail. Plug-in adapter with DC barrel cable and 9 V/1 A label. Read polarity symbol and barrel dimensions.
- Male-to-female jumper leads make removable point-to-point connections for low-current prototype signals. Pin at one end and socket at the other. Check intact insulation and snug contacts before use.
- 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.
- The AstroAI meter measures electrical quantities through selectable probe circuits; its exact model, fuse arrangement and safety category remain unverified. AstroAI-branded handheld display with rotary selector and COM/voltage/current sockets. Read the actual model number and socket legends before choosing its manual.
Assembly
- Disconnect all power and plug the matching 5 V five-wire motor into the driver’s keyed socket.
- Set the breadboard supply rail to 5 V; verify it before connecting the driver. Use the 9 V adapter at its DC input and leave its USB socket unused.
- Keep motor +5 V separate from Mega 5 V; join their grounds. Connect USB, then motor power.

Source-reviewed wiring. Hardware not bench-tested; firmware uploads have not been performed.
Code
Download complete PlatformIO project
ZIP includes platformio.ini, src/main.cpp, and bundled libraries (Stepper).
Extract the ZIP, then open its lesson-31-project/workspaces/lesson-31 folder in VS Code. Keep the adjacent third-party folder in place.
PlatformIO configuration
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
lib_deps =
file://../../third-party/Stepper
Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.
#include <Arduino.h>
#include <Stepper.h> // Schedules the coil sequence through a separate ULN2003 driver.
const int STEPS_PER_REVOLUTION = 2048; // Nominal geared-output full steps; actual gearing varies.
// Constructor pin order 8,10,9,11 gives IN1,IN3,IN2,IN4 sequencing for this motor.
Stepper motor(STEPS_PER_REVOLUTION, 8, 10, 9, 11);
void setup() {
motor.setSpeed(15); // Here 2048 steps define a nominal output revolution, so this requests 15 RPM.
Serial.begin(9600);
}
void loop() {
Serial.println("clockwise"); motor.step(STEPS_PER_REVOLUTION); delay(500);
Serial.println("counterclockwise"); motor.step(-STEPS_PER_REVOLUTION); delay(500);
// step blocks until all commanded steps finish and leaves coils energized during the pauses.
}
Source: SOURCE_RECONCILED. Compile: COMPILE_VERIFIED. Hardware: NOT_TESTED. Upload: NOT_PERFORMED.
Expected behavior
The monitor prints clockwise/counterclockwise and the unloaded shaft turns roughly one output revolution each way, with 500 ms holds. 2048 is nominal; gearbox ratio and missed steps affect angle.
Experiments & Challenges
- Lower setSpeed(15) and compare motion without changing the constructor’s 2048-step scale.
Troubleshooting
- Buzzing without rotation: check physical IN1=D8, IN2=D9, IN3=D10, IN4=D11 and constructor order 8,10,9,11.