ESPHome Wireless PIR and Light Sensor
by qpaulson in Workshop > Home Improvement
19849 Views, 12 Favorites, 0 Comments
ESPHome Wireless PIR and Light Sensor
![Screen Shot 2021-01-22 at 10.09.07 AM.png](/proxy/?url=https://content.instructables.com/FAH/PQL4/KK8AMITV/FAHPQL4KK8AMITV.png&filename=Screen Shot 2021-01-22 at 10.09.07 AM.png)
This project is to build a cheap ( Under $15 ) Wireless PIR motion sensor with Lux sensor so that you can automated the lights for whatever room its monitoring.
I built this because my Son was always leaving his lights on and I thought this was a good way to save some money.
It uses the ESP8266 for to allow for wireless communications. ESPHome allows easy configs and OTA updates.
With the Lux sensor, you can have the lights only come on when its actually dark in the room.
This sensor works with Home Assistant ( I have not tried it with any other Platform )
Supplies
- WeMos D1 Mini ESP8266 Development board ( Or equivalent/similar )
- HC-SR501 PIR Sensor
- Light Sensitive photoresistor
- 1 - 10K Ohm Resistor
- Small silicon Wire
- Need to be running Home Assistant ( I have not tried these with any other Home Automation platform )
Wire It Up
![Screen Shot 2021-01-22 at 10.43.28 AM.png](/proxy/?url=https://content.instructables.com/FNN/BGRZ/KK8AMIWH/FNNBGRZKK8AMIWH.png&filename=Screen Shot 2021-01-22 at 10.43.28 AM.png)
The wiring is fairly straight forward as the schematic shows.
The PIR Sensor takes 5V input and the Photoresistor takes 3.3V input
Wire the PIR Sensor:
Note: If you take the white cover off the front of the PIR sensor, you can see the labels for the pins
- Wire the Ground on the Development Board to both the 10K resistor and the ground pin on the PIR sensor
- Wire the D4 Pin on the Development Board to the middle pin on the PIR sensor, that is the signal wire.
- Wire the 5V output on the Development board to the VCC pin on the PIR sensor
Wire the Photorestor:
- Wire the other end of the 10K resistor to one end of the Photorsistor and to the A0 pin on the Dev board.
- Wire the other end of the Photo resistor to the 3.3V power output of the Dev board.
IF you use my 3D printed housing/case for this project - The wires will need to be short :)
Setup ESPHome in HA
![Screen Shot 2021-01-22 at 11.01.38 AM.png](/proxy/?url=https://content.instructables.com/FKW/D13C/KK8AMK1K/FKWD13CKK8AMK1K.png&filename=Screen Shot 2021-01-22 at 11.01.38 AM.png)
In Home Assistant, if you don't already have ESPHome setup - You will need to install it.
- Login to your Home Assistant
- Click on "Supervisor"
- Click on "Add-on Store"
- Search ESPHome
- Install
- Once installed..
- Set the slide bar to "Start on Boot"
- Set the slide bar for "Add to sidebar"
- Click "Start"
- There should be a green dot in the top right corner once its started successfully
- Click on the ESPHome link that is now showing on the left side Nav
- It will ask you for a login:
- User: admin
- Leave the password blank
Setup the Sensor in Home Assistant Using ESPHome
This is the tricky part....
Now that the ESPHome plugin is fully installed we can plug in the Wemos D1 Mini board and program it.
- Plug in your Development Board to the same machine that Home Assistant is running on.
- Don't worry if the USB device does not show up in the dropdown just yet
- Click on the Green + in the bottom right of the screen
- Give your Node a name and click on the big green "next" button.
- In the Device dropdown, select "Wemos D1 and Wemos D1 Mini" or whatever board you have
- Enter in your WIFI SSID and WIFI Password
- Set a password for your device as well so you can protect the OTA updates.
- Click the big green "Next" button
- Click Submit
- Now the best thing to do is reboot your Home Assistant because ESPHome is a bit buggy and it will save you hassle later .. To do this:
- Click on Supervisor
- Click System at the top
- Click on Reboot Host
- Confirm
- Wait...
... 5 Minutes Later ...
- When HA is back up and ready, and ESPHome loads again, you can load the ESPHome page in the left nav
- Click on the EDIT button under your newly created Sensor
- Append the Code snippet below to the end of your file:
- Click Save
- At the top of the screen is a dropdown that says "OTA", click on that and you should be able to choose the USB device that the dev board is plugged into
- Click the "Upload" button
If all goes well, it should build, compile and push the code to your dev board. You should hopefully see the Sensor come online and show the WIFI signal strength, IP address and events that look something like this:
[11:19:02][D][adc:056]: 'LUX Sensor': Got voltage=0.38V
[11:19:02][D][sensor:092]: 'LUX Sensor': Sending state 76.56250 lux with 2 decimals of accuracy [11:19:32][D][adc:056]: 'LUX Sensor': Got voltage=0.38V [11:19:32][D][sensor:092]: 'LUX Sensor': Sending state 76.56250 lux with 2 decimals of accuracy [11:19:50][D][binary_sensor:036]: 'PIR Sensor': Sending state OFF
Code Snippet: ( REMEMBER TO APPEND, DO NOT REPLACE )
sensor: - platform: adc pin: A0 name: "LUX Sensor" update_interval: '30s' unit_of_measurement: lux filters: - lambda: |- return (x / 10000.0) * 2000000.0; binary_sensor: - platform: gpio pin: GPIO2 name: "PIR Sensor" device_class: motion
Setup the Automation to Turn the Lights ON!
Once everything is working, you can move the sensor to the room it will be monitoring. Just make sure it has access to the WIFI so that it can connect and send its data to Home Assistant.
Now that it's in the room where the light level is going to be monitoring you can have it turn the lights on!
In Home Assistant:
- Click on "Configuration" in the bottom left.
- Click on "Automations"
- Click "ADD AUTOMATION"
- Give your Automation a Name
- Under Triggers, Set:
- Trigger Type to State
- Entity: You should see in the dropdown now a PIR Sensor ( something like: binary_sensor.pir_sensor )
- To: on ( must be lowercase )
- Under Conditions, Set:
- Condition Type to Numeric State
- Entity set to the new brightness sensor in the list ( something like "sensor.multisensor_brightness" )
- Below to 50 ( You will vary this setting to adjust the ambient light level you want to allow 50 works for me and my home ) Higher == More bright, Lower == More Dark
- Under Actions, Set:
- Action type to Device
- Device to whatever light you want to turn on
- Action to Turn on ... Light
- Brightness to 100
- Click on Save
Move around in the room when its dark - the lights should come on..
Turn Those Lights Back Off When There Is No Motion
We will set two lights off modes.. The first is when you leave the room, the motion stops. The lights should go out. To do this we need to create another Automation. So, in Home Assistant:
- Click on "Configuration" in the bottom left.
- Click on "Automations"
- Click "ADD AUTOMATION"
- Give your Automation a Name ( "Turn lights off after 2 minutes of no motion" )
- Trigger, set:
- Trigger type to State
- Entity to your PIR sensor again, same as last time
- To to off ( again, must be lower case)
- for to 00:02:00 ( This is the duration you want to ensure there's no motion, you can adjust as you need )
- Actions, set:
- Action Type to Device
- Device to the same light you chose in the previous automation
- Action to Turn off .... Light
Leave the room for ~3 minutes depending on your PIR sensor sensitivity and the lights should turn off
Turn Those Lights Back Off When Its Bright
Now that the lights turn on and off as expected, we need to save some money and not have the lights on when its bright in the room.. So lets turn them off when the sun shines through.
Create a new Automation, as we did in the last 2 steps
- Give your Automation a Name ( "Turn lights off when its bright" )
- Triggers, set:
- Trigger Type to Numeric State
- Entity set to the Brightness Sensor ( should be something like "sensor.multisensor_brightness" )
- Above set to 50 ( or whatever you set the value to in the first automation )
- Actions, set:
- Action Type to Device
- Device to the same light you setup in the last 2 automations
- Action to Turn off ... Lights
Thats it.. Now you should never have to use that light switch again. :D
Clean It Up and Make It Pretty
![Screen Shot 2021-01-22 at 10.09.34 AM.png](/proxy/?url=https://content.instructables.com/FYC/M71S/KK8AMITW/FYCM71SKK8AMITW.png&filename=Screen Shot 2021-01-22 at 10.09.34 AM.png)
If you have a 3D printer you can print the case for this and package it up nice.. Otherwise I find a small box and some glue can also work wonders.
If you want to 3D print it, check out my Thingiverse project here: https://www.thingiverse.com/thing:4733292
I hope it works and I hope you enjoy your newly automated home!
Build more of these and automate the world! :D