Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversFall ResetAmazon USFall reset deals: check better picks before checkoutAmazon US: today's deals, useful picks and quick comparisons.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
Blog

Build an ESP32 Smart-Home System With Alexa and Manual Switches

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

You can control ESP32-connected lights or other suitable loads from the Alexa app and keep a physical switch—but the ESP32 does not connect to Alexa by itself. For a flexible DIY setup, use ESP32 + ESPHome + Home Assistant + an Alexa integration. Home Assistant links the device’s actual state to Alexa, while the ESP32 can continue reading local switch inputs. Treat a bench prototype and a permanent mains installation as different projects: exposed hobby relay boards are not a safe substitute for a properly rated, enclosed smart relay installed to local electrical code.

What you are building

The ESP32 reads a button or wall-switch input and controls a relay output. ESPHome runs on the ESP32; Home Assistant registers the device, provides local control and automations, and passes supported entities to Alexa. Alexa’s app and voice commands operate those exposed entities through the linked integration.

Manual switch or button
        ↓
ESP32 GPIO input → ESPHome → Home Assistant
                               ↓
                    Alexa integration → Alexa app / voice
                               ↓
ESP32 GPIO output → suitable relay hardware → load
Part Job
ESP32 development board Reads inputs and drives relay-control signals.
ESPHome Firmware configured in YAML; connects the ESP32 to Home Assistant.
Home Assistant Maintains entities and state, runs local automations, and bridges supported entities to Alexa.
Relay or smart-relay hardware Switches the electrical load; it must be selected for the actual load and installation.
Physical switch Provides local control. Its behavior depends on whether it is momentary or maintained.
Alexa Provides app and voice control for entities exposed through the integration.

Possible entities include lights, fans, low-voltage pumps or valves, and sensors. An appliance or mains load is appropriate only when the switching hardware, enclosure, wiring, protection, and installation are suitable for it. The word “ESP32” covers multiple chip families and boards; the GPIO map and electrical behavior are not universal.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the integration path

For most DIY builders: ESPHome, Home Assistant, and Home Assistant Cloud. ESPHome integrates with Home Assistant using its native API, whose documented default port is 6053. Devices may be discovered automatically or added by hostname or IP address. See the Home Assistant ESPHome integration documentation and ESPHome API documentation.

#1 Best Overall
ESP-WROOM-32 ESP32 ESP-32S Development Board 2.4GHz Dual-Mode WiFi + Bluetooth Dual Cores Microcontroller Processor Integrated with Antenna RF AMP Filter AP STA Compatible with Arduino IDE (3PCS)
  • 2.4GHz Dual Mode WiFi + Bluetooth Development Board
  • Support LWIP protocol, Freertos
  • SupportThree Modes: AP, STA, and AP+STA
  • Ultra-Low power consumption, Compatible with Arduino IDE
  • ESP32 is a safe, reliable, and scalable to a variety of applications

To expose supported devices to Alexa, Home Assistant Cloud is the simpler route. Home Assistant documents a 30-day free trial followed by a paid subscription; check the official page for current terms and price. Cloud avoids the need to configure dynamic DNS, manage an SSL certificate, and forward a router port for this integration. Alexa control still uses an Internet/cloud path, even when ESP32-to-Home Assistant control is local. Details: Home Assistant’s Alexa integration guide.

The manual Alexa Smart Home Skill route is possible, but is substantially more involved. It calls for an Amazon Developer account, AWS setup, a skill and Lambda configuration, account linking, and an HTTPS-reachable Home Assistant installation. That adds operational and security responsibilities; follow Home Assistant’s current documentation rather than relying on an old tutorial. AWS allowances and pricing can change, so verify current AWS terms before deployment.

