Last week we built a set of beautiful Christmas tree lights. You can reprogram, and reuse these all year round. But what's cooler than beautiful Christmas tree lights? How about Christmas tree lights that change based on what's happening around the tree? Adding light sensors to your tree lights should take between half an hour and an hour from start to finish.

We put together a HIDIOT photoresistor tutorial a while back. It's really easy to add a photoresistor to your HIDIOT, but we didn't do much with it. Right now, our lights slowly cycle through blue, green, yellow, red and purple. Lets use a photoresistor to jazz up our tree and change colour patterns based on how dark our room is.

Adding a light sensor

First we need to look at how we'll physically connect our sensor. As with the photoresistor tutorial, we're going to set up a voltage divider circuit using a CdS Photoresistor and 4.7k ohm fixed resistor in series. We'll use the Analog to Digital Converter (ADC) on PB2 to sense light, and we'll modify our existing Christmas tree code to switch FastLED palette based on specific values. You'll need to tune these values for your tree.

It's best to use the code and setup from our earlier photoresistor tutorial to get an idea of possible value ranges before soldering parts to the HIDIOT. You can do it afterwards when it's all working, but it'll probably be quicker if you take measurements in advance.

Start by going back through the photoresistor tutorial to calibrate your light sensor. Make sure the Christmas tree lights are off. Use a bit of black electrical tape to form a tube around your photoresistor.

This just stops the photoresistor from being affected by the light from the tree itself. Point the photoresistor at the floor. In the photo, the photoresistor is connected to the HIDIOT. It might be easier if you follow the photoresistor tutorial at using a breadboard if you're new to soldering. Turn the room lights on and off, taking readings each time to get an idea of possible value ranges and write them down somewhere for later.

Ok, we're ready to start looking at how we wire this up!

Our voltage divider is simple, just like in our earlier tutorial. The circle colours on the picture above represent the 3 pins used by our sensor:

Red - Photoresistor (P4/N to PB2/L)

Brown - 4.7k fixed resistor (P4/M to PB2/J)

Green - Wire to PB2 (PB2/K to PB2/H)

Wire up your components on the board as shown above. Depending on how you orient your HIDIOT around the tree and whether or not you soldered the power LED, it may make sense to solder the photoresistor to the back side of the board.

Point the photoresistor down, as this stops dust collecting on the sensor. In the photo below, this is towards the USB cable that plugs into a wall wart adapter.

Once you have it all wired up, you should have something that looks a little like the above on the back, and the below on the front. This is a board that's seen more than a few projects and is a bit battle scarred, but it still works! The photoresistor in the shot above is still connected to PB2, but is on a slightly different spot on the positive voltage rail, which still works fine.

Once you have your photoresistor wired up, it's time to look at our code. We'll build on the last tutorial, which is based off the FastLED library ColorPalette example.

Updating Our Code

In our previous code, we left the ChangePalettePeriodically() function in but didn't use it, as it's a useful bit of code to look at. This time, we've changed the name to ChangePaletteFromLight() and call it to check light levels. This means that minor changes in light around a threshold level will cause palettes to bounce back and forth. You might want to make it so that this is only called once every few minutes if you notice the lights start freaking out at certain times of the day.

The ChangePaletteFromLight() function reads the ADC on PB2 and changes the palette based on the resulting value. Change the ranges to your calibrated values and see what happens when the lights are on versus when they're off. You should see the palettes change. You can easily add extra light levels if you prefer.

How does it look? If all goes well, you should have something like this.

Tuning your lights

Test your lights are changing by shining a torch on the photoresistor. If you find the lights aren't changing enough for you, you can recalibrate the lights on the tree. Just upload the code from the photoresistor tutorial and it'll print out the light level every few seconds. Once you know what light level to use, update the tree code's ldrStatus value checks in ChangePaletteFromLight()'s if statements and upload it back onto your HIDIOT. It might take a few minutes of trial and error to get the right combination of palettes and lights that you want, but your lights will be unique!

Going further

You can still use the tact switch on PB0, and with a bit of work you could use the one on PB2 too. Why not use one button to toggle whether or not the sensor palette change is called? You could also program the other button to cycle through palettes instead.

The next thing we'll do is connect our lights to the Internet via Bluetooth both for control and notification. If you've not built your lights yet, don't forget you can get up and running in under an hour following the previous post in this series.

Don't forget, it's easy build this yourself and you can get £5 off the HIDIOT Gift Pack until December 31st using the code XMAS17.

Join our newsletter

Subscribe now and get our latest blog posts, videos, tips and tricks every Thursday.

Success! Now check your email to confirm your subscription.

There was an error submitting your subscription. Please try again.

First Name

Email Address

We use this field to detect spam bots. If you fill this in, you will be marked as a spammer.