Displaying Messages on an I2C LED matrix
You want to control the pixels of a multicolor LED matrix display.
Use an I2C LED module, such as the model shown in Figure 13-3 attached via a breadboard to a Raspberry Pi. Note that the LED in the breadboard layout looks a little different to the one in the wiring diagram, but their pinouts are the same.
To make this recipe, you need:
• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)
• Adafruit bicolor LED square-pixel matrix with I2C backpack (see “Modules” on page 381)
Figure 13-4 shows the arrangement of components on the breadboard. In fact, if you do not have any buttons or extra components to add, you could just use female-to-female jumper wires and connect the module directly to the Raspberry Pi.
For this recipe to work, you will also need to set up your Raspberry Pi for I2C, so follow Recipe 8.4 first.
The display has an accompanying Python library written by Adafruit. It’s not installed as a proper library, so to use it, you first need to download the folder structure. If you don’t have Git installed, install it now with the following command (see Recipe 3.19):
$ sudo apt-get install git |
Now, you can download the folder structure from GitHub:
$ git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git |
Change directory into the Adafruit code using:
$ cd Adafruit-Raspberry-Pi-Python-Code $ cd Adafruit_LEDBackpack |
In this folder, you will find a test program that displays the time as scrolling digits. Run it using the command:
$ sudo python ex_8x8_color_pixels.py |
Discussion
The program cycles through all the colors for each pixel in turn. The code is listed here, with some of the comments and an unnecessary import removed:
import time from Adafruit_8x8 import ColorEightByEight grid = ColorEightByEight(address=0x70) iter = 0 # Continually update the 8×8 display one pixel at a time while(True): iter += 1 for x in range(0, 8): for y in range(0, 8): grid.setPixel(x, y, iter % 4 ) time.sleep(0.02) |
After the import, an instance of ColorEightByEight is created.
The address supplied as an argument to the next line is the I2C address (see Recipe 8.4):
grid = ColorEightByEight(address=0x70) |
Every I2C slave device has an address number. The LED board has three pairs of solder pads on the back that can be bridged with solder if you want to change the address. This is essential if you need to operate more than one of the displays from a single Raspberry Pi.
The variable iter gets 1 added to it each time through the loop. The command grid.setPixel takes x and y coordinates as its first two parameters. The final parameter is the color to which the pixel will be set. This is a number between 0 and 3 (0 is off, 1 is green, 2 is red, and 3 is orange).
The variable iter is used to generate the number between 0 and 3 using the % operator, which is the modulo remainder (i.e., what is left over when you divide iter by 4).
See Also
You can find out more about this product at http://www.adafruit.com/products/902.
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |
Table A-8. Modules | |
Raspberry Pi camera module | Adafruit: 1367, MCM: 28-17733, CPC: SC13023 |
Arduino Uno | SparkFun: DEV-11021, Adafruit: 50, CPC: A000066 |
Level converter, four-way | SparkFun: BOB-11978, Adafruit: 757 |
Level converter eight-way | Adafruit: 395 |
LiPo boost converter/charger | SparkFun: PRT-11231 |
PowerSwitch tail | Adafruit: 268 |
16-channel servo controller | Adafruit: 815 |
Motor driver 1A dual | SparkFun: ROB-09457 |
RaspiRobot board | Sparkfun: KIT-11561, raspirobot.com |
PiFace digital interface board | MCM: 83-14472, CPC: SC12827 |
Humble Pi | MCM: 83-14637, CPC: SC12871 |
Pi Plate | Adafruit: 801 |
Gertboard | MCM: 83-14460, CPC: SC12828 |
Breakout board with paddle terminals | MCM: 83-14876, CPC: SC12885 |
PIR motion detector | Adafruit: 189 |
Venus GPS module | SparkFun: GPS-11058 |
Methane sensor | SparkFun: SEN-09404 |
Gas sensor breakout board | SparkFun: BOB-08891 |
ADXL335 triple-axis accelerometer | Adafruit: 163 |
4×7-segment LED with I2C backpack | Adafruit: 878 |
Bicolor LED square-pixel matrix with I2C backpack | Adafruit: 902 |
PiLite interface board | Ciseco, CPC: SC13018 |
aLaMode interface board | Makershed: MKWY1, Seeedstudio: ARD10251P |
Freetronics Arduino LCD shield | www.freetronics.com |
RTC module | Adafruit: 264 |
16 x 2 HD44780 compatible LCD module | SparkFun: LCD-00255, Adafruit: 181 |