Building $8 motion and temperature sensor

Himanshu Baweja
3 min readFeb 5, 2017

--

One of the things I wanted to do was to turn on the stair lights whenever I enter the house. So i needed a motion sensor which can connect to my hub over wifi to turn the light on.

In case you haven’t heard about ESP8266, be ready to be amazed. For $3, you can now buy a development board with wifi running at 80Mhz with 11 GPIO pins (it has 17 but not all are usable). I used NodeMCU v3 for my use case but if you are fine with soldering Wemos D1 mini pro is a better option because of their smaller size and available shields.

NodeMCU — $3 Wifi SOC [1]

To NodeMCU, I connected two PIR Sensor (HC-SR501) — one in each direction since I have two doors leading to the stairs and one DS18b20 temperature sensor. Check my previous post for more details on how to use them. One thing to look out for is initially my temperature sensor was showing higher temperature than actual — turns out the sensor was too close to NodeMCU and also the length of wire connecting them was only few cms. Increasing the distance and wire length to above 20cm fixed it.

Instead of NodeMCU directly connecting to my Home Assistant hub, I used MQTT. MQTT is very lightweight publish-subscribe protocol. I ran the broker on the raspberry pi with all my sensors publishing to it and Home Assistant subscribing to the messages.

Next step was writing the code to wire up the whole system. You can Arduino IDE to write code for NodeMCU. If you prefer, you can write code in LUA also for NodeMCU. Also, unlike how I did for first few hours, you don’t need to connect the device to your laptop to program it. You can use Arduino OTA to publish the code directly to the device. My code is available at github here.

Now add the rule to Home Assistant

- alias: L1Sensor Stairs On
hide_entity: False
trigger:
platform: state
entity_id: binary_sensor.entrance_motion_1
from: 'off'
to: 'on'
condition:
condition: and
conditions:
- condition: numeric_state
entity_id: sun.sun
value_template: '{{ state.attributes.elevation }}'
below: 3.5
action:
entity_id: light.stairs
service: light.turn_on

The final step was hiding the device somewhere where my wife can’t see it ;). We have two doors leading to the stairs. I was able to hide everything below the storage bench with one motion sensor pointed towards each door. In future I plan to explore making a case to build an enclosure for the circuit instead of keeping having them in open.

House Entrance with circuit hidden under the storage bench

Total Cost = $8

I bought everything from Aliexpress. Since I had to create 5 of them, I ordered 5 of each which lowered the price. Ordering one item each will probably increase your cost to $10–$12. Also, I didn’t include cost of shipping.

  • $3 for NodeMCU
  • $1.5 for two PIR sensors
  • $0.5 for DS18B20
  • $3 for USB power supply and connecting wires and board

Where to find the items:

To find the items I used on Aliexpress or ebay, search for the text in quotes:

  • “Nodemcu v3” for the main circuit
  • “DS18b20” for temperature sensor
  • “HC SR501” for motion sensor
  • “micro usb wall charger” for power
  • “proto board” or “mini breadboard” depending on whether you can solder or not

[1] NodeMCU Image by Vowstar

--

--