Controlling LEDs With The Ciseco Slice of Pi/O - Part
In Part 1 I showed you how to set up a Slice of Pi/O add on board for the Raspberry Pi, which we then tested in Part 2 by getting an LED to light up using one of the additional outputs the board supplies.
In Part 3, I'll show you the Slice of Pi/O being used to its full potential - using all 16 outputs to drive LEDs as a prototype for my sports scoreboard project.
An army of LEDs! |
What you will need:
- 1 x large breadboard (830-point or similar)
- 16 x LEDs
- 16 x resistors for the LEDs
- 1 x 10k resistor for the switch
- 1 x 4-pin header (optional)
- 2 x 8-pin headers (optional)
- 1 x Tactile switch
- 20 x male-to-male jumper wires
Check out Part 2 if you want to use the same items that I use here.
Before we start
As always, turn off your Pi (you can use sudo halt from Terminal, or just do it the GUI way)
Solder the headers
If you want to solder your power and GPIO jumpers directly to the Slice of Pi/O, then technically you can skip this step. However, I like to add the headers to the breakout holes of my Slice of Pi/O to make prototyping easier.
I'll use the 4-pin header as an example. Your 4-pin header should look like this:
4-pin header |
And here is where it goes on your Slice of Pi/O:
Enter the 4-pin header here |
View from the back |
Pop the header through the holes and solder away (best find a soldering blog if you want to learn how to solder):
Soldering the 4-pin header |
NOTE: I also soldered the GPIO holes into headers too - same method:
GPIO headers added as well |
Connect the parts
Now that your Slice of Pi/O's header is soldered, it's time to dive right in and connect all of the parts together.
I could take 20 pictures and try and explain it piece by piece, but this time I think a well annotated video may the better way to do it. The video quality isn't great but the text should help you along:
All we have done here is:
- Link up the PWR and GND pins to the breadboard power lanes
- Added 16 LEDs to the centre split on the board (positive leg upwards)
- Linked the positive LED legs to each MCP23017 output in order
- Linked the negative LED legs to the GND supply with a suitable resistor in line
- Added a tactile switch linked to GPIO 25 to act as our 'goal' button
All parts connected
What it should look like |
By watching the video, paying attention to the video comments and looking at the pictures in this blog, you shouldn't have any problems putting this together. If you do, drop me a comment and I'll add more detail.
The Python code
As I'm still in the prototype stage, this code isn't the full scoreboard experience - not even close. The end result will have 2 different score lanes and sets of buttons for each team, with 'minus' goal buttons in case of mistakes. I may also add power-play indicator LEDs, a timer and other features.
What this code does do is confirm all 16 outputs work, the button to register a new goal works, and that we can make some kind of order to the lights so that it will act like a scoreboard. It's a great starting point, and the code doesn't need to get too complex.
On each button press, my code will light one LED after another until it hits the last (16th) LED. After this, another press will (pointlessly) flash each LED back down to the 1st LED and then all lights will go out - I guess in my head this is some kind of 'game over' sequence!? I'm not the most efficient Python writer...in fact I'm pretty sure there are much better ways of doing this, but it does work, I did write it, and that's good enough for the 'Average Man'!
Any suggestions or improvements are welcome:
--------------------------------------------------------------------------------------------------
#Imports
import smbus
import sys
import getopt import time
import RPi.GPIO as GPIO
#Set GPIO mode
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
#Set GPIO pin for switch
TEAM1 = 25
#Set GPIO name and input/output
GPIO.setup(TEAM1, GPIO.IN) #Team 1 score button
bus = smbus.SMBus(0) #for revision 2 board...use 1
#Set the address (can check using i2cdetect -y 0 )
address = 0x20
# Set all banks to outputs bus.write_byte_data(address,0x00,0x00) # Set all of bank A to outputs
bus.write_byte_data(address,0x01,0x00) # Set all of bank B to outputs
#gives each MCP pin a name led0 = 0
led8 = 128
def set_led(data,bank): #means bank1 or bank0...simple
#print "set_led bank=" + str(bank) + " data=" + str(data)
bus.write_byte_data(address,0x12,data)
bus.write_byte_data(address,0x13,data)
return
set_led(led0,bank0) #clear LEDs
set_led(led0,bank1) #clear LEDs
count = 0 #initially start 'count' at zero aka no LEDs lit
if ( GPIO.input(TEAM1) == True): #aka if button clicked
set_led(led1,bank0)
set_led(led2,bank0)
set_led(led3,bank0)
set_led(led4,bank0)
set_led(led5,bank0)
set_led(led6,bank0)
set_led(led7,bank0)
set_led(led8,bank0)
set_led(led0,bank0) # turn off other bank
set_led(led8,bank1)
set_led(led7,bank1)
set_led(led6,bank1)
set_led(led5,bank1)
set_led(led4,bank1)
set_led(led3,bank1)
set_led(led2,bank1)
set_led(led1,bank1)
if ( count == 17): #end 'game over' sequence
set_led(led2,bank1)
set_led(led3,bank1)
set_led(led4,bank1)
set_led(led5,bank1)
set_led(led6,bank1)
set_led(led7,bank1)
set_led(led8,bank1)
set_led(led0,bank1)
set_led(led8,bank0)
set_led(led7,bank0)
set_led(led6,bank0)
set_led(led5,bank0)
set_led(led4,bank0)
set_led(led3,bank0)
set_led(led2,bank0)
set_led(led1,bank0)
set_led(led0,bank0)
--------------------------------------------------------------------------------------------------
The End Result
Here's a video showing the code and LEDs in action:
Considerations
You might be thinking "Why doesn't the Average Man keep the previous LEDs lit after each goal"...
Good question, and one I did look in to.
From the Google research I did, it seems that this chip has a limitation on the amount of current it can handle, and powering 16 LEDs at once would most likely cause something to go 'pop'. I'm not entirely educated on that part yet, but I'm looking into transistors to make use of an external power supply for the LEDs.
So if you decide to take this and modify it for your own use, keep this in mind.
Part 3 Complete!
If all went to plan (comment if not), you should be seeing the same result as the video - an early primitive prototype that can record a score.
Whilst I will be continuing this project over time, this series of posts will end here as you now have the knowledge and starter code to help you on your way with whatever you will build on the Slice of Pi/O.
I'll post an update later in the year once I complete the project. I would love to hear from anyone who has used this to help them, to see what projects people are using this for.
Here's a little hint of what one of my next blogs might cover
2 x MCP23017 chips...watch this space... |
Continue reading here: The MicroUPS - An Uninterruptible Power Supply for the Raspberry Pi
Was this article helpful?
Readers' Questions
-
Amos4 months ago
- Reply