Automatic IoT Hallway Night Light With ESP8266




Introduction: Automatic IoT Hallway Night Light With ESP8266

I started this project inspired by a staircase light from another instructable post. The difference is the brain of the circuit is using ESP8266, which means it will be come an IoT device.

What I have in mind is to have the hallway night light for the kids, when they step out from their room, it light up the path going to the bathroom. For this I am using the ESP8266 to detect the motion from the PIR sensor. I used 2 PIR sensors, one at each end for the return trip. Given that ESP8266 is IoT capable, then I can also use this to detect whether there is any movement on the hallway by posting MQTT message to the Home Assistant.

Step 1: Gather All the Materials Needed

For this project I am using the following materials:

- ESP8266

- PIR sensor

- 330 Ohm resistor which act as the current limitter

- 5 V addressable LED Strip (WS2812B)

- Electrical conduit to house the lights

Step 2: Connect the Circuit

We are essentially connecting the LED strip Data line to pin D2 or ESP8266 via 330 Ohm resistor to limit the current. Keep in mind the ESP8266's operating voltage is 3.3V.

The PIR sensors are connected to Pin D5 and D6, one for the left sensor and one for the right. Don't forget to connect the power for PIR and LED strip to 3.3V in this case.

Step 3: Upload the Code

For the code to work you will need the following library:

- "FastLed" library by Daniel Garcia, in this instance I have version 3.3.3 installed

- TimeLib

- ESP8266Wifi

- ESP8266WebServer

- ArduinoOTA

If you don't already have them installed you can install them from "Tools->Manage Library" in the Arduino interface.

In the following section of the code

#define FASTLED_ESP8266_D1_PIN_ORDER<br>#include "FastLED.h"
#define NUM_LEDS 30
#define LEDS_PER_STAIR 2        // Number of Leds per stair.  Not yet currenlty changable - just noteable
#define BRIGHTNESS 120          // 0...255  ( used in fade7 )
#define PIN_LED 04               // LED Data pin (GPIO4) D2
#define PIN_PIR_DOWN 14          // PIR Downstairs Pin (GPI14) D5
#define PIN_PIR_UP 12            // PIR Upstairs Pin (GPI12) D6

You can configure the Number of LED in your LED strip, also the connection of the LEDStrip if you decide to connect it to a different pin and also the PIR sensor pin if you decide to connect it to a different pin of the MCU.

The above configuration is located in "ledsettings.h" file.

You can download the full source code from the following link.

Once you are able to compile the code successfully you can upload it to the ESP8266.

Step 4: Test and Test and Troubleshooting

If all goes well, you should have a working Hallway Lights that you can be proud of. When you power up the circuit for the first time, the LED Strip will light up with some rainbow sequence. Then the can the ESP8266 will act as an access point (AP) to allow you to configure the WiFi connection.

If you are using the code that I have you should be able to see "ESP-HallLight" as the access point. For security I have put password protection for the AP. The default password is "arduino" you can change this in the settings.h file, in the following section.

#define CLOCK_NAME "ESP-HallLight"
#define WIFI_APPSK "arduino"  // default AP password

Once connected successfully using a mobile phone or laptop via WiFi, you should be able to point your browser to, you should see the settings screen as shown in the picture above. You can now enter your WiFi settings and once entered ESP8266 will re-boot and trying to connect to your WiFi. If it is able to connect successfully you will no longer see the "ESP-HallLight" access point.

If you are still connected to Arduino interface you can monitor this through the serial monitor.

Note: You don't have to configure the WiFi for the sensors to work, it should work after a few seconds when the initial lights are off.

To test you can try walking from one side or waving your hand, the light should light up following the direction of travel, if it is doing the opposite, then you need to swap the left and right sensor in the code.

During my first build I accidentally connect the wrong end of the LED strip, which resulting none of the LEDs lighting up.

I hope you enjoy this build, if this is helpful, please vote for the contest. If you have any questions don't hesitate to drop me a note, and I will get to it as soon as I can.

Some after thought would be to add the additional features like :

Lighting Challenge

Participated in the
Lighting Challenge

Be the First to Share


    • Microcontroller Contest

      Microcontroller Contest
    • Back to School: Student Design Challenge

      Back to School: Student Design Challenge
    • Halloween Contest

      Halloween Contest



    1 year ago

    Can someone tell me how to speed up the effect, it's pretty slow if you're walking down a hall ?

    How does one find the TimeLib.h library required?
    Ok, I found it, Using the Arduino Library Manager, install "Time by Michael Margolis".


    2 years ago

    Hi, what are you feeding it with, please? Those individually addressable LEDs are 5V, while current for such length would be substantial (18 or even 30 mA per LED at full brigtness, I recall?), right? Still in the schematic I see you have the VCC connected to the ESP directly - does this for you like this?


    Reply 2 years ago

    Hi, I actually connect the LED to 3.3V instead of 5V. This seems to work fine.
    With the current limiter, I use 330 ohm resistor connected to the data line and this seems to be enough as it is only required to drive the signaling logic.
    The VCC of 5V from power supply is connected to VIN which will go through the regulator to power the ESP8266 which is 3.3 V.


    Reply 2 years ago

    That is interesting, how many LEDs you have in the strip, please? I actually have 5050 stripes, which may require more juice. The other day I fried a Nano with two SG90s povered directly under some load and it never occured to me to try something like this again ... but will likely try it out, although I have bought a 5V 6A adapter or two. After all, I have a stock of them boards. :D


    Reply 2 years ago

    Ah, that makes sense then and that was wat got me wondering, from the schematic above my impression was tha you were feeding the LEDs fro the ESP ... which only makes sense with some mosfets, I guess.

    In my scenario I am powering two 30x5050 LED strips along with Arduino from a 5V 6A source and have connected the LED control wire to a PWM pin and was wondering if it can be done any way easier.


    Reply 1 year ago

    The Vin pin from the Arduino is pretty much the same connection from the USB power so you can connect a small amount of LED to i. It draw the current from the USB port and not the Arduino.
    If you are connecting more LEDs it might not work depends on where you are connecting the USB connector to. Preferably connecting it directly to external power supply instead of USB port of pc.


    Reply 1 year ago

    Yep, was looking at the specs and there is a 50 or 200mA limit on Vin & GND (as opposed to 40mA on other pins). Enuf for a small ligt.


    Question 2 years ago on Step 3

    i Can't find the mainpage.h librarie
    Where do i find that?