Practical PIR for Home Use

by mrmfong in Circuits > Sensors

2850 Views, 23 Favorites, 0 Comments

Practical PIR for Home Use

fongpir_0.jpg

Like many of you out there working with home automation projects, I was looking to build a functional PIR sensor for automating some corner turns in my own home. Although light switch PIR sensors would have been optimal, you can't bend a corner. This project went through a couple iterations and I ran the attempts through many other available tutorials online and couldn't find one that worked for me. If you want to just get right to the making, skip ahead to step 3, otherwise continue on to step two where I'll discuss the development.

Supplies

Soldering Iron

Solder and Flux for electronics

Spare hookup wire

3D Printer

Breadboard

Basic Understanding of Hassio

Basic Arduino Programing skills

Development

pirdev_1.jpg
PIR sensors.png
pirdev_3.jpg
pirdev_5.jpg

Home Assistant is a great tool for linking some of the more complicated setups you may want. For me, getting a light in a stairway with corner was my initial interest in the project. Finding the right guide to building an effective home use PIR sensor was tricky. Sure there's a ton of easy ways to make it work, but making it energy efficient and effective for everyday use was another story. There was also the issue of latency, or how fast the light would turn on once it gets the signal. It's a tricky project once I really got into the weeds of it all. What happened was I came down to two major points on why this design was effective.

Latency

I started with ESPHome to design this sensor. It has all the bells and whistles but also a very friendly interface. Unfortunately, the ESPhome protocol and frame work is a pretty big energy user when you're counting the mWh. There's also a bit of a latency issue when the calls to turn on a light need to pass through ESPhome's bootup, Hassio, then your light controller. I found that these would end up in the range of 10 seconds. You'd already but up the stairway (or maybe you're walking very slowly since there's no light). So what became the most energy efficient and fastest way to bring a motion signal to Hassio was MQTT.

Using MQTT with a static IP made dropped the time to about less than 2 seconds. The MQTT signal would reach Hassio in-between about 800ms - 1200ms. Pretty damn good.

Battery Life

As mentioned before, switching to MQTT also saved a lot on the energy usage. The average sensor with no deep sleep on ESPHome would last less than a day on about 800mWh batteries. With deep sleep, about 3-5 days depending on activation. The WeMos D1 Mini is not a crazy energy hog, but it also isn't the most efficient at managing its power, so squeezing every bit of battery was important. Reducing every consuming part was the most important step.

Many PIR sensors exists but not all are created equal. One of the first points that I noticed was the rate, angle, and firing rate of each PIR sensor tested. Of the sensors used, I found the Simplytronics Wide Angle PIR to be the most effective with range and energy cost. It is a wide angle PIR sensor with excellent range, and it only runs on 3v which is absolutely amazing for what I needed.

Materials

fongpir_1.jpg

WeMos D1 Mini

T4056 Lipo/Li-Ion USB Charger

Simplytronics Wide Angle PIR Sensor

3.7v 1000 mWh Lipo Battery

2x 10k Resistors

120K Resistor

5k Resistor

1N4001 Rectifier Diode

1uF Capacitor

2N2222 Transistor

Base Code and Arduino

arduino setup.jpg
Hassio MQTT.jpg
hassio setup 1.png
template binary sensor.jpg
template sensor.jpg
dashboard sensor.jpg

As an easy go, download the arduino file modify it to work with your setup. The most important aspects to keep in mind is to make sure your settings match what is given in Hassio.

In my example, I'm using the Mosquitto Broker. I've entered those settings into my arduino code. For my MQTT server, since its hosted in Hassio, I put the IP address of my Hassio.

Next thing we'll need to do is set up some template sensors to hold our MQTT data so it's a bit more Hassio front end friendly. If you'd like to know more about templates and templating, I'll drop this Hassio link here.

Our motion MQTT will be a template binary sensor and our battery levels will be a sensor in Hassio.

