Autonomous Lights With Raspberry Pi
By: Vikram Ramkumar
Tags: Raspberry Pi, Software Development, Philips, Autonomous Lights, Philips Hue

Photo by Johannes Plenio on Unsplash

I began this year like most other people, anticipating another typical year of my life. I had my goals at work, and I planned to ski every weekend of the winter, and rock climb my hardest.

2020 had different plans, and I got none of those goals done properly much like most of us. Quarantining and isolating is still miserable, but one good thing came from it. I decided to try my first Raspberry Pi project!

What I Built

Autonomous lights for my home office!

An LED light strip under my desk adds ambient light.

When people walk into the room, the lights under my desk turn on. When the last person leaves, the lights turn off.

This involves two laser circuits running off a wall socket, with the lasers pointing at horizontally aligned light sensors across a walkway. The light sensors are connected to a Raspberry Pi, which continuously monitors fluctuations from these sensors.

Why are there two sensors? Because a direction of travel is established based on which of the two sensors is triggered as being “dark” first. Triggering the sensors in one direction is understood by the service as a person entering the room, and triggering them in the other direction is understood as a person leaving.

This directional triggering is tracked as a count of people in the room, which then triggers an action from the lights if there is a need.

GitHub repo

What I Used

Philips Hue Lightstrip

Philips Hue Hub

Raspberry Pi 3

1 Multimeter

Voltage regulator

2 laser diodes, 5V 650nm 5mW

Capacitors

3 Breadboards

A bunch of breadboard wires

A bunch of resistors

2 light resistors

1 Micro-USB breadboard adapter

Micro-USB to wall plug charger

Waiting for all that to arrive during a pandemic took way too long!

Fixing The Lights

I love soft, ambient lighting. I stuck the Philips Hue light strip under my desk to provide a glow to the room when it turns on.

The light strip is stuck in an arching shape around the bottom of my L-shaped desk.

Philips provides an app to toggle your lights, but I decided that was too much work. It would be vastly better if the lights could turn themselves on and off.

Phase 1: Building The Circuits

As a software engineer, this was the hardest part for me. Building a basic circuit is a hilarious ordeal when you’ve only ever had to think about electricity in conceptual terms. Many steps of the process induce reactions like “Whoa! That works, look how cool it is!”

Why couldn’t my college professors teach us to build basic circuits in all those physics classes? Strange.

Lasers hiding in plain sight, sensors not so much yet.

The Laser Circuits

I followed this guide to build my laser circuits. I made two separate circuits for each laser diode and connected them in parallel to a power source. I stuck the breadboard on my wall where it would be hidden from plain sight positioned the lasers pointing across the walkway..

What a mess!

The Sensor Circuits

I followed this guide on creating the light sensor circuit and connecting it to the Raspberry Pi.

I used the same breadboard to create two separate light sensor circuits.

I hid the Raspberry Pi and light sensor breadboard away from plain sight. I extended the wires to sensors to position them exactly where the LED lights fell on the other side of the walkway.

I know, it’s a bit of a mess. But that can be improved or hidden easily.

And that finished the hardware rigging part.

Phase 2: The Programming

I setup my Raspberry Pi in a “headless” mode using this guide, meaning I didn’t set up a GUI on it and ran it through terminal instead. In my case, I felt that this project was simple enough to accomplish through the terminal.

First things first, there needed to be a way to connect to the Philips Hue lights from the Raspberry Pi. There’s an excellent guide on how to do this here.

After setting it up and messing around with HTTP requests, I hooked up the code to the light sensors connected to the Raspberry Pi’s GPIO pins. All this brought me to the code shared in my GitHub repo here. These two files together detect triggers from the sensors and toggle my desk lights correspondingly.

I placed these files inside a folder in the Raspberry Pi directory system. But this code won’t run itself. Something needs to run it to keep track of people entering and exiting the room.

After weighing my options on what I needed, I decided to use the systemd approach to set up a service that runs the entire time the Raspyberry Pi is on. The code for this service is in the same GitHub repo here.

Done!

Final product

Problems

Okay, so the project is technically done but it’s not perfect. There are some basic things that became very annoying in practice:

The laser beams keep moving over time because my condo is made of wooden construction, which slightly adjusts with traffic and temperature changes. It’s enough movement that the beams fall off the sensors, which then requires re-adjustment. The light sensors are unreliable. Watching their output on my Pi logs reveal that they incorrectly report darkness at random intervals. I adjusted the values as much as I could, but I was only able to mitigate the issue instead of eliminating it.

For technical context: The Raspberry Pi API allows you to set a threshold for what value is considered dark between 0.0 and 1.0. If the average value of the last few reported values drops below the threshold, the Pi considers it to be dark.

My light sensors were incorrectly reporting low average values. To combat this problem, I set the darkness threshold to 0.01. The sensors are set at waist height because we have a dog who should not be able to trigger the lights. So it turns out that hand movement while walking causes some significant chance of error with the sensors. A hand that goes in front while entering can block the father sensor first, making the system flip the direction of travel.

This combination of issues leads to mild chaos.

I’ve woken up on many nights to see the lights on. So now I disconnect the system every night and turn it back on in the morning.

The sensors misjudging my direction of travel even once in a day undermines the usefulness of the system altogether.

And I’m tired of adjusting the sensors.

Conclusion

Great project, not yet great in practice.

I may need to rethink the tools I’m using for detection. Or maybe the way I’ve connected the wires at length is the reason the sensors glitch.

I could also try out a machine learning model and hook up a camera on the room to detect presence.

I loved making this system and I learned a good amount. I’d love questions, feedback, or suggestions on this. If you know what I did wrong, help me! Please reach out to me through any means listed on my website.