Advertisement
Electronics

Control Your Christmas Lights With a Raspberry Pi

by

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.

Related Posts
  • Computer Skills
    Electronics
    How to Build a Stop Motion Animation Studio With a Raspberry PiStopmopreview400
    Creating animated movies can be fun and easy using stop motion techniques. Stop motion movies are built from individual pictures of slight changes in a scene compiled into a video. Setting up a studio to film an adventure is a simple and fun project with a Raspberry Pi and its camera module. Read More…
  • Computer Skills
    Electronics
    Controlling DC Motors Using Python With a Raspberry PiPi motor
    In this tutorial I will show you how to connect some motors to your Raspberry Pi. Doing so will allow your Raspberry Pi to interact in the real world, making it possible to build a robot, turn on a fan on a hot day or even drop a treat for your cat or dog while your away.Read More…
  • Computer Skills
    Electronics
    Building a Wireless Sensor Network in Your HomeSensors
    Your environment is full of all sorts of fun and useful data, but getting it all together can be a challenge. In this tutorial you will learn how to use Arduino and Raspberry Pi technology to create inexpensive, wireless sensor nodes that report their data back to the Internet of Things. Read More…
  • Computer Skills
    Electronics
    How to Set Up a Real Time Business Statistics Dashboard Dashboard
    In order to be successful in business you need to make rational decisions based upon cold, hard data. In this tutorial I will show you how to set up an inexpensive dashboard that constantly monitors your business' vital statistics. Read More…
  • Computer Skills
    Electronics
    How to Control a Lamp With Voice Commands and a Raspberry PiPivoice400
    Voice control is not just for smart phones and B-movies anymore. Steve Hickson has created a system to bring automation and the intelligence of Wolfram Alpha to your beck and call. In this tutorial I'll show you how to install Voice Command, configure it, and teach it to turn a lamp on and off. The control of mains power is a tricky and dangerous subject for hobbyists so you'll be using a prebuilt solution for controlling the voltage to the lamp.Read More…
  • Computer Skills
    Electronics
    Creating a Speaker for Your Raspberry Pi Using a Piezo ElementPispeaker400
    Adding sound to a Raspberry Pi project is a simple matter of hooking up an HDMI monitor with built in speakers or attaching amplified speakers to the audio port. However, neither solution is very light or portable. Projects that require a small, energy efficient footprint don't have room for big amplified speakers. However for simple sounds, the Pi can generate frequencies on its GPIO pins that drive a piezo element to produce sounds. This tutorial will show you how to connect an inexpensive piezo electric element to your Pi to produce simple tones.Read More…