##Introduction
You may have a funky LED board for your Raspberry Pi called a PiGlow.
This project gives you the opportunity to put your PiGlow to good work by programming it using Python. First you will need to set up your Raspberry Pi by configuring some files and downloading some modules. Then you can start to make individual LEDs blink, before getting them to pulse in pretty ways for displays!
##Requirements
- Raspberry Pi
- Micro USB power adaptor
- An SD card with Raspbian already set up through NOOBS
- USB keyboard
- USB mouse
- HDMI cable
- A monitor or TV
- A PiGlow
- An internet connection (Ethernet or WiFi) to download the required files
First check that you have all the parts you need to get your Raspberry Pi set up and working.
- Raspberry Pi
- Micro USB power adaptor
- An SD card with Raspbian already set up through NOOBS
- USB keyboard
- USB mouse
- HDMI cable
- A monitor or TV
###Activity checklist:
-
Place the SD card into the slot of your Raspberry Pi. It will only fit one way so be careful not to break the card.
-
Next connect the HDMI cable from the monitor (or TV) to the HDMI port on the Pi and turn on your monitor.
-
Plug the USB keyboard and mouse into the USB ports on the Pi.
-
Plug in the micro USB power supply and you should see some text appear on your screen.
-
When prompted to login type:
Login: pi Password: raspberry
In order to use your PiGlow board, there are some settings that need to be changed and some files that need to be downloaded.
Note: PiGlow is based on an IC that communicates via the i2c protocol. We need to enable i2c communication on your Raspberry Pi to allow it to work.
###Activity checklist:
-
Enable the i2c driver modules by editing the modules config file:
sudo nano /etc/modules
-
Then either add or ensure the following lines are at the end of the file:
i2c-dev i2c-bcm2708
Press CTRL and X, followed by Y and Enter to save your changes.
-
You may also need to ensure the driver modules are not blacklisted by editing the blacklist config file. Type the following:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
-
Comment out the following lines by adding a # sign at the start of the line. So:
blacklist spi-bcm2708 blacklist i2c-bcm2708
...should become...
# blacklist spi-bcm2708 # blacklist i2c-bcm2708
Then save and exit by pressing CTRL and X, followed by Y and Enter.
-
Now download and install the i2c libraries and Python support by typing the following:
sudo apt-get install python-smbus
-
Finally, reboot your Pi!
To program the PyGlow with your Raspberry Pi you will need to download and use the PyGlow Python module. Your Raspberry Pi will need to be connected to the internet in order to this.
###Activity checklist:
-
Begin by creating a folder or directory for your PyGlow files by typing
mkdir pyglow
. -
Next, change to the pyglow folder by typing
cd pyglow
. -
This folder will be empty; you can see this by typing
ls
to check the folder's contents. -
Download the Python file required by typing the following all one one line and pressing Enter on your keyboard:
wget https://raw.github.com/benleb/PyGlow/master/pyglow.py --no-check-certificate
-
This will give you a file called pyglow.py. This file is the module and will do all the hard work for you.
-
Now download
test.py
, which is a program to test that your PiGlow will work, by typing the following:wget https://raw.github.com/benleb/PyGlow/master/examples/test.py --no-check-certificate
-
Once downloaded, run it by typing:
sudo python test.py
-
The program will ask you to set a number between 0 (off) and 255 (brightest) for each colour. Type the following values:
White: 10 Blue: 20 Green: 30 Yellow: 40 Orange: 50 Red: 60 All: 1
Press Enter and you should see the PiGlow light up!
Now that you have all the files that you need for your PiGlow, it is time to create your very first glowing LED program using Python. In this step you will learn how to import the functions that you need from the modules to turn individual LEDs on the PiGlow on and off.
###Activity checklist:
-
To write your glowing PiGlow program using Python, you will need to open IDLE by double-clicking on the desktop icon.
-
Once the Python shell has loaded click on File and New Window to open a new text editor file.
-
Save this file as
FirstPiGlow.py
by clicking on File and Save As. -
Begin your program by importing the Pyglow module and the Time module:
from pyglow import PyGlow from time import sleep
-
Now initialise the module by typing the following underneath:
pyglow = PyGlow()
-
Next, set all the LEDs on the PiGlow to
0
or off:pyglow.all(0)
-
We are now going to turn on LED 1 on the PiGlow for one second and then off again by typing:
pyglow.led(1,100) sleep(1) pyglow.led(1,0) sleep(1)
-
To light up the LEDs add the following:
pyglow.update_leds()
-
Save your file by clicking on File and Save.
-
In an LXTerminal window type the following to run your program:
sudo python FirstPiGlow.py
Make sure you keep an eye on your PiGlow to see if LED 1 lights up!
###Challenge:
Now that you have LED1 turning on and off, why not see if you can turn any of the other LEDs on and off in a similar way?
In the last step, you created a program to turn LED1 on and off on the PiGlow. We can now add a loop to repeatedly turn groups of coloured LEDs on and off.
###Activity checklist:
-
Open the file
FirstPiGlow.py
in IDLE. -
Underneath
pyglow.all(0)
type the following:while True: pyglow.color("blue",100) sleep(1) pyglow.color("blue",0) pyglow.color("red",100) sleep(1) pyglow.color("red",0)
-
Save it as
FlashPiGlow.py
by clicking on File and Save As. -
In LXTerminal run the program by typing:
sudo python FlashPiGlow.py
###Challenge:
Can you create a loop where all the LED colours come on in sequence? The colours include:
- "White"
- "Green"
- "Yellow"
- "Orange"
- "Red"
- "Blue"
Finally, why not get your PiGlow pulsing for the ultimate in Raspberry Pi geek chic? We can write a simple program that uses a pulsing function from the PyGlow module.
###Activity checklist:
-
Open a new text editor file in IDLE.
-
Type the following code:
from pyglow import PyGlow from time import sleep pyglow = PyGlow() pyglow.all(0) while True: pyglow.pulse_all(150,500) sleep(1) pyglow.update_leds()
-
Save the file as
pulse.py
by clicking on File and Save As. -
In an LXTerminal window, run the file by typing:
sudo python pulse.py