ELEGOO MEGA · LESSON 5
Digital Inputs
Use one button to turn an LED on and the other to turn it off.
Read active-low buttons with internal pull-ups.
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.
- 1 ×Red LEDA red indicator LED converts forward current into visible light; use it for status indication with a series resistor. Red two-lead dome; longer lead normally anode, flat edge normally cathode. Lead length may have been trimmed; confirm polarity with diode test.
- 220 Ω resistor provides a fixed opposition to current. LED current limiting 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.
- A small tactile pushbutton closes a momentary contact while pressed to request a digital action. Small square four-legged tactile switch. Confirm paired contacts by continuity; the switch may bridge two legs even before pressing.
- 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
- Unplug USB; place each four-leg switch across the breadboard center gap.
- Use one terminal from each distinct switch contact pair; check the diagram rather than connecting two already-common legs.
- Fit the LED and resistor, then reconnect USB.

Reviewed pin-to-pin connections
| From | To | Purpose / notes |
|---|---|---|
| Mega: D5 | Series resistor220Ω: input | |
| Series resistor220Ω: output | Red LED: anode (+) | |
| Red LED: cathode (-) | Mega: GND | |
| Mega: D9 / ON | Button A: contact group1 | |
| Button A: contact group2 | Mega: GND | |
| Mega: D8 / OFF | Button B: contact group1 | |
| Button B: contact group2 | Mega: GND |
Code
PlatformIO configuration
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
#include <Arduino.h>
// These named constants identify the LED output and the two button inputs.
const uint8_t LED_PIN = 5;
const uint8_t ON_BUTTON = 9;
const uint8_t OFF_BUTTON = 8;
// Configure the LED as OUTPUT and the buttons as INPUT_PULLUP.
void setup() {
pinMode(LED_PIN, OUTPUT);
// INPUT_PULLUP uses an internal resistor: pressed = LOW, released = HIGH.
pinMode(ON_BUTTON, INPUT_PULLUP);
pinMode(OFF_BUTTON, INPUT_PULLUP);
}
// Read each button repeatedly. The LED remembers the last button command.
void loop() {
// If the ON button is pressed, switch the LED on.
if (digitalRead(ON_BUTTON) == LOW) digitalWrite(LED_PIN, HIGH);
// If OFF is pressed, switch it off. This second if runs independently.
if (digitalRead(OFF_BUTTON) == LOW) digitalWrite(LED_PIN, LOW);
}
Expected behavior
Pressing Button A turns the LED on; Button B turns it off. It retains that state after release; if both are held, OFF wins.
Experiments & Challenges
- Swap the ON and OFF button assignments in code.
Troubleshooting
- LED always on/off: a button may connect two permanently common legs instead of the two switch contact groups.