In my main configuration.yaml file I've added some lines to include both template binary sensors and template sensors in separate yaml files. You don't have to do it this way but I find it keeps things a bit more organized. To do this simple use the file editor to create a new yaml file and title it something you can reference in the configuration.yaml. In my example I use templatesensor.yaml and templatebinarysensor.yaml

The thing to make sure is to setup the MQTT topics and payloads to match your arduino setup or vis versa.

Last but not least, setup a dashboard item that can see the battery levels and motion sensor.

Schematic and Testing

WeMos D1 Mini PIR.png
fongpir_4.jpg
fongpir_8.jpg
fongpir_5.jpg
pirdev_2.jpg
fongpir_7.jpg

Following the wiring schematic, wire up the components for testing on a breadboard. Important notes in the wiring is to make sure you have the ground wires correct for the pull-down effect. This is what will make the transistor reset the WeMos D1 Mini on wake. You should be able to test the wake and reset function by plugging the WeMos D1 Mini to a usb port. It should reset once you wave you hand in front of the PIR. This is optional but you can also desolder the smd led lights from the motion sensor to squeeze a bit more battery life. I would recommend doing this after you have tested that the motion sensor works as expected. If you have your USB plugged into your computer, double check it with the arduino IDE that boots and resets with a trigger from the motion.

In your Hassio Dashboard you should be able to see some values from the battery and also the motion sensor going off. If all has gone well so far you should be in business! You could take this little breadboard prototype and move it around your house and it will function as your new homemade motion sensor. You could use this to trigger anything within Hassio, and you'd be done here if that's all your looking for. But let's give this a final polish to be something that's worthy of a mainstay in the home.

Some troubleshooting tips

- by hitting the reset button on the WeMos D1 Mini you should get the MQTT to trigger with the arduino code

- uncomment some of the arduino code to see where each step is, and what it's doing to the hardware

- don't forget to connect all the negative lead points

Connecting to Your Wifi Light Switch

hassio automation 1.jpg
hassio automation 2.jpg

Thankfully Hassio has a really great automation wizard that can help with your setup. I'm not going to go into adding lights or add-ons, but I will saw that the people at Hassio have made it really easy to add integrations and other platforms to be controlled with Hassio. Go over and check out how to add your wifi light switch of choice.

In this automation wizard we want to pay attention to one important thing, which is the trigger. You could add the template binary sensor as the trigger, but I found the motion sensor was a bit more "snappy" when I went directly with the MQTT payload. Last but not least, configure your choice of light or device and the sensor should be in business.

Project Housing

fongpir_9.jpg
fongpir_0.jpg
motion case.jpg
3m strip size.jpg

Once you're confident with your breadboard, move all the parts on to a prototyping pcb board and solder all the connections to the smallest board you can find. I've kept the wires short, but flexible in case of recovery/editing/repairing. The case design is a minimal case that can be inserted into a corner or flat surface. It also works really well with the non-damaging adhesive 3M strips =)

Note I kinda forgot where I got this weird format prototyping pcb, so I'd suggest just cutting down your pcb to size and drilling a hole or two. If this guide ends up being popular, I'll release an edited version with a more common size(I only needed two motion sensors, and I had exactly two of those weird boards)

Closing

stairway location.jpg
pir in the wild.png

I hope this design has been helpful in your endeavors to get some home automation projects going. It was quite a long haul for me to get all the moving parts to produce this instructable but I'm glad I took some time to get it down. This project showed me a bit of the limits of using some of the more approachable avenues for programing my ESPs. This isn't to say you shouldn't use ESPHome, but for projects that are more strict in their energy management, you may need to go a different route. The sensors were finished around May or June and haven't needed a charge since. So far they've gone about 4-5 months without needing a charge. As a side note, I've also gone into developing a new PCB layout based off the WeMos D1 Mini. The thing about the WeMos D1 Mini is that it has a built in 5v to 3v convertor and a power hungry USB programing IC. This means that if we eliminated these two factors, we could push the ESP8266 to suck up even less power.

Once again thank you for indulging me in my ramblings and following along in this project.