A vendor cloud platform may be convenient for a single supported device, but it brings vendor-account and service dependencies. Matter is another possible route only when the selected hardware, firmware, device type, commissioning flow, and Alexa support all fit. A chip’s Matter or Thread capability alone does not make a finished device Alexa-compatible. A familiar ESPHome/Home Assistant setup is usually the clearer starting point for a Wi-Fi relay project.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Parts for a low-voltage prototype

  • A documented, well-supported ESP32 development board and its pinout or schematic.
  • A relay module suitable for the intended prototype load, with known coil supply, trigger logic, polarity, isolation, and contact ratings.
  • A power supply appropriate to both the ESP32 board and relay module. Do not assume the board’s USB supply can power a relay coil.
  • A momentary push button or maintained switch, plus pull-up or pull-down biasing if the chosen input does not provide it.
  • Proper terminals and wiring; an enclosure and appropriate circuit protection for the application.
  • Flyback protection when driving a bare inductive load, unless the driver assembly already provides it.
  • A Home Assistant host—such as a supported small computer, mini PC, virtual machine, or appliance—and an Alexa device or the Alexa mobile app.
  • A USB cable for initial firmware flashing and serial diagnostics.

Some preassembled boards document their own contact arrangement, supply options, pin map, and nominal relay ratings. Those are properties of that specific assembly, not of ESP32 boards generally. For example, consult the ESPHome device database entry for the ESP32 Relay X1 as a board-specific reference—not as proof that any relay board is suitable for household wiring.

Rank #2
ELEGOO 3PCS ESP-32 Dev Boards, ESP-WROOM-32, USB-C, WiFi Bluetooth 4.2
  • Dual-Core Performance Up to 240 MHz: Run sensor processing, wireless communication, automation logic and connected-device tasks on a 32-bit dual-core ESP32 platform designed for responsive embedded and IoT projects
  • Built-in Wi-Fi and Bluetooth 4.2: Connect to 2.4 GHz Wi-Fi networks or use Bluetooth Classic and BLE for wireless sensors, smart devices, remote controls, home automation and other connected projects
  • Flexible Power-Saving Modes: ESP32 power-management features support dynamic clock scaling and low-power operating modes, helping developers reduce energy use in compatible sensing, monitoring and connected-device applications, suitable for battery-powered Internet of Things (IoT) devices.
  • USB-C Programming with CP2102: Connect through USB-C for power, sketch uploads and serial monitoring, while GPIO, UART, SPI and I2C interfaces support sensors, displays, motor drivers and other modules (USB-C cable not included)
  • Over-the-Air Update Support: Configure OTA functionality through a compatible ESP-32 software framework to update deployed firmware over Wi-Fi without reconnecting the board by USB for every revision

Prominent mains warning

Do not work on energized mains wiring. An exposed breadboard or hobby relay module does not belong loose inside a wall box. A relay’s printed contact rating alone does not establish that the complete board or installation is safe: load type and inrush, isolation, PCB spacing, terminals, thermal behavior, enclosure, fusing, certification, and local code all matter. Use a certified, enclosed smart-relay product for permanent household switching and have a qualified electrician perform or inspect the installation when required. Keep hazardous-voltage conductors physically separated from low-voltage ESP32 wiring. This guidance is not a substitute for electrical design or code compliance.

Choose GPIOs for the exact board

Before wiring, identify the precise chip variant and development-board pinout. GPIO capabilities and numbering differ across original ESP32, S2, S3, C3, C6, H2, and other boards. On the original ESP32, for instance, GPIO34 and above are input-only and lack internal pull-up/down resistors. Flash- or PSRAM-associated pins should not be casually reused. Boot-strapping pins can affect startup, and their behavior depends on the board circuitry and connected module. Review Espressif’s ESP32 hardware design guidance and boot-mode selection notes.

Pick GPIOs from that board’s documentation, avoid boot-sensitive pins for relay outputs unless you have tested the full circuit, and check the relay state at power-up, reset, Wi-Fi loss, and firmware flashing. Do not copy a pin table from another ESP32 variant or board revision.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Decide what the physical switch means

Momentary push button: press to toggle

A common low-voltage prototype connects a momentary button between a GPIO input and ground. The ESP32 input uses a pull-up; a press reads as active. The firmware toggles the relay on a press and filters contact bounce. This event-based model is easy to combine with app or voice commands, but the button itself does not show the current light state by its position.

