Back to IoT Blog
Smart Home 18 min read Updated: March 2025

Smart Lighting with Alexa & Google Home

Build a complete voice-controlled smart lighting system using NodeMCU ESP8266, relay modules, and Home Assistant. Control your lights with Alexa and Google Assistant.

1. Project Overview

In this project, we'll build a complete smart lighting system that integrates with both Amazon Alexa and Google Home. You'll be able to control your lights with voice commands like "Alexa, turn on the living room lights" or "Hey Google, dim the bedroom lights to 50%".

🎯 What You'll Build:
  • WiFi-controlled relay module using NodeMCU ESP8266
  • Home Assistant integration for centralized control
  • Amazon Alexa voice control via HA-Bridge or Nabu Casa
  • Google Home integration via Home Assistant Cloud
  • Mobile app control through Home Assistant
  • Automation routines (sunset/sunrise, schedules)

This system is expandable - start with one light and add more channels as needed. You can control lamps, ceiling lights, LED strips, or any AC appliance safely.

2. Required Components

NodeMCU ESP8266 ESP-12E or ESP-12F module with WiFi
Relay Module (1-4 Channel) 5V relay, optically isolated
Light Bulbs & Holders E27/E26 LED bulbs recommended
Electrical Box For safe enclosure of AC wiring
Jumper Wires Male-to-female and male-to-male
USB Cable Micro-USB for NodeMCU programming
Power Supply 5V 1A for NodeMCU and relay
Wire Connectors Wago or wire nuts for AC connections
💡 Cost Estimate:

Total cost: $25-40 for a 1-channel system, $40-60 for 4-channel. Much cheaper than commercial smart switches ($50+ per switch)!

3. Understanding Relay Modules

A relay is an electrically operated switch. It allows low-voltage DC (from NodeMCU) to control high-voltage AC (your lights) safely through electromagnetic isolation.

Relay Module Pinout

DC Side (Low Voltage)          AC Side (High Voltage)
┌─────────────────┐           ┌──────────────────────┐
│ VCC  ─── 5V     │           │ COM  ─── Live In    │
│ GND  ─── GND    │           │ NO   ─── Live Out  │
│ IN1  ─── GPIO   │           │ NC   ─── (Unused)   │
└─────────────────┘           └──────────────────────┘
    Control Side                  Load Side

Relay Types

Type Active Description
Active HIGH 3.3V/5V Relay turns ON when GPIO is HIGH
Active LOW GND Relay turns ON when GPIO is LOW (more common)
⚠️ Important:

Most relay modules are Active LOW, meaning they trigger when the input pin is connected to GND. This is opposite to what you might expect!

4. Hardware Setup

1 Connect NodeMCU to Relay Module

Low-voltage DC wiring:

  • Relay VCCNodeMCU VIN (5V) or external 5V
  • Relay GNDNodeMCU GND
  • Relay IN1NodeMCU D1 (GPIO 5)
NodeMCU ESP8266 Pinout:
┌─────────────────┐
│  VIN   GND      │  VIN = 5V power in
│  D0    D1       │  D1 = GPIO 5 (relay control)
│  D2    D3       │  D0 = GPIO 16
│  D4    D5       │  D4 = GPIO 2 (built-in LED)
│  D6    D7       │
│  D8    RX       │
│  TX    RST      │
└─────────────────┘
2 AC Wiring (⚠️ HIGH VOLTAGE)

Connect your light bulb through the relay:

  • AC Live (Brown/Black)Relay COM
  • Relay NO (Normally Open)Light Bulb Live
  • AC Neutral (Blue/White)Light Bulb Neutral
  • AC Ground (Green/Yellow)Light Bulb Ground
⚠️ DANGER - HIGH VOLTAGE:
  • ⚠️ AC mains electricity can KILL you
  • Turn off power at the circuit breaker before wiring
  • Use a proper electrical enclosure
  • Don't touch exposed wires
  • If unsure, consult a licensed electrician
  • Never work on live circuits

5. NodeMCU Programming

Option 1: Arduino IDE with MQTT

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// MQTT Broker
const char* mqtt_server = "192.168.1.100";  // Your broker IP
const int mqtt_port = 1883;

// Relay pin
#define RELAY_PIN D1  // GPIO 5

// MQTT Topics
const char* lightTopic = "home/livingroom/light";
const char* lightStateTopic = "home/livingroom/light/state";

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
  delay(10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void reconnect() {
  while (!client.connected()) {
    String clientId = "NodeMCU-" + String(random(0xffff), HEX);
    if (client.connect(clientId.c_str())) {
      client.subscribe(lightTopic);
    } else {
      delay(5000);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  String message;
  for (int i = 0; i < length; i++) {
    message += (char)payload[i];
  }
  
  if (message == "ON") {
    digitalWrite(RELAY_PIN, LOW);  // Active LOW
    client.publish(lightStateTopic, "ON");
  } else if (message == "OFF") {
    digitalWrite(RELAY_PIN, HIGH);  // Active LOW
    client.publish(lightStateTopic, "OFF");
  }
}

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, HIGH);  // Start OFF (Active LOW)
  
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) reconnect();
  client.loop();
}

Option 2: ESPHome (Recommended for Home Assistant)

ESPHome is the easiest way to integrate with Home Assistant:

# living_room_light.yaml
esphome:
  name: living_room_light
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "YOUR_WIFI_SSID"
  password: "YOUR_WIFI_PASSWORD"

# Enable Home Assistant API
api:
  password: "your_api_password"

