Testing / Displays / 04
LCD Menu System
Use debounced input events and explicit page state instead of delay-heavy menu branches.
All 3 stages compiled 2026-10-19 (historical) · hardware review required
What you'll build
Log a debounced stable press from a button to ground.
First result after hardware review: One press produces one event; holding does not repeatedly fire.
What you need
- LCD1602 16×2 Character Display with I²C Backpack
- ELEGOO Mega 2560 R3 board
- 10 kΩ potentiometer
- Small tactile pushbutton
Check availability and exact module markings before starting.
Connections / current hardware gate
Before connecting power. Confirm the LCD rails, potentiometer and button contacts before power. The button uses a pull-up input and a reviewed connection to ground.
- Retain the reviewed LCD and potentiometer setup from the preceding projects.
- Identify the actual button contact pairs and compare them with the conditional map.
- Review the fail-closed electrical flag before any upload intended to operate hardware.
Start building
- 1
Observe one event
Log a debounced stable press from a button to ground.
- 2
Advance page state
Cycle Measurement, History and Status as a finite page domain.
- 3
Render from state
Update row content from current page and current measurement.
Download complete PlatformIO project
Each stage has its exact environment, complete source and expected result. Editor and tools setup →
Troubleshooting
Many page changes per press
Check that the event return is used, not the continuously true pressed level. Confirm state objects persist between loop calls.
No event
Verify the two switch terminals used are electrically different contact sides. Tactile buttons often have internally connected pairs.
Slow response
Remove blocking delays and repeated bus scans. Measure input polling cadence rather than assuming the loop runs instantly.
More detail
Foundations, safety and learning objectives
170 minutes · Editorial learning estimate excluding unresolved procurement/driver-port time
Three actual PlatformIO firmware environments; embedded compile and bench verification unestablished.
- Keep electrical review false until the exact wiring/power/logic checklist is completed.
- No power budget is inferred from successful compilation.
- Use debounced input events and explicit page state instead of delay-heavy menu branches.
- A mechanical switch can bounce between electrical states during one human action.
System diagram and electrical boundaries
Show the meaningful subsystem and signal/power boundary for LCD Menu System. Conceptual system view; physical connections remain conditional.
- ELEGOO Mega 2560 R3 boardClub-owned
- LCD1602 16×2 Character Display with I²C BackpackClub-owned
- Small tactile pushbuttonClub-owned
- 10 kΩ potentiometerClub-owned
Signals and interfaces
- LCD1602 16×2 Character Display with I²C BackpackI2C SDAELEGOO Mega 2560 R3 boardConditional reference
- LCD1602 16×2 Character Display with I²C BackpackI2C SCLELEGOO Mega 2560 R3 boardConditional reference
- 10 kΩ potentiometerADCELEGOO Mega 2560 R3 boardConditional reference
- Small tactile pushbuttonActive-low buttonELEGOO Mega 2560 R3 boardConditional reference
Power and common reference boundary
- display/peripheralSignal referencecontrollerConditional reference
- display/peripheralPeripheral powerverified supplyConditional reference
Interfaces: GPIO · I2C
Electrical boundaries and interpretation
- Use conditional wiring records; null pin/voltage means unresolved, not permission to choose a value.
- Draw power and logic paths separately.
- Do not turn a reference signal label into a guessed connector position.
- Keep member, club and campus access labels distinct.
- Include explicit port gate for unresolved driver/identity.
- Access scopes: CLUB_OWNED.
- Firmware source is not bench evidence.
Full hardware facts and conditional wiring reference
Access describes the source of a requirement. Check availability, the exact revision and the complete power/driver/harness system before starting. Inventory quantities are expected purchase/BOM quantities, not live stock.
product family verified · preflight required
Compatibility and unresolved details
- Display: 16 × 2 characters
- Interface: I²C backpack; exact expander revision not asserted
- ELEGOO Mega 2560 R3 boardClub-owned
product family verified · preflight required
- 10 kΩ potentiometerClub-owned
product family verified · preflight required
Compatibility and unresolved details
- Taper/wiper power: Unverified
- Small tactile pushbuttonClub-owned
product family verified · preflight required
Compatibility and unresolved details
- Contact/header current rating: Unverified; use as signal input
Electrical review is incomplete. Conditional references require confirmation; unresolved pins or rails are not wiring instructions. Power off before changing connections.
| From device / pin | To device / pin | Signal / voltage | Connection status and notes |
|---|---|---|---|
LCD1602 16×2 Character Display with I²C BackpackSDA | ELEGOO Mega 2560 R3 boardD20 / SDA | I2C SDA Voltage unresolved | Conditional reference Verify backpack IC, carrier supply and pull-up rail before enabling. |
LCD1602 16×2 Character Display with I²C BackpackSCL | ELEGOO Mega 2560 R3 boardD21 / SCL | I2C SCL Voltage unresolved | Conditional reference Mega mapping; not Uno A4/A5. |
10 kΩ potentiometerwiper | ELEGOO Mega 2560 R3 boardA0 | ADC 5 V | Conditional reference Identify terminals; outer ends to reviewed Mega reference supply and ground; nominal estimate is not calibration. |
Small tactile pushbuttoncontact side A | ELEGOO Mega 2560 R3 boardD2 | Active-low button 5 V | Conditional reference Other contact side to GND; INPUT_PULLUP. Avoid same-side terminal pairs. |
display/peripheralGND | controllerGND | Signal reference 0 V | Conditional reference Unpowered wiring check; use correct common-ground topology and keep high-current panel returns out of thin signal paths. |
display/peripheralVCC | verified supplyPin unresolved | Peripheral power Voltage unresolved | Conditional reference Supply voltage/current unknown at carrier level until reviewed; no assumed 5 V or 3.3 V instruction. |
All source files, supporting libraries and project download
Download complete project · workspaces/lcd-menu-system
Open the extracted folder containing platformio.ini and keep the complete supporting files.
duinoWitchery/hd44780@1.3.2
workspaces/lcd-menu-system/src/main.cpp
#include <Arduino.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>
#include <DisplayCore.h>
#include "board_profile.h"
hd44780_I2Cexp lcd;
bool ready=false;
uint32_t lastUpdate=0;
void row(uint8_t r,const char* text){lcd.setCursor(0,r);char padded[17];uint8_t i=0;for(;i<16&&text[i];++i)padded[i]=text[i];for(;i<16;++i)padded[i]=' ';padded[16]='\0';lcd.print(padded);}
#include "ui.h"
void setup(){Serial.begin(115200); if(!kElectricalReviewComplete){Serial.println("STOP: complete wiring/power review in guide before enabling hardware.");return;}Wire.begin();const int status=lcd.begin(16,2);if(status){Serial.print("LCD initialization error: ");Serial.println(status);return;}beginInput();ready=true;}
void loop(){if(!ready)return;uint32_t now=millis();const bool event=readPageEvent(now);
#if DISPLAY_STAGE == 1
if(event)Serial.println("One debounced press");
#elif DISPLAY_STAGE == 2
if(event)Serial.println((unsigned)currentPage());
#endif
if(!event&&!displaylab::elapsed(now,lastUpdate,250))return;lastUpdate=now;
const auto page=currentPage();row(0,page==displaylab::Page::measurement?"Measurement":page==displaylab::Page::history?"History":"Status");
char text[17];snprintf(text,sizeof(text),"A0:%4d",analogRead(A0));row(1,text);
}
Source integrity · SHA-256
b23c895650905d780924ad993a2e1378731c4e7d2a42fcb3bffe48c62a39d051Raw downloads and standalone ZIPs retain the canonical bytes and supporting files. Copy follows the selected Comments setting.
Original engineering guide
Original research wording is preserved. Use the stage-specific dated build evidence for later compile results; it does not establish physical behavior.
Use debounced input events and explicit page state instead of delay-heavy menu branches.
Status: FIRMWARE_AUTHORED_UNVERIFIED. FIRMWARE_SOURCE_AUTHORED; embedded build UNVERIFIED; hardware NOT_TESTED. Three actual PlatformIO firmware environments; embedded compile and bench verification unestablished.
What you are building
A mechanical switch can bounce between electrical states during one human action. INPUT_PULLUP establishes a released high state and a ground-connected pressed low state. The portable Button model tracks raw and stable values plus the most recent transition time. It emits a press only after the new value remains stable for 25 ms. That delay is an authored policy to test with this button, not a measured bounce duration for every switch.
Before you start
Estimated learning time: 170 minutes, excluding unresolved hardware/port work. Required prerequisites: display-lcd-instrument-panel. See PLATFORMIO_ORIENTATION.md and the hardware identity/access records. Current main inventory IDs and expected quantities were compared read-only at a406480e1e13807983f1c03b8a531c2dcd310ddb; expected quantities are not live stock. Member and campus access remain separate from club ownership.
Hardware and conditional wiring
| Component ID | Access scope | Expected snapshot quantity | Preflight |
|---|---|---|---|
| club:elegoo-mega-2560-compatible-board | CLUB_OWNED | 3 | PREFLIGHT_REQUIRED |
| club:lcd1602-with-i2c-backpack | CLUB_OWNED | 1 | PREFLIGHT_REQUIRED |
| club:pushbutton-small | CLUB_OWNED | 20 | PREFLIGHT_REQUIRED |
| club:potentiometer-10k | CLUB_OWNED | 2 | PREFLIGHT_REQUIRED |
Breadboard, appropriately rated jumpers/data cables and a computer are shared setup requirements. Review supply, signal levels and ground separately; power off before changing connections. Null pins/rails remain unresolved.
| From | To | Signal | Voltage / gate | Condition |
|---|---|---|---|---|
| club:lcd1602-with-i2c-backpack / SDA | club:elegoo-mega-2560-compatible-board / D20 / SDA | I2C SDA | None / CONDITIONAL_REFERENCE | Verify backpack IC, carrier supply and pull-up rail before enabling. |
| club:lcd1602-with-i2c-backpack / SCL | club:elegoo-mega-2560-compatible-board / D21 / SCL | I2C SCL | None / CONDITIONAL_REFERENCE | Mega mapping; not Uno A4/A5. |
| club:potentiometer-10k / wiper | club:elegoo-mega-2560-compatible-board / A0 | ADC | 5 / CONDITIONAL_REFERENCE | Identify terminals; outer ends to reviewed Mega reference supply and ground; nominal estimate is not calibration. |
| club:pushbutton-small / contact side A | club:elegoo-mega-2560-compatible-board / D2 | Active-low button | 5 / CONDITIONAL_REFERENCE | Other contact side to GND; INPUT_PULLUP. Avoid same-side terminal pairs. |
| display/peripheral / GND | controller / GND | Signal reference | 0 / CONDITIONAL_REFERENCE | Unpowered wiring check; use correct common-ground topology and keep high-current panel returns out of thin signal paths. |
| display/peripheral / VCC | verified supply / None | Peripheral power | None / CONDITIONAL_REFERENCE | Supply voltage/current unknown at carrier level until reviewed; no assumed 5 V or 3.3 V instruction. |
Software setup and dependencies
Open workspaces/lcd-menu-system in VS Code/PlatformIO. Configuration: workspaces/lcd-menu-system/platformio.ini. Platform: atmelavr@5.1.0. Board/framework: megaatmega2560 / arduino. Dependencies: duinoWitchery/hd44780@1.3.2. Dependency resolution occurs during build; an include is not an installation. See LIBRARY_MATRIX.md and COMPILATION_AUDIT.md for exact verification limits. The default electrical-review flag remains false. Review the matching pins, power and logic before changing it; a build cannot complete that physical review.
Build it: checkpoint path
| Stage | Goal | Environment | Expected / gate |
|---|---|---|---|
| Observe one event | Log a debounced stable press from a button to ground. | checkpoint1 | One press produces one event; holding does not repeatedly fire. |
| Advance page state | Cycle Measurement, History and Status as a finite page domain. | checkpoint2 | Three presses return to the first page. |
| Render from state | Update row content from current page and current measurement. | checkpoint3 | Page labels and the A0 count render independently of input polling. All checkpoints already render the page; stage1 adds press logs and stage2 logs the page index. |
Choose a stage deliberately. Project downloads include all authored environments; model-only project downloads include checkpoints1/2 and explicitly exclude the unfinished physical checkpoint. Stage3 for a port-gated project refuses download as finished firmware.
Understand it: software and physical model
ui.cpp owns the page and button state, while main reads a page query and renders. This is the first justified subsystem boundary: input handling has an independent contract. The scoped Page enum prevents treating arbitrary unrelated integers as page identities. A transition changes page once; a renderer can run many times without changing page. No nested menu loop waits for a button or stops measurement updates.
The input model starts released, so a button held during startup is detected as a newly stable press after the debounce interval. Decide if that is the product behavior you want. Page wrap is explicit modulo three. The checkpoint names represent growing understanding even though one canonical workspace contains complete source controlled by DISPLAY_STAGE. Open ui.h beside ui.cpp and identify declaration, definition and caller; including a header does not compile the implementation into the caller twice.
Source review precision: All three source environments already read A0 and render the selected page. Checkpoint1 additionally logs a stable press; checkpoint2 additionally logs the numeric page; checkpoint3 removes those diagnostic prints. The stages isolate reading tasks rather than pretending the earlier renderer is absent. A 10 kΩ potentiometer is required for a meaningful A0 input.
Canonical code reading map
| Real file | Source landmarks (1-based) | Lines |
|---|---|---|
| workspaces/lcd-menu-system/include/board_profile.h | Supporting declarations / portable helpers | 5 |
| workspaces/lcd-menu-system/include/ui.h | Supporting declarations / portable helpers | 5 |
| workspaces/lcd-menu-system/src/main.cpp | 12: void setup(){Serial.begin(115200); if(!kElectricalReviewComplete){Serial.println("STOP: complete wir; 14: void loop(){if(!ready)return;uint32_t now=millis();const bool event=readPageEvent(now); | 23 |
| workspaces/lcd-menu-system/src/ui.cpp | Supporting declarations / portable helpers | 6 |
| common-libs/DisplayCore/src/DisplayCore.h | 30: inline bool parsePacket(const char* text,Packet& result){; 40: bool accept(const Packet& p,uint32_t now){ | 60 |
Read initialization and failure guards first, then the selected DISPLAY_STAGE region and its supporting modules. Source hashes, owners and roles are in code-assets.json. Shared software tests establish portable invariants only. No model substitutes for a physical driver.
Troubleshooting with evidence
Many page changes per press
Check that the event return is used, not the continuously true pressed level. Confirm state objects persist between loop calls.
No event
Verify the two switch terminals used are electrically different contact sides. Tactile buttons often have internally connected pairs.
Slow response
Remove blocking delays and repeated bus scans. Measure input polling cadence rather than assuming the loop runs instantly.
Experiments after the baseline
Add a Back button with its own independent debounce state.
Create a parameter-editing page with distinct selection and editing modes.
Replace button input with an encoder only after its signal/pulse contract is verified.
Challenge
Implement an inactivity return-to-home timer that does not fire while editing a value.
Verification and next work
No physical upload or bench observation occurred. Expected device results are targets. Native test output is recorded separately in validation/host-results.json. Current bounded build attempts are in validation/repair-compile-results.json; historical attempts remain under validation/history/. Embedded compile/link and hardware behavior remain unverified until actual evidence is recorded.
Source records: inventory-club-v3, inventory-member-v14, pio-deps, lcd-lib, pio-mega, mega-doc, pio-ide. Verified repository destinations are recorded in repository-integration.json; only resolved IDs are exposed in relatedLabs/relatedLearnConcepts.
Build evidence and diagnostic references
Current compile attempts: not performed in this pass. Physical Upload and bench observation: not tested.
- Stage 1: successful compile recorded 2026-10-19; 5 warnings recorded. The supplied electrical-review flag is false.
- Stage 2: successful compile recorded 2026-10-19; 5 warnings recorded. The supplied electrical-review flag is false.
- Stage 3: successful compile recorded 2026-10-19; 5 warnings recorded. The supplied electrical-review flag is false.
Historical compile evidence is separate from the original guide's earlier unverified status and does not establish today's build or hardware behavior.
Diagnostics before integration
- ACK/NACK bus scan Environment: checkpoint1
Technical sources and references
- PlatformIO Mega board
megaatmega2560, Arduino framework and target memory
- PlatformIO dependencies
Dependency installation occurs during package/build processing
- Arduino Mega hardware
Board pinout, voltage and ADC family
- hd44780 source/API
Compatible backpack classes, initialization status, GPL-3.0 license
- Official PlatformIO IDE installation
Official VS Code extension includes Core and project environment tasks