Chapter 01: Moving In

Easy Light Toggles (adding some button inputs)

Mission Briefing: Silent Switches

The bunker's emergency lighting system has been flickering for three days now. Every shadow could hide another security drone, every blackout could mean the difference between staying hidden and becoming another casualty in this wasteland. The old-world engineers who built this place were clever, but their manual switches are too loud, too obvious. Metal clicking against metal in the silence of the apocalypse might as well be a dinner bell for the hunters above.

You've scavenged some momentary buttons from a derelict command center two sectors over. These aren't like the toggle switches you've been using. Press them, they work. Release them, they stop. Simple, quiet, perfect for what you need. But there's a problem: floating pins. When these buttons aren't pressed, the HERO Board's input pins don't know if they should read high or low. They float in electronic limbo, randomly switching states like a broken compass spinning in circles.

The solution lies in pull-up and pull-down resistors, concepts the pre-war electronics textbooks mention but never properly explain. One approach uses external resistors to pull the pin to a known state. Another uses the HERO Board's built-in pull-up resistors, eliminating extra components but inverting the logic. Both methods work, but understanding when and why to use each could determine whether your lighting system responds instantly to threats or fails when you need it most.

Tonight, you'll master both techniques. The wasteland doesn't forgive unreliable electronics, and neither should you.

What You'll Learn

When you finish this lesson, you'll be able to:

Wire momentary buttons to control LEDs using both external pull-down resistors and internal pull-up resistors. You'll understand why floating pins cause random behavior and how to eliminate that uncertainty. The HERO Board will respond predictably to button presses, turning lights on while pressed and off when released.

More importantly, you'll grasp the fundamental concept behind reliable digital input: ensuring every pin has a defined state at all times. This principle applies to every button, switch, and sensor you'll ever connect to a microcontroller.

Understanding Button Inputs

A momentary button works like a doorbell. Press it, the circuit completes. Release it, the circuit opens. Simple enough, but here's where things get tricky: when the button isn't pressed, the input pin on your HERO Board isn't connected to anything. It's literally floating in space, electrically speaking.

Imagine trying to measure the temperature of air that isn't there. Your thermometer would give you random readings because it has nothing concrete to measure. That's exactly what happens with a floating pin. The HERO Board's digitalRead() function will return completely random HIGH and LOW values, making your button appear to press itself.

The solution is to give that pin a default state. Pull-down resistors connect the pin to ground through a high-value resistor (usually 10k ohms), ensuring it reads LOW when nothing else is happening. Pull-up resistors do the opposite, connecting to power so the pin reads HIGH by default. When you press the button, you override this default state.

The HERO Board includes internal pull-up resistors you can enable in software, eliminating external components. However, this inverts the logic: unpressed reads HIGH, pressed reads LOW. Both approaches work perfectly, you just need to account for the logic difference in your code.

Wiring Your Light Switch

Wiring diagram showing LED and button connections

This lesson demonstrates two different wiring approaches. We'll start with the external pull-down resistor method, then switch to the internal pull-up approach.

Method 1: External Pull-Down Resistor

  1. Connect LED positive leg to HERO Board pin 22
  2. Connect LED negative leg to ground through a 330-ohm resistor
  3. Connect one button terminal to +5V on the HERO Board
  4. Connect the other button terminal to pin 23 AND to ground through a 10k-ohm resistor

The 10k resistor ensures pin 23 reads LOW when the button isn't pressed. When you press the button, +5V overrides the pull-down resistor, making pin 23 read HIGH.

Method 2: Internal Pull-Up (Simpler)

  1. Keep the LED wiring exactly the same
  2. Connect one button terminal to ground
  3. Connect the other button terminal to pin 23
  4. Remove the external 10k resistor

The HERO Board's internal pull-up resistor keeps pin 23 HIGH when the button isn't pressed. Pressing the button connects pin 23 to ground, making it read LOW.

The Complete Code