Control Room Override
The ancient brass button sits before you like a weathered sentinel, its worn surface catching the amber glow of Cogsworth City's clockwork streetlamps. Steam hisses through copper pipes overhead as another gear assembly clicks into place somewhere in the mechanical depths below your workshop. Your fingertip hovers mere inches above the button's surface, feeling the slight vibration of the city's heartbeat through its metal housing.
This isn't just any button. In Cogsworth City, buttons are the neural pathways of the great machine that keeps everything running. Press this one, and the LED beacon on Tower Eight will illuminate, sending a coded message to the Harbor District's automated cranes. Release it, and darkness returns, the message complete. Every citizen knows the weight of responsibility that comes with button control, because in a city powered by precision timing, a single misplaced press can throw entire districts out of synchronization.
Your HERO Board sits connected to this critical piece of Cogsworth infrastructure, its pins carefully wired to both monitor the button's state and control the beacon's response. The city's Chief Engineer has assigned you a crucial task: master the art of digital input control. Understanding how your microcontroller reads button presses and translates them into immediate actions isn't just academic knowledge here. It's the foundation skill every Cogsworth citizen needs to participate in the grand mechanical symphony that keeps their world turning, one perfectly-timed button press at a time.
What You'll Learn
When you complete this lesson, you'll be able to:
- Connect a push button to your HERO Board and understand the electrical principles behind digital input
- Write code that reads button states and responds instantly to user input
- Control an LED directly based on whether a button is pressed or released
- Use pull-down resistors to prevent floating inputs and ensure reliable button readings
- Structure your code with constants, variables, and conditional logic for responsive user interfaces
- Debug common button wiring issues and understand why proper grounding matters
By the end of this lesson, you'll have built your first interactive device that responds immediately to physical input, giving you the foundational skills needed for everything from simple switches to complex user interface controls.
Understanding Digital Input
Think of your microcontroller as a very attentive security guard stationed at the gates of a busy castle. This guard has one job: constantly check whether the gate is open or closed, then report that information to the castle's command center. A push button works exactly the same way, except instead of a physical gate, it's an electrical pathway that can be either connected (pressed) or disconnected (released).
When you press a button, you're physically forcing two metal contacts inside it to touch, creating a complete electrical circuit. When you release it, those contacts spring apart, breaking the connection. Your HERO Board can detect this change because it's constantly measuring the voltage on the button's pin. High voltage means pressed, low voltage means released. It's that simple, but getting reliable readings requires understanding a critical concept called pull-down resistors.
Without a pull-down resistor, your button pin becomes what engineers call "floating." Imagine trying to read a book in a room where the lights randomly flicker between on and off. That's what happens to your microcontroller when it tries to read a floating pin. The pull-down resistor acts like a gentle hand that keeps the lights steady when the switch is off, ensuring your microcontroller always gets a clear, reliable signal. This small component is the difference between a button that works perfectly every time and one that triggers randomly or fails to respond when you need it most.
Wiring Your Button Circuit
This circuit requires three connections, each serving a specific electrical purpose:
- LED Connection: Connect the long leg (anode) of your LED to pin 8, short leg (cathode) to ground. Pin 8 will provide the voltage needed to light the LED when your code sets it HIGH.
- Button Power: Connect one side of your push button to the 5V pin. This provides the high voltage signal that your microcontroller will detect when the button is pressed.
- Button Input with Pull-down: Connect the other side of your button to pin 2 AND to ground through a 10kΩ resistor. Pin 2 reads the button state, while the resistor ensures the pin reads LOW when the button isn't pressed.