The easiest way to get started with the installation and initialising the RGB LED is to follow the instructions on this page: http://www.piborg.com/ledborg/install. (!!!) Please make sure you attach the LedBorg onto the Raspberry Pi when it is not running, because otherwise the Pi will reset and this might be harmful for the device. The reason why it`s resetting is the presence of a decoupling capacitor between +5V and GND, which initially charges from the 5V power supply and causes the voltage to drop slightly, but enough to make the Pi not draw enough power which makes the device reset.
I found the light coming from the LED quite strong to watch with direct eye, instead cover the LED with something thin which will ensure the colour can still be seen (like a piece of paper/plastic) but will diminish the light intensity. Note that, even though at the end of the installation you get a message saying “LedBorg should now be green”, you might get a different colour or even no colour on your LED (I got no colour) depending on the value stored in the “ledborg_bootcolour” found in your home directory. You can manually change this by typing echo “RGB” > /home/pi/ledborg_bootcolour and replacing RGB with your desired level. The way setting the RGB level works is explained at the link above, in the Usage section.
Examples
There are few simple examples on the piborg website which can get you started with a sequence of lights or let you adjust the light from the keyboard directly. The three examples we will look at can be run with the following commands (in terminal):
cd ~
wget -O *.py http://www.piborg.org/downloads/ledborg/*.txt
chmod +x *.py
~/*.py
making sure to replace “*” with the following:
1)
random-colours
(this will open up a script that picks random colours at regular intervals and displays them on the LED)
2)
sequence
(script to run a preset sequence of colours at a regular interval)
3)
user-colours
(script providing a user menu in which the user can manually adjust the light that is displayed on the LED).
Pick up colour using a Camera
The Raspberry Pi Camera Board described in an earlier post on this blog can be used to capture images which will then be used to
change the colour of the LedBorg accordingly. The code loads the images into the program, focuses on the center of the image and uses some image processing tools to convert the colour into LedBorg code and set the LedBorg to that colour.
The program called ChameleonPi can be found on the piborg website
at
:
piborg.org/chameleonpi
To get this program working, first install the python imaging libraries:
sudo apt-get instal
l
python-imaging.
Now create a new blank python script with the command
sudo nano ChameleonPi.py,
and copy paste the following code in that script:
#!/usr/bin/env python
# coding: latin-1
# Import library functions we need
import time
import os
import threading
from PIL import Image
# Setup for ChameleonPi
imageFile = '/tmp/chameleon-image.jpg' # Image file to read in for processing
imageCommand = 'raspistill -t 0 -o ' + imageFile # Command used to create image file
interval = 1 # Seconds to wait between updates
# Set the LedBorg colour by LedBorg code
def SetColour(colour):
LedBorg=open('/dev/ledborg','w')
LedBorg.write(colour)
LedBorg.close()
# Convert a single 0-255 channel into an LedBorg 0-2 code
def ChannelToCode(c):
if c < 85:
return '0'
elif c < 171:
return '1'
else:
return '2'
# Set the LedBorg colour by 0-255 RGB values
def SetRgb(r, g, b):
ledCode = ChannelToCode(r) + ChannelToCode(g) + ChannelToCode(b)
SetColour(ledCode)
# Class for the user interaction thread
class WaitForUser(threading.Thread):
# The code which will be run when the thread is started
def run(self):
global waiting
# Wait for the user to press enter
tempString = raw_input()
# Set the running flag and finish
print 'Please wait...'
waiting = False
# Setup status flags
global waiting
waiting = True
try:
# Setup a thread to wait for user input
print 'Press ENTER to end the program'
WaitForUser().start()
# Run until interrupted
while waiting:
# Call the external command to generate a photo
os.system(imageCommand)
# Read in the photo
photo = Image.open(imageFile)
# Crop out the center section
width, height = photo.size
x1 = int(width * 0.4)
x2 = int(width * 0.6)
y1 = int(height * 0.4)
y2 = int(height * 0.6)
cropPhoto = photo.crop((x1, y1, x2, y2))
# Average the cropped image down to a single colour
tinyPhoto = cropPhoto.resize((1,1), Image.ANTIALIAS)
r, g, b = tinyPhoto.getpixel((0,0))
# Scale RGB such that the highest channel is fully on
scale = 255.0 / max(r, g, b)
r *= scale
g *= scale
b *= scale
# set the LedBorg colour
SetRgb(r, g, b)
# Wait a while
if waiting:
time.sleep(interval)
print 'All done :)'
SetColour('000')
except KeyboardInterrupt:
# CTRL+C exit, turn off the LedBorg
print 'Terminated'
SetColour('000')
Save the python script with
Ctrl+x
without changing the name and make sure you give the right permission by making the file executable, typing the following command:
sudo chmod +x ChameleonPi.py
Make sure you have the LedBorg connected and working and also the camera board connected and working (see http://blog.soton.ac.uk/pi/modules-available/raspberry-pi-camera-board/). Now open up the program by typing
./ChameleonPi.py
and see how it works by pointing the camera close to different objects of different colours. The video from the piborg website above uses a Rubick`s cube but you can use any
slightly uniformly coloured
object to give good results.