Sunday 13 December 2015

Christmas Lights with Raspberry Pi and GPIOZERO

We were in Wilco yesterday and spotted some battery powered LED xmas lights for the bargain price of £2.50. They seemed ideal for a bit of festive hacking

There were a few types available and I picked up 3 different ones: warm white LEDs on a flexible but fairly rigid wire, some brighter white LEDs encapsulated in a snowflake housing and some traditional multi-colour LEDs.

The strings of LEDs come attached to  two AA cells in a neat enclosure with an on/off switch. I snipped the wires fitted jumper wire housings to the end of each so I could plug them straight onto the GPIO header. As the LEDs run on 3v and I was intending to drive them using an 'expendable' model B, I decided not to bother with a series resistor. 

I kept the batteries and the holders - these are bound to be useful for other projects (similar boxes can cost almost as much as the whole LED set).

Then I knocked up a quick Python program to run a mixed illuminate sequence for each of the lights. 

from gpiozero import PWMLED
from time import sleep
import random
import threading

white = PWMLED(23)
snowflakes = PWMLED(18)
colours = PWMLED(25)

LIGHTS = [white,snowflakes,colours]

def gentle(lights):
    for x in range(1,100):
        lights.value = x/100

def randomblink(lights): 

def sequence(lights):
    while True:

for i in LIGHTS:
    t = threading.Thread(target=sequence, args=(i,))

I then added a line to the /etc/rc.local file on the Pi so that the Python code runs automatically at startup.

Our Christmas tree was already quite covered with decorations so I decided to use these new lights on our spiky cactus!

Friday 11 December 2015

BOM for sub-£15 Raspberry Pi robot

A few people on Twitter asked for a BOM for my CodeClub sub-£15 robot kit. So here it is:

Chassis, wheels, motors and battery pack - £6.57

L9110S DC motor driver board - £1.72

Power bank - £2.67

HC-SR04 ultrasonic distance sensor - £0.99

I already have enough Pis, cases, breadboards and leads but for completeness if you were starting from nothing:

mini breadboard - 5 for £2.09
M-F jumper leads - £0.89
F-F jumper leads - £1.05

For the Pi itself, any model will do. I'm planning on using our original model Bs but if you were buying just for this project I'd go for model A+s or o Pi Zero.

In many cases you can get the components even cheaper (per unit) if you're buying larger quantities.

The only other thing you'll need is AA cells to power the motors and (optionally) a wifi dongle if you want to remotely control the robot.

In oder to fit everything onto the chassis shown above I had to drill some extra holes, mainly to accommodate the Pi in its PiBow case and allow the power bank to be slung underneath.  Although this worked fine, I wanted to make things simpler so I've been developing a 3D-printed chassis that has slots for the main elements and also allows the Pi (the heaviest part) to sit more towards the centre.

My plan is that over the holidays I'll finish off some of the activity plans for building this in CodeClub next term and share them online.

Tuesday 1 December 2015

Raspberry Pi Advent Calendar with SenseHAT

We missed out on a Lego Advent calendar this year so I decided to code up one for the Pi and the SenseHat.

The top 6 rows of the LED matrix are used to show the days on the month, each one being represented by 2 LEDs . Every day a new pair of LEDs illuminates and you can then select them using the joystick to navigate (your cursor is a pair of bright red LEDs). Pressing the joystick as a button will then display the day number followed by a Christmas image or animation created by my son Ozzy.

To make it funky, each day the grid redraws itself with different coloured LEDs in each position.

Everything you need is in this Github repo. Clone it and then run

Because the images are just 8x8 bit, they can take a little bit of imagination to work out (the Dec 1st is a snowball fight, before you ask!). You can, of course use your own content for each day. The static pictures should go in the pngs folder: name them decx.png, where x is the day. Animations should be added as a list of frames to  (follow the format of the existing ones).

You can use my 8x8grid drawing program to create your images and animations.

I wrote this in a bit of a rush so let me know if you spot any bugs surprise features!