Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

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.
  • A 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

  1. Unplug USB; place each four-leg switch across the breadboard center gap.
  2. Use one terminal from each distinct switch contact pair; check the diagram rather than connecting two already-common legs.
  3. Fit the LED and resistor, then reconnect USB.
Illustration © ELEGOO. All rights reserved.
Illustration © ELEGOO. All rights reserved. Open full-size ↗
Reviewed pin-to-pin connections
FromToPurpose / notes
Mega: D5Series resistor220Ω: input
Series resistor220Ω: outputRed LED: anode (+)
Red LED: cathode (-)Mega: GND
Mega: D9 / ONButton A: contact group1
Button A: contact group2Mega: GND
Mega: D8 / OFFButton B: contact group1
Button B: contact group2Mega: GND
Open vector connection diagram ↗

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
main.cpp
#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.