ELEGOO MEGA · LESSON 30
Relay
Switch a low-voltage DC motor on and off with a bare relay.
Separate a relay’s coil driver from its COM/NO load contacts.
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 bare 5 V relay uses a coil and mechanical contacts to switch a separate low-voltage load from a control signal. Box-shaped relay with pins underneath, no carrier PCB. Read the coil/contact markings and locate the contact diagram; do not infer every pin from cube shape.
- A PN2222 NPN transistor uses base drive to control a larger collector current for switching or amplification. TO-92 body marked PN2222; confirm pinout from the supplied part. TO-92 outlines do not establish emitter/base/collector order; read the exact manufacturer suffix or verify with a transistor tester.
- 330 Ω resistor provides a fixed opposition to current. Lower-current LED indication is a common use when its unverified power and tolerance limits fit the circuit. Small axial body with one wire at each end. Find the labeled value compartment and measure an isolated loose part with a meter; the shared inventory image is a form reference.
- 1N4007 is a general-purpose silicon rectifier that conducts mainly from anode to cathode for steering or rectification. Axial black diode with a cathode band and 1N4007 marking. The band marks cathode; diode-test mode verifies forward behavior but not maximum ratings.
- A small brushed DC motor spins a fan blade or light mechanical load under external drive. Small motor with a detachable plastic propeller; keep fingers and loose leads clear of the spinning blade.
- 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.
- ?1 ×Regulated 5.0 V current-limited DC supply with output leads, limit set to 0.35 AUnconfirmed · Availability and exact identity unconfirmed.
Assembly
- Disconnect all power and remove the fan blade. Identify the relay’s approximately 70 Ω coil pair and COM/NO/NC from its exact bottom-view pinout; do not force misaligned leads into connected breadboard strips.
- Build the PN2222 coil circuit and both diode clamps from the table; the original L293D bridge is not used.
- Set motor power to 5.0 V with a 0.35 A current limit; keep that + separate from Mega 5 V, sharing GND only.
- Connect USB, then motor power; open Monitor at 9600 baud.

Reviewed connections
| From | To | Note |
|---|---|---|
| Mega D5 → 330 Ω → PN2222 B pin 2 | PN2222 E pin 1 → GND; C pin 3 → coil − | Original D3/D4 and L293D are omitted |
| Mega 5 V → relay coil + | 1N4007 across coil, band to + | Do not drive the coil from D5 directly |
| External 5.0 V, limit 0.35 A → COM; NO → motor + | Motor − → external −; NC open | External − and Mega GND common; positive rails separate |
| Second 1N4007 across motor | Band to motor +; other end to motor − | Unidirectional motor only in this relay circuit |
Source-reviewed wiring. Hardware not bench-tested; firmware uploads have not been performed.
Code
PlatformIO configuration
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.
#include <Arduino.h>
// D5 now switches a PN2222 low-side coil driver. The manual's L293D coil bridge is replaced.
const uint8_t RELAY_PIN = 5;
void setup() {
digitalWrite(RELAY_PIN, LOW); // Keep the coil off when making the pin an output.
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
for (uint8_t i = 0; i < 5; ++i) {
Serial.println("spin");
digitalWrite(RELAY_PIN, HIGH); // Energize the coil: COM joins NO, powering the DC motor.
delay(2000);
Serial.println("stop");
digitalWrite(RELAY_PIN, LOW); // Release the coil: the normally-open contact opens.
delay(2000);
}
// The outer loop starts another five-cycle group, just like the manufacturer's example.
}
Source: SOURCE_RECONCILED. Compile: COMPILE_VERIFIED. Hardware: NOT_TESTED. Upload: NOT_PERFORMED.
Expected behavior
The relay clicks on for 2 s (“spin”) and releases for 2 s (“stop”), repeatedly. Motor power must come through NO so it stops when the coil releases; a click alone does not verify the contacts or motor.
Experiments & Challenges
- Move the disconnected load from NO to NC to compare normally-open and normally-closed behavior, then restore NO.
Troubleshooting
- Clicks without motion: verify COM/NO and motor supply; no release: check D5 goes LOW, transistor polarity and coil diode orientation.