Rank #3
ELEGOO ESP-32 Super Starter Kit with Tutorial Compatible with Arduino IDE
  • Powerful ESP-32 Board: Unlock the world of Internet of Things (IoT) and advanced electronics with the heart of this kit: the ESP-32 board. It features a powerful dual-core processor, integrated Wi-Fi and Bluetooth 4.2, making it perfect for building connected, smart devices that communicate with your phone or the cloud. It's fully compatible with the Arduino IDE for easy programming.
  • Super Starter Kit: This kit contains over 35 different modules and electronic components, including sensors, displays, motors, and input devices. From LEDs and buttons to an OLED screen, servo motor, and keypad, you have everything needed to explore a vast range of projects in one box.
  • Step by Step Online Tutorial: Jump right in with our detailed, beginner-friendly tutorial. Access 30+ projects with complete code, clear circuit diagrams, and step-by-step instructions. Learn the fundamentals of electronics, coding, and how to utilize the ESP-32's unique capabilities without any prior experience.
  • Hands-on Learning for All Skill Levels: Perfect for students, makers, engineers, and hobbyists. Start with basic circuits and coding, then progress to intermediate and advanced IoT applications. Build practical projects like weather stations, smart home controllers, remote-controlled devices, and interactive gadgets. The skills you learn are the foundation for real-world innovation.
  • Quality & Great Support: Elegoo is committed to quality. We provide a clear, detailed tutorial guide, refined code, and a well-organized component kit. All modules are carefully selected for reliability and ease of use. Our dedicated technical support team and active online community are ready to help you succeed in your learning journey.

Maintained switch: position or transition

A maintained switch has an on/off position. One approach makes the relay follow that position: switch-on requests relay-on, switch-off requests relay-off. This is intuitive when the physical lever should represent the output. Another approach treats each change as a toggle command, which can avoid some state conflicts after Alexa changes the relay, but the lever position may then disagree with the light.

A conventional maintained switch is not automatically synchronized with an app-controlled relay. Decide whether position or toggle behavior is intended, then configure and test it. Two-way or multi-way lighting circuits need a different design from a simple GPIO input; do not treat this example as a drop-in replacement for household switching.

Illustrative ESPHome configuration

This is a starting example for a momentary button and one active-low relay input. Replace the board choice, GPIOs, polarity, credentials, and behavior to match the actual hardware. Never assume GPIO16 or GPIO17 is suitable on your board, or that a relay is active-low.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
esphome:
  name: esp32-smart-home
  friendly_name: ESP32 Smart Home

esp32:
  board: esp32dev
  framework:
    type: esp-idf

logger:

api:
  encryption:
    key: "REPLACE_WITH_A_GENERATED_KEY"

ota:
  - platform: esphome
    password: "REPLACE_WITH_A_STRONG_PASSWORD"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  ap:
    ssid: "ESP32 Smart Home Fallback"
    password: "REPLACE_WITH_A_STRONG_PASSWORD"

captive_portal:

switch:
  - platform: gpio
    id: relay_light
    name: "Living Room Light"
    pin:
      number: GPIO16
      inverted: true
    restore_mode: RESTORE_DEFAULT_OFF

binary_sensor:
  - platform: gpio
    id: wall_button
    name: "Living Room Wall Button"
    pin:
      number: GPIO17
      mode:
        input: true
        pullup: true
      inverted: true
    filters:
      - delayed_on: 20ms
      - delayed_off: 20ms
    on_press:
      - switch.toggle: relay_light

The API encryption key and OTA password are placeholders, not usable credentials. Generate a key using the method supported by your installed ESPHome version; the native API documentation describes encryption keys as 32-byte base64-encoded values. Store Wi-Fi details in secrets rather than publishing them in shared YAML. Check the ESPHome component documentation for syntax compatible with your installed version.