ota:
  password: "your_ota_password"

# Relay configuration
switch:
  - platform: gpio
    name: "Living Room Light"
    pin: D1
    id: living_room_relay
    restore_mode: ALWAYS_OFF
    
# Optional: Status LED
status_led:
  pin:
    number: D4
    inverted: true

Upload to NodeMCU:

# Install ESPHome
pip install esphome

# Compile and upload
esphome living_room_light.yaml run

6. Home Assistant Setup

Install Home Assistant

Choose one of these installation methods:

# Docker installation example
docker run -d \
  --name homeassistant \
  --privileged \
  --restart=unless-stopped \
  -e TZ=MY_TIME_ZONE \
  -v /path/to/config:/config \
  --network=host \
  ghcr.io/home-assistant/home-assistant:stable

Add ESPHome Integration

  1. Open Home Assistant UI
  2. Go to Settings → Devices & Services
  3. Click Add Integration
  4. Search for ESPHome
  5. Your NodeMCU should appear automatically
  6. Click Submit and enter API password

Configure MQTT Light (Alternative)

Add to your configuration.yaml:

mqtt:
  switch:
    - name: "Living Room Light"
      state_topic: "home/livingroom/light/state"
      command_topic: "home/livingroom/light"
      payload_on: "ON"
      payload_off: "OFF"
      qos: 1
      retain: true

7. Alexa Integration

Option 1: Home Assistant Cloud (Nabu Casa) - Easiest

  1. In Home Assistant, go to Settings → Home Assistant Cloud
  2. Click Start Free Trial ($6.99/month after 1 month free)
  3. Create account and log in
  4. Enable Alexa integration
  5. Say: "Alexa, discover devices"
  6. Your lights will appear in Alexa app

Option 2: HA-Bridge (Free, More Complex)

HA-Bridge emulates Philips Hue bridge for Alexa:

# Docker installation
docker run -d \
  --name ha-bridge \
  --net=host \
  -v /path/to/config:/config \
  bwssytems/ha-bridge
  1. Access HA-Bridge at http://your-ip:80
  2. Add new device with MQTT commands
  3. Set On/Off URLs to publish MQTT messages
  4. In Alexa app: Devices → + → Add Device
  5. Say: "Alexa, discover devices"

Option 3: Home Assistant Skill (Legacy)

Use the official Home Assistant skill in Alexa app (requires port forwarding or Duck DNS).

8. Google Home Integration

Home Assistant Cloud (Recommended)

  1. In Home Assistant Cloud, enable Google Assistant
  2. Link your Home Assistant account in Google Home app
  3. Say: "Hey Google, sync my devices"
  4. Control lights with: "Hey Google, turn on living room light"

Manual Configuration

Expose specific entities to Google:

# configuration.yaml
google_assistant:
  project_id: YOUR_PROJECT_ID
  service_account:
    key_file: /config/google_credentials.json
  expose_by_default: false
  exposed_domains:
    - switch
    - light
  entity_config:
    switch.living_room_light:
      expose: true
      room: Living Room
      aliases:
        - Main Light
        - Ceiling Light

9. Voice Commands & Routines

Basic Voice Commands

Alexa Google Home Action
"Turn on living room light" "Turn on living room light" Switch ON
"Turn off living room light" "Turn off living room light" Switch OFF
"Dim living room light to 50%" "Set living room light to 50%" Dim (if supported)
"Brighten living room light" "Brighten living room light" Increase brightness

Creating Routines

Alexa Routine - "Good Morning":

  1. Open Alexa app → More → Routines
  2. + → When: Voice → "Good morning"
  3. Add action: Smart Home → Turn on living room light
  4. Add action: Smart Home → Turn on kitchen light
  5. Add action: Music → Play morning news

Google Routine - "Good Night":

  1. Open Google Home app → Routines
  2. + → Starter: "Hey Google, good night"
  3. Add action: Turn off all lights
  4. Add action: Set thermostat to 68°F
  5. Add action: Play sleep sounds

Home Assistant Automations

# Automations in configuration.yaml
automation:
  - alias: "Sunset - Turn on lights"
    trigger:
      platform: sun
      event: sunset
    action:
      service: switch.turn_on
      target:
        entity_id: switch.living_room_light
        
  - alias: "Motion - Turn on hallway light"
    trigger:
      platform: state
      entity_id: binary_sensor.hallway_motion
      to: "on"
    action:
      service: switch.turn_on
      target:
        entity_id: switch.hallway_light
      data:
        duration: 60  # Turn off after 60 seconds

10. Safety Considerations

🔌 Electrical Safety:
  • Always turn off power at breaker before working with AC
  • Use a voltage tester to confirm power is off
  • Never work on live circuits
  • Use proper wire gauge for your load (14-16 AWG for lights)
  • Ensure all connections are tight and secure
  • Use wire nuts or Wago connectors, not electrical tape
  • Install in a proper electrical enclosure
  • Don't exceed relay current rating (typically 10A)

Best Practices

11. Troubleshooting

Problem: NodeMCU Won't Connect to WiFi

Problem: Relay Clicks But Light Doesn't Turn On

Problem: Home Assistant Can't Find Device

Problem: Alexa/Google Can't Discover Devices

Next Steps

Expand your smart home system:

  • Add more lights and rooms
  • Integrate motion sensors for automation
  • Add dimmer support with PWM
  • Create scenes (Movie Time, Dinner Party)
  • Integrate with smart switches
  • Add energy monitoring