Back when life was simple and the only people wearing masks into shops were bandits or folk in fancy dress, I made an LED stair light project using a Raspberry Pi Zero. But in the three years since that time many things in the world have changed, most notably of all being the fact that the stair lights stopped working. First the SD card mysteriously became corrupted, then after acquiring a new one and installing the software and copying over the stair light code, I was able to get it working again.. for about a week. It was clear this wasn't going to be a simple fix. I had two options, take the construction I had apart completely and try to figure out where it was going wrong or do something else. I decided to do something else.
The PicoWhen the Raspberry Pi Pico was announced I really wanted to get one and do something with it, just because. No ideas were forthcoming though, but then the stair lights stopped working. I wasn't entirely sure what could be done with a Pico really, so I searched for some examples and found this list. Not only examples with Neopixels but also flashing LED, PIR sensor and a LDR sensor, all the things required for the stair project. A golden opportunity now presented itself to not only use a Pico but to redo the stairs with Neopixels too!
NeopixelsThe previous stair light project had 11 LEDs in it, but the Neopixel strip came in sections of 30 per meter. My stairs are 3.5 meters long so I'd need three and a half strips making 105 LEDs! I figured that was way too much, so I decided I'd cut the LEDs out and solder them back together with 10cm pieces of wire.
I found that blobbing a little bot of solder onto the contacts on the LED and then some on the wire and then just touching the two of them together with the soldering iron was the easiest way to solder the connections. I thought it would take ages but it only took a couple of hours or so.
The end cable for the Neopixel strip is only a few inches and that isn't long enough to go from my stairs to the cupboard where the Pico would be, so I needed to solder an extension wire onto the strip. Also soldering the pre crimped header wires onto the end of the extension meant that I could plug the wires into the headers on the Pico.
Attaching the cable to the stair was done with white duct tape between the lengths of cable. Then to finish it off topped with a length of wood.
The Pico came with two sets of header strips. Rather than soldering all 40 onto the Pico I decided to cut off individual pieces and solder just the pins I'd need to make the project work. It needs three 5v power sources so I tapped on a couple of extra headers onto the one 5v that's there, it's not pretty but it does the job. If I was being professional about it I should have taken the 5v directly off the power source before going into the Pico, but I felt the power drain would be so low it doesn't really matter.
I had the 5v power supply and power socket left over from the old project, so I just cannibalised the power socket and soldered the micro USB cable onto it.
The light sensor tutorial seemed too good to be true really, but I went along with it and plugged in the sensor as per the description. The results that I read from the sensor were very erratic ranging from 0 to 60000 in daylight and 0 to 9000 in darkness, so it worked but not terribly well. I figured I must be getting some interference by using such long wires. I searched for other examples and found this one using a voltage divider. I setup the circuit as below:
This worked so much better, giving values in excess of 50000 in light and lower than 30000 when it was darker. This I could use!
The codeIn the original project I only had white LEDs, but with Neopixels being coloured I felt it would be a waste to just use white. I thought it would be nice to blend from dark to a colour and then fade from the colour to white and then back to the colour and then fade out.
There's the stair.py module which houses a set of intensity values for the number of LEDs. Two of these are needed, one for the top and one for the bottom. This allows both top and bottom to run at the same time and the main code doesn't need to keep track of the values. The main code can just take the highest intensity value and use that to blend to a colour.
The colour fading is handled in the BlendToColour function
def BlendToColour(toSet, levelVal):
interp = 0.0
if levelVal < 1:
interp = levelVal
newCol = (colour[0] * interp, colour[1] * interp, colour[2] * interp)
else:
interp = levelVal - 1
newCol = (colour[0] * (1.0 - interp) + white[0] * interp, colour[1] * (1.0 - interp) + white[1] * interp, colour[2] * (1.0 - interp) + white[2] * interp)
pixels.set_pixel(toSet, GammaCorrect(newCol))
When the number of active LEDs reaches zero a new main colour is selected at random with:
if anyActive == 0:
colour = picColours[ random.randint(0, numColours) ]
This picks a colour from the colour list that will be used the next time the lights are triggered. As it's zero to numColours the last colour in the list will only ever be picked very infrequently, which is why I stuck orange on the end.
The endRename ledstrip.py to main.py and copy it over to the Pico, then when the power goes on the script is run and fancy lights go up and down the stairs. It was fun to do and so much easier than the previous one! Hope you like it.
Comments