Rank #4
ESP-WROOM-32 ESP32 ESP-32S Development Board 2.4GHz Dual-Mode WiFi + Bluetooth Dual Cores Microcontroller Processor Integrated with Antenna RF AMP Filter AP STA Compatible with Arduino IDE (1 PCS)
  • 2.4GHz Dual Mode WiFi + Bluetooth Development Board
  • Support LWIP protocol, Freertos;ESP32 is a safe, reliable, and scalable to a variety of applications
  • SupportThree Modes: AP, STA, and AP+STA
  • Ultra-Low power consumption, Compatible with Arduino IDE
  • 1PCS 30Pin ESP32 Development Board 2.4GHz WiFi Dual Cores Microcontroller Integrated with Antenna RF Low Noise Amplifiers Filters
  • inverted: true on the relay is appropriate only if the module turns on when its input is low.
  • The button’s pull-up and inversion assume the button closes the GPIO to ground. Different wiring needs different configuration.
  • The 20 ms filters debounce this example; adjust only after testing the chosen switch and wiring.
  • RESTORE_DEFAULT_OFF asks for a conservative default after restoration when no saved state is available. It is not a substitute for safe hardware behavior during reset.
  • A maintained switch generally needs state-following or transition logic, not a blind on_press: switch.toggle action.

ESPHome warns that a GPIO relay can momentarily activate during reset before firmware initialization, depending on the pin, board and relay circuitry. Software settings cannot control a GPIO before the microcontroller starts. Choose a predictable hardware default and test with the real load disconnected first. See the ESPHome GPIO switch documentation.

Flash, add, and test the device

  1. Install or prepare Home Assistant, then create a device in ESPHome. Select the exact board variant and add Wi-Fi credentials, API encryption, and OTA credentials.
  2. Configure the relay and input using GPIOs verified against the board’s documentation. Validate the configuration before connecting a load.
  3. For first installation, flash by USB. Observe serial logs through boot and Wi-Fi connection. USB is easier to recover than relying on OTA before the network setup has been proven.
  4. Confirm the ESP32 joins Wi-Fi and that its API is reachable. If using the command-line workflow, typical commands are esphome config esp32-smart-home.yaml, esphome compile esp32-smart-home.yaml, esphome upload esp32-smart-home.yaml, and esphome logs esp32-smart-home.yaml; command details depend on the installed ESPHome version and setup.
  5. In Home Assistant, open Settings → Devices & services → Add integration, choose ESPHome, and follow discovery. If the device is not found, enter its hostname or IP address and provide the configured API encryption key if prompted.
  6. Test the relay from Home Assistant, then the physical input, then the reported state. Repeat across a reboot and a Wi-Fi interruption. Keep the hazardous load disconnected until behavior is verified.

Expected result: the device appears in Home Assistant, its relay entity can be controlled, the input is reported, and the configured local action works. Test whether the load remains safely off or follows the intended recovery policy after a power cycle. Do not infer electrical safety from successful software tests.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Expose supported entities to Alexa

With Home Assistant Cloud, sign in or create the cloud account, configure Alexa exposure in Home Assistant, choose only the entities you want Alexa to operate, and link the account when prompted. Then discover devices in the Alexa app, rename them clearly, assign them to rooms, and test both app control and voice commands. Exact Home Assistant menus can evolve, so use its current Alexa integration instructions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Amazon’s general device-add flow in the Alexa app is Devices → plus icon → Add Device, followed by the device type/brand prompts. In this project, Alexa generally discovers the entity exposed by Home Assistant, not the bare ESP32 development board. Amazon notes that many Wi-Fi smart-home devices use 2.4 GHz, but compatibility depends on the specific device; see its device discovery guidance.

Best Value
HiLetgo ESP-WROOM-32 ESP32 ESP-32S Development Board 2.4GHz Dual-Mode WiFi + Bluetooth Dual Cores Microcontroller Processor Integrated with Antenna RF AMP Filter AP STA for Arduino IDE
  • 2.4GHz Dual Mode WiFi + Bluetooth Development Board
  • Ultra-Low power consumption, works perfectly with the Arduino IDE
  • Support LWIP protocol, Freertos
  • SupportThree Modes: AP, STA, and AP+STA
  • ESP32 is a safe, reliable, and scalable to a variety of applications

Use distinct, natural names such as “Living Room Light,” “Bedroom Fan,” or “Porch Light.” Avoid duplicate names across exposed entities, group devices by Alexa room, and test the actual commands. Not every Home Assistant entity or capability appears identically in Alexa. Entity type matters: for example, Home Assistant’s Alexa Smart Home documentation notes limitations around using switch entities as routine triggers in some contexts. Do not promise that every sensor, button, scene, or automation will be controllable or usable as a routine trigger.

How state stays in sync—and where it can fail

In the intended flow, a button press changes the ESPHome relay entity; Home Assistant receives the update over the native API; and the Alexa integration can report the changed state to Alexa. Amazon’s Smart Home Skill API supports proactive device state updates when a physical device changes, subject to the integration, permissions, and account configuration. See Amazon’s Smart Home Skill API overview.

Avoid having a manual switch change the relay without updating the entity Home Assistant tracks, or maintaining a separate software state that can drift from the physical relay. With a maintained lever, Alexa may change the output while the lever remains in the opposite position; choose an input strategy that makes this behavior clear. Test the physical output, Home Assistant state, and Alexa-reported state together rather than assuming that a successful command proves all three agree.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Power, network, and failure behavior

  • Boot-time relay activation: Can result from active-low logic, a floating GPIO, board pull resistors, or a boot-strapping conflict before ESPHome initializes. Verify polarity, choose a better documented pin, add appropriate hardware biasing, and test reset with the load disconnected.
  • ESP32 will not boot: Inspect the selected GPIO and board schematic for a strapping pin pulled to the wrong level, GPIO0 held in download mode, or a connection to flash/boot circuitry. Also check whether relay-coil current causes the supply voltage to drop. Espressif documents how strapping-pin levels determine boot mode.
  • Wi-Fi or Internet outage: Design the button-to-relay behavior to work locally where the selected firmware and hardware permit. Home Assistant automations cannot run if their host is offline; Alexa’s cloud control path will not work when its required network/cloud connection is unavailable. Set and document a safe relay state, and do not display a command as successful when the device is unavailable.
  • Power restoration: Decide explicitly whether the load should restore off, on, or to a prior state. “Off” is often easier to reason about for ordinary lights, but pumps, heating, refrigeration, security, and medical applications require application-specific fail-safe analysis.
  • OTA failure: If an update fails, check the network and device availability, review logs, and reflash over USB if needed. ESPHome notes that actions may not be delivered while the API connection is unavailable; design automations to account for availability rather than treating an offline command as completed.
  • Stale Alexa state: Confirm that the physical input changes the Home Assistant entity, ESPHome API stays connected, the correct entity is exposed, and the Alexa account/skill is linked correctly. After permission changes, Home Assistant notes that unlinking and relinking may be necessary.

Security and maintenance

  • Use WPA2 or WPA3 and strong, unique Wi-Fi credentials.
  • Enable ESPHome API encryption and use a strong OTA password; keep secrets out of public YAML repositories.
  • Do not add unnecessary router port forwarding. The manual Alexa route exposes more infrastructure to the Internet and requires HTTPS and careful maintenance.
  • Use multi-factor authentication on Amazon and other relevant accounts where available, secure Home Assistant backups, and expose only devices appropriate for voice/app control.
  • Keep Home Assistant and ESPHome updated, and recheck documentation when changing versions or board hardware.

Alexa devices and the Alexa app can allow people to operate connected devices, so secure the account and mobile device as well; consult Amazon’s Alexa security guidance.

When a DIY ESP32 relay is the wrong choice

Choose a certified, enclosed smart relay or have an electrician handle the installation if the project involves permanent in-wall mains wiring, high current, motors or compressors, heating equipment, pumps, security-critical loads, or any situation where you cannot safely isolate, enclose, rate, and test the assembly. For construction and building work, installation method, inspection, and code compliance matter as much as firmware. Keep custom ESP32 control to a low-voltage prototype or a properly engineered enclosure when you cannot verify the complete mains assembly.

Practical recommendation: For a hobby system, use a documented ESP32 board, ESPHome, Home Assistant, and Home Assistant Cloud for the Alexa bridge; test switches and state locally before exposing entities. For a permanent household electrical installation, use suitable certified hardware and qualified installation rather than placing a loose relay module in the wall.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.