Hostingheaderbarlogoj
Join InMotion Hosting for $3.49/mo & get a year on Tuts+ FREE (worth $180). Start today.
Advertisement

Control Your Christmas Lights With a Raspberry Pi

by
Gift

Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.

In this tutorial I will show you how to control five sets of Christmas lights with a Raspberry Pi and the PiFace I/O expansion board. Since the board is pre-assembled this project doesn't require any soldering.

You'll be able to control the lights with a simple Python program, which is easily modifiable for different patterns or triggers. You could also setup your Pi for remote control and be able to control your Christmas lights with an iPad or mobile phone.


Prerequisites

You'll need

  • A Raspberry Pi (Model A or B) running Raspbian
  • PiFace I/O board
  • A set of of 5 volt LED light strings (the board can control up to 8 sets of strings, 4 or 5 is recommended)
  • A set of terminal blocks
  • A wire cutter / stripper tool
  • A basic understanding of the console and running Python scripts

Setting Up the Raspberry Pi

First you will need to make sure your Raspberry Pi is up to date. On the Raspberry Pi command line (the first thing you see when you boot up) type:

sudo apt-get update
sudo apt-get upgrade
Tip: you can follow the steps in the tutorial, ‘Take control of Your Raspberry Pi using you Mac, PC iPad or Phone’ to set up your Raspberry Pi to work over your home network and provide it with a static IP address, then you will be able to remotely control your lights over your home network.

Then you will need to install the software to communicate with the PiFace board.

First you need to enable the SPI interface -- this is easily done using the Raspberry Pi configuration program which runs the first time the Raspberry Pi boots -- or by by typing into the terminal:

sudo raspi-config

This will load the blue setup screen. Go to Advanced options then choose SPI Enable/Disable automatic loading of SPI kernel module which is needed for PiFace.

if you don't see this option, choose the Update option on the Advanced Options menu to update your copy of raspi-config.

Next install a set of tools that include the graphical control panel -- in the terminal type:

sudo wget -O – http://pi.cs.man.ac.uk/download/install.txt | bash

Finally you need to install the Python library which will allow you to control the lights using the program:

sudo apt-get install python{,3}-pifacedigitalio

Once you've installed the software, shutdown your Pi with

sudo shutdown -h now

Installing the PiFace Board and Testing

The PiFace board plugs into the GPIO pins directly on top of the Raspberry Pi. Ensure the Pi is powered off before connecting the board.

Restart your Pi and use the  graphical interface to test out the board -- you'll need to be running the Pi desktop to do this:

startx

Open LX terminal and type:

~/piface/scripts/piface-emulator

This runs the PiFace Emulator:

PiFace emulator
The PiFace emulator window

Click Overide Enable and then on the Output Pin 0 to 7 buttons to test the board -- you should see the LED lights illuminate on the board. You can also test the push button sensors and relays on the board.


Setting Up Your Christmas Lights

The PiFace board provides a five volt power supply from the Raspberry Pi, or can switch up to 20 volts through the two relays on the board. In this example five sets of lights are being switched.

There are a number of ways you can source your LED light strings:

Five-volts is the same voltage as used by USB connectors, so any USB powered LED lights will work. Cut off the USB connectors and wire the lights to the board.

Alternatively you can use battery powered LED light strings which are available cheaply online. I found that 20 LED light stings powered by three AA batteries worked without problem. Most LED strings have resistors soldered inline already.

Or you can make up your own LED strings by purchasing wire, resistors and LEDs. The LED parallel wiring wizard helps calculate the resistors you'll need to use with a five-volt power supply.

Once you have your lights use a five-volt power supply to test your LED strings, as LEDs are unidirectional you will need to label the positive and negative wires.

Note: never use mains voltage lamps with the PiFace board.


Connect Your Christmas Lights to the PiFace Board

To connect your LED strings to the PiFace board wire all the positive wires together to the black five-volt terminal (the red wire in the photo below), you can use a terminal block and jumper wires to attach all the wires together.

Wire the negative wires (one to five) to the orange terminal blocks

PiFace Christmas lights wiring
PiFace Christmas lights wiring

Finally power up the Raspberry Pi and run the PiFace emulator again. You should be able to switch the lights on and off.


Run a Simple Christmas Lights Python Program

In addition to using the PiFace emulator you can write programs in Python 3 to control the board. The following is a simple program that switches the lights on and off in sequence:

from time import sleep
import pifacedigitalio

DELAY = 0.5  # seconds

if __name__ == "__main__":
    pifacedigital = pifacedigitalio.PiFaceDigital()
    while True:
        pifacedigital.leds[7].toggle()
        sleep(DELAY)
        pifacedigital.leds[6].toggle()
        sleep(DELAY)
        pifacedigital.leds[5].toggle()
        sleep(DELAY)
        pifacedigital.leds[4].toggle()
        sleep(DELAY)
        pifacedigital.leds[3].toggle()
        sleep(DELAY)

To enter this program open a text editor, or in the console type

sudo nano xmaslights.py

and cut and paste the code above. Save and exit.

Then run the program with:

python3 xmaslights.py

You can alter the timing by changing the DELAY variable, and control additional outputs by using the pifacedigital.leds[number of the output] line.


Summary

You now have a set of computer controlled Christmas tree lights. You could try experimenting with the code, change the pattern of the flashing, or add triggers for events using the inputs on the board.

This tutorial covered the basic setup and testing of the PiFace I/O board, and it's use to control festive Christmas lights.

Advertisement