Inputs and Outputs: Button, Reed Switch, LED, and Relay at Low Voltage
In this lesson: a button, a reed switch, and an LED, all wired safely at low, logic-level voltage only. Not in this lesson: a mains-voltage relay, controlling household lighting circuits, or wiring mains electrical, your Zigbee smart plug from Module 9 already handles mains voltage safely as a finished, certified product.
Input vs output in ESPHome
An input simply reads the world, a button being pressed, a reed switch quietly detecting a door. An output actively changes the world, an LED simply turning on. This lesson covers exactly one of each, plus a button that briefly acts as an input triggering a clearly visible output.
binary_sensor vs switch vs button
A binary_sensor simply reports a state, on or off, like a reed switch does. A switch is simply a controllable output that Home Assistant can turn reliably on and off. An ESPHome "button" component is different again, a momentary trigger rather than a persistent state, this lesson's physical push button is wired as a binary_sensor since we care about its pressed state.
Pull-up and pull-down: intuitively
A GPIO pin left entirely disconnected can float between electrical states quite unpredictably. A pull-up resistor holds a pin reliably high until something actively pulls it low, a pull-down does exactly the opposite. ESPHome can enable an internal pull-up entirely in software, no physical resistor ever needed for a simple button.
Pull-up: a step-by-step example
Simply wire your button between a GPIO pin and GND. With an internal pull-up enabled, the pin reads high normally and drops low the instant the button is pressed, connecting it to ground. ESPHome's binary_sensor config simply inverts this logic so "pressed" reads as true.
GPIO safety: rules that stay with you for years
Never connect a GPIO pin directly to more than 3.3V. Never connect two outputs directly together. Always use a resistor with an LED. These three rules, learned once here, apply to essentially every microcontroller project you'll ever build, well beyond this course.
GPIO pinout: which pins to choose to start
Use GPIO18 for your button and GPIO19 for your LED, both freely available on most ESP32 DevKit boards and not reserved for I2C or other built-in functions. Avoid GPIO strapping pins your board's documentation flags as special-purpose, using a plain, unremarkable pin avoids an entire category of confusing first-project problems.
Order of work: hardware, then YAML, then test
Wire the button first, confirm your wiring visually, then write the YAML, then compile and test. Adding both the button and LED to YAML at once, before either is wired, makes troubleshooting considerably harder if something doesn't work as expected.
Button and reed switch in YAML
A binary_sensor block with platform gpio, a pin number, an internal pull-up setting, and a friendly name is all a button needs. A reed switch, if you have one, wires and configures identically, only the physical component and its friendly name differ.
LED with a resistor
Never connect an LED directly to a GPIO pin without a resistor, it will draw too much current and can damage both the LED and the pin. A 220 ohm resistor in series limits current to a safe level while still giving a clearly visible brightness.
LED + resistor diagram: wiring it correctly
Connect GPIO19 to the resistor, the resistor to the LED's longer leg (the anode), and the LED's shorter leg (the cathode) to GND. LEDs only work in one direction, if it doesn't light up, try reversing it before assuming anything else is wrong.
Full YAML block: button + LED together
Add a switch section with platform gpio, GPIO19, and a friendly name like "Office LED", alongside the button's binary_sensor block. Both compile together in one file, and can optionally be linked with an automation later so pressing the button toggles the LED directly.
A relay? Not yet as a project
A low-voltage relay module exists and can be wired safely at logic level, but this course doesn't build a mains-voltage relay project, that combination of DIY wiring and mains electricity carries real risk best left to certified products. Your Zigbee smart plug already fills that role safely.
Detailed comparison: hallway_motion vs ESPHome button
Your Module 9 Zigbee motion sensor reports automatically with no configuration beyond pairing. This ESPHome button required wiring and YAML but gives you complete control over its exact behavior, both are valid, the right choice depends on whether you need a certified off-the-shelf product or a custom-built input.
Testing entities in Home Assistant
After flashing, confirm the button's binary_sensor entity toggles between "off" and "on" the instant you press and release it, and confirm the switch entity actually turns your LED on and off when toggled from Home Assistant's interface.
What a correct result means
A correct result means the button state changes instantly and reliably with each press, and the LED responds immediately to a toggle command, no delay, no flickering, no unresponsive presses.
Common mistakes with a first GPIO
Forgetting the pull-up setting leaves a button reading randomly. Forgetting the resistor risks the LED. Reversing the LED's polarity simply means it won't light, easily fixed by flipping it around.
Exercise: button + LED in one device
Wire both the button and LED to your existing ESP32, add both YAML sections, flash over OTA, and confirm both entities behave correctly in Home Assistant. Record the pins used in your notebook.
Key takeaways
This lesson stays at safe, low voltage, no mains wiring anywhere.
A pull-up resistor, often internal, keeps a button reading reliably.
Always use a resistor with an LED, never connect it bare.
Mains-voltage relay projects stay outside this course's scope entirely.
With a working input and output, Lesson 8 combines everything into a single mini project, your own control module for Home Assistant.
A real story: the LED that never lit
A reader in this course's community wired an LED, added the resistor correctly, and compiled successfully, yet the LED never lit despite the switch entity toggling on and off in Home Assistant without any error. The LED had simply been wired backwards, its two legs look nearly identical to a beginner. Flipping it around resolved it instantly, a reminder that "compiled successfully" and "wired correctly" are two entirely separate questions.
Why low voltage is the right boundary for this course
Every single project in this module stays firmly at 3.3 or 5 volts, genuinely safe to touch with bare hands and quite forgiving of small mistakes. Mains voltage carries real risk of shock or fire if wired incorrectly, a boundary this course maintains deliberately and consistently, certified products exist specifically to handle that risk safely on your behalf.
What a reed switch adds beyond a button
A reed switch, commonly used for door and window sensors, closes a circuit magnetically rather than mechanically, useful for detecting whether a door is open or closed rather than requiring a person to press anything. Wiring and YAML are otherwise identical to a button, only the physical mechanism differs.
Debouncing: a small detail worth knowing
A mechanical button can briefly register several rapid on-off transitions during a single physical press, called bouncing. ESPHome's binary_sensor component handles this automatically in most cases, worth knowing the term exists if you ever see a button trigger an automation multiple times for what felt like one press.
Extending this pattern to more inputs and outputs later
Once you're comfortable with one button and one LED, adding a second of either follows the exact same YAML pattern with a different pin number and friendly name, this lesson's real value is the underlying pattern, not the specific two components used to teach it.
Choosing between an internal and external pull-up
ESPHome's internal pull-up, enabled with a single YAML setting, is genuinely sufficient for nearly every simple button project this module covers, no physical resistor needed at all. An external pull-up resistor, a real component wired alongside the button, is occasionally preferred for longer wire runs or noisier electrical environments, a detail well beyond what this lesson's short breadboard wiring requires.
Understanding inverted logic in YAML
Because a pull-up button reads high when unpressed and low when pressed, ESPHome's configuration includes an inverted setting that flips this so the entity reports "on" exactly when you'd intuitively expect, at the moment of pressing rather than releasing. Without this setting, the entity's logic would feel backwards even though the wiring itself is entirely correct.
Choosing an LED color and brightness
Any basic 5mm LED works fine for this exercise, red and green are the most common and cheapest colors, and both are bright enough to see clearly without needing to dim your room. Later, more advanced projects might use PWM, pulse-width modulation, to dim an LED smoothly, a genuinely interesting technique but unnecessary for this lesson's simple on-off exercise.
Confirming your resistor's value by its color bands
A 220 ohm resistor typically shows red-red-brown color bands, though reading these bands reliably takes practice, a cheap multimeter set to resistance mode gives a definitive reading in seconds if you're ever unsure which resistor you're holding. Mixing up resistor values rarely causes damage at this voltage, but getting it right keeps your LED at a predictable, comfortable brightness.
What happens if you skip the resistor briefly
Briefly connecting an LED without a resistor won't necessarily destroy it instantly, but repeated or sustained use this way shortens its lifespan considerably and can damage the GPIO pin driving it over time. There's no good reason to skip the resistor even briefly, it costs almost nothing and entirely removes this risk.
Naming these entities consistently
Following Module 9's naming convention, "Office Button" and "Office LED" keep this project's entities clearly grouped with your other office-area entities on any dashboard, exactly the same discipline that's kept every device in this course organized since Module 9's very first lesson on naming and areas.
A brief look at what an automation could do here
Module 11 introduces Home Assistant automations properly, but it's worth previewing the idea here, a simple automation could watch this button's binary_sensor entity and toggle the LED switch entity each time it's pressed, entirely within Home Assistant rather than ESPHome's own YAML. This lesson stops short of building that automation, saving it for when Module 11 covers the concept properly.
Physical mounting considerations for later
For now, your button and LED stay comfortably on the breadboard, no enclosure or permanent mounting needed. If you eventually want a tidier, more permanent installation, a small project box with drilled holes is a common, low-cost next step well beyond this module's current scope.
A note on mains-voltage relay modules, for completeness
You may encounter cheap relay modules online marketed for controlling household lamps or appliances directly from a microcontroller, wiring mains voltage through a DIY relay board carries genuine risk of shock or fire if done incorrectly, and this course deliberately steers away from that path entirely. A certified Zigbee or WiFi smart plug, already vetted and safety-tested by its manufacturer, remains the right tool whenever you need to switch anything running on mains voltage, regardless of whether your home uses 120V or 230V wiring.
Low-voltage relays: a legitimate, safer alternative
A low-voltage relay, switching a small DC motor or a low-power device rather than mains electricity, is a genuinely reasonable project for an experienced hobbyist, and follows the exact same GPIO safety principles this lesson already covers. This course simply doesn't build one as a project, keeping its scope focused on the components most useful to a typical smart home beginner.
A real story: the reader who wanted more
A reader in this course's community once asked directly why this lesson doesn't cover controlling a lamp through a relay, since it genuinely seemed like a natural next step right after wiring an LED successfully. The honest answer given was that mains-voltage wiring genuinely deserves dedicated, proper electrical safety training well beyond what a smart home course can responsibly teach in a single lesson, and that a certified smart plug accomplishes the exact same practical goal without carrying any of the associated risk at all. The reader ultimately agreed the tradeoff genuinely made sense once framed that way, and happily stuck with the certified plug they already owned from Module 9 instead.
Comparing this lesson's risk profile to earlier lessons
Every wiring task in this module, from Lesson 6's sensor through today's button and LED, has stayed at the same safe, low, logic-level voltage throughout, nothing here carries meaningfully more risk than plugging in a USB cable. This consistency is deliberate and worth appreciating, this course never asks you to take on electrical risk beyond what a curious beginner should reasonably handle.
What happens to the LED entity if the device restarts
By default, a switch entity restores its last known state after a restart, so if your LED was on before a power interruption, it should return to on once the device reconnects. This behavior can be configured differently if you'd prefer a switch to always default to off after a restart, a small YAML setting worth exploring once you're comfortable with the basics this lesson covers.
Reading your board's actual pinout diagram
While GPIO18 and GPIO19 work well on most common ESP32 DevKit boards, always cross-check against your specific board's own pinout diagram before wiring, a handful of board variants label pins slightly differently or reserve a small number of pins for special functions. A quick search for your board's exact model name alongside "pinout" usually turns up a clear diagram within moments.
Why this lesson pairs a sensor lesson with an actuator lesson
Lesson 6 taught reading the world, Lesson 7 teaches changing it, together they cover the two fundamental halves of what any smart home device does, sensing and acting. Lesson 8's mini project deliberately combines both halves into one cohesive device, exactly the kind of integration a real smart home project eventually needs.
Testing components individually before combining them
Wiring and testing the button by itself first, well before adding the LED, makes it considerably easier to isolate a problem if one genuinely arises, since you'll know for certain whether an issue lies with the button's wiring or the LED's own wiring the moment it appears, rather than troubleshooting two entirely unfamiliar components simultaneously. This same one-thing-at-a-time discipline has served every single lesson in this module well so far, and it continues to pay off consistently here too.
What to do if the button entity never changes state
If pressing the button produces no visible change in Home Assistant at all, first confirm the pull-up setting is genuinely present and correctly spelled in your YAML, a surprisingly easy line to accidentally omit or mistype when writing a new sensor block from memory. Next, confirm the button itself makes proper, firm contact when pressed down, a loose breadboard connection can silently prevent the circuit from ever actually closing properly, even though everything else looks entirely correct.