8x8_led_matrix_raspberry_pi_setup.jpg

 

 

One of the most accessible projects you can try with your Raspberry Pi is to learn about the communications features of the expansion header. The Raspberry Pi has 26 pins which carry signals ranging from GPIO (which can be configured in software) to +5V,  +3.3V and ground. There is a full description of each of the pins on the elinux site which explains which ones can be reconfigured to suit your needs.

 

Perhaps the easiest way to control fairly complex external modules is to use the I2C, SPI or UART communications standards. These give you a way of quickly accessing data from hardware such as GPS units, LED arrays or sensors. In other words, they make it easier for you to build a project without having to get too involved in the finer details of programming.

 

Of course the Raspberry Pi was developed to help beginners hone their skills in this area, but it is nice to be able to see results quickly when you are first starting out. With this in mind, I recommend taking advantage of the excellent work that Adafruit have done in developing libraries which allow you to easily communicate with some of their popular hardware expansion devices.

 

An easy project to get to grips with is controlling an 8x8 LED matrix using I2C. It involves a little soldering and requires configuration of the GPIO, but is a good way to start. I chose the LED matrix because it gives you a visible sign that your code is working! Adafruit also have tutorials for other I2C devices in their learning centre.

 

Here's what you will need:


Raspberry PiRaspberry Pi

Raspbian OS (the easiest way to get this is to download the NOOBS package from the Raspberrypi.org website, alternatively, you can buy a Raspberry Pi which comes with one)

Adafruit Pi CobblerAdafruit Pi Cobbler (a device for breaking out the expansion headers on the Raspberry Pi to a breadboard, for easy prototyping. Available from CPC, MCM and Newark)

Adafruit 8x8 LED matrixAdafruit 8x8 LED matrix (sold as a backpack which needs assembling. Available from CPC, MCM and Newark)

BreadboardBreadboard (these Wish ones are

StrapsStraps (the wire for connecting up the hardware on your breadboard)


If you have already blinked a LED as your first Hello World! style hardware project, the concept of this will be familiar. What we are going to do is run and modify the test programs for the LED matrix which will help you explore how it works.

 

Hardware Assembly: Pi Cobbler


Before we start with any configuration on the software side, let's put the Pi Cobber together. The set comes in four parts: the ribbon cable which carries the signals from the Pi, the adapter, a PCB and a row of 2.54 mm headers.

 

You should be able to see on the blue PCB that the footprint of the adapter has been outlined, this makes it easy to get the adapter the right way round. Slot the adapter through the central holes in the PCB so that the writing on the PCB will face upwards once it's soldered. The notch in the footprint should line up with the notch in the adapter.

 

PiCobbler_ready_to_solder.jpg

PiCobbler_neat_solder.jpg

 

Now solder it on. If you are inexperienced with soldering then it's worth checking out the tutorial videos that Gert Van Loo made which should help.

 

Once you have completed that, cut the 2.54 mm headers to size and then slot them into the holes in the PCB so that the long legs are on the opposite side to the PCB writing. Adafruit recommend that you can keep the whole thing steady by first pushing the headers into a breadboard. Gert suggests another way, which is to use a sponge to hold the components in place. Either way works fine. Solder the pins and then make sure you test them. An easy way of doing this is to use a multimeter with a beeper which sounds when there is a short circuit.

 

Now that you have the soldering done, fit the ribbon cable into the adapter. The notch on the ribbon cable with fit into the notch in the adapter so that you know you have it the right way round. Notice that there will be a coloured (or white) stripe on the ribbon cable. This has to be the right way round when connected to the Raspberry Pi so be sure that it lines up with pins 1 and 2 on the Pi.

 

Hardware Assembly: 8x8 LED Matrix & Backpack


Depending on the type of matrix you have bought, it is wise to follow Adafruit's assembly tutorial to ensure that you orientate the LED matrix correctly. It is square so can easily be soldered in back to front. Like the Pi Cobbler, there is a little soldering required, but nothing too tricky. The 8x8 matrix uses a constant current controller IC which means you don't have to dedicate a large number of IO pins to controlling the LEDs (like you would if you just expanded your Hello World! program). All you need to do is to use the I2C line from the Raspberry Pi.

 

Wiring it up


I2C only needs simple connections, which is why it is so popular. It uses a Serial Data Line (SDA) and Serial Clock Line (SCL), which can be found at pins 1-03 and 1-05 on the Raspberry Pi. It also uses a ground and 3.3V connection for power. If you look closely at the LED matrix, you can see that the 4 connection pins are marked +, -, D and C. The SDA pin on the Raspberry Pi needs to be connected to the pin marked "D", and the SCL pin needs to be connected to the one marked "C". The "+" symbol needs to be connected to 3.3V and the "-" symbol needs to be connected to ground. The Raspberry Pi has 5 GND pins to choose from so pick one and wire it up. You can find a diagram to help explain these connections here, but is useful to figure out what goes where from looking at the PCBs. This will help later on when you have to wire up more complex circuits.

8x8_led_matrix_raspberry_pi.jpg

8x8_led_matrix_raspberry_pi_connections.jpg

 

Enabling I2C


On some versions of Raspbian, I2C is enabled by default, but on the standard distribution it is not. You can check what kernel modules are loaded as default by booting up your Raspberry Pi, logging in and then typing:

 

sudo nano /etc/modules


You need to use sudo as you will be editing this file as a super user with full admin access to your Raspberry Pi. Nano is a lightweight file editor. What you are doing is opening a file in an editor fairly similar to Notepad if you have ever used Microsoft Windows.

If the lines following lines are not listed below snd-bcm2835, then add them in:

 

i2c-bcm2708

i2c-dev


Press ctrl-X to exit and save the changes by hitting Y (for yes).

 

This enables the I2C for this family of chips (you might have guessed that bcm stands for Broadcom, who manufacture the CPU of the Raspberry Pi).

 

Now, you need get rid of the "blacklisted" SPI and I2C modules. On Raspbian, these are blacklisted by default, which stops the kernel from using them. According to the comments in the file, this is because most people don't need to use them.

 

All you need to do is type:

 

sudo nano /etc/modprobe.d/raspi-blacklist.conf

 

and put a # in front of the two lines:

 

i2c-bcm2708

i2c-dev

 

so that they now look like this:

 

#i2c-bcm2708

#i2c-dev


You have "commented out" this code, meaning that it will be ignored. Now the restrictions on our communications channels will be lifted.

 

Just like you did before, exit in the same way and save your changes.

 

You could also type:

 

sudo apt-get install i2c-tools


Which installs a useful package to help you identify which hardware is connected to the Raspberry Pi over I2C.

 

Now type:

 

sudo adduser pi i2c


This will mean you can access it from locations other than the root which is handy.

 

Finally, type:

 

sudo apt-get install git (this is needed later on for downloading code from Github)

sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade


This makes sure all the software is up to date. Between each of those commands, the Raspberry Pi will need to communicate over the internet so make sure you have your Pi connected using either an Ethernet cable, or WiPi.

Now  reboot your Pi. You have to do this so that the changes you made earlier will take effect. This can be done by pulling out the power supply, or more elegantly by typing:

 

sudo reboot


Your Raspberry Pi is now I2C and SPI enabled!

 

To get hold of the software which runs the 8x8 LED matrix, you need to type the following:

 

git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git

 

This downloads a whole selection of code examples that Adafruit have put together for the Raspberry Pi. Helpfully, they have created some demo code for the LED matrix as well, which you can access by typing:

 

cd Adafruit-Raspberry-Pi-Python-Code

cd Adafruit_LEDBackpack


This changes your directory (imagine clicking through different folders in Windows). You are now in the folder Adafruit_LEDBackpack and can access the demo program. In my experience, there is no need to reconfigure any of this code, which used to be dependent on the version of the Raspberry Pi that you were using, but now seems to have been fixed and improved. To run the program type:

 

sudo python ex_8x8_pixels.py


If everything has worked as expected, you should see the LED matrix light up in a scrolling sequence. Well done! If not don't worry, in electronics, things rarely work the first time, so go back through the steps and test to see what might be wrong. Most of the time it is a spelling error, or simple mis-wiring.

To exit the program hit ctrl-z. This just breaks out of the program so the LEDs will remain lit in whatever their last state was, so the scrolling will freeze.

 

Finally, play around with the demo program and see what you can change to alter the LED function. Type:

 

sudo nano ex_8x8_pixels.py


Try changing the time delay between LEDs lighting up or swapping the y and x axes to make the LEDs scroll in a different direction. It is best to change one thing at once so that you can see the outcome!

 

Good luck with this project, for detailed instructions on the component assembly, visit Adafruit's product assembly instructions. They also provide a few extra lines of code which help you to further explore the I2C functions on the Raspberry Pi.