In this example we going to connect a MAX7219_LED_MATRIX and control it with the SPI bus on the Raspberry PI.
8. Nov 2015 Important !!
When I was working on next step in this to daisy chain more than one on same bus then I measured some extreme power usage at startup when the Raspberry PI booted until the application was running. If you have big power supply then you should be ok with one such display, don’t try to daisy chain for now, I had 2,5 A power supply. This guide will be updated when I have tackled this problem.
Dealing with this one in only Xojo code turned out to be a bit hellish. So I ended up with making a plugin which acts as device driver for the LED_MATRIX. The plugin comes free of charge for everyone.
I had hellish time doing this example as I started out with two such displays, one was completely broken and the other would not turn on one of its rows. But then I found the fault in it and repaired it with a little bit of soldering.
In theory the plugin supports chaining up to 8 such matrixes on the same SPI bus and controlling them all. But I could not test this at this time because the second display was totally bad, I will test and revisit chaining once I get more such matrix displays.
Disclaimer:
We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry PI.
Pieces we use are:
- Raspberry PI 2
- Cobbler and Cobbler cable
- Bread board
- MAX7219_LED_MATRIX. (Those matrixes are cheap, I am getting them for $15,5 for 10 of them from store called Advanced Tech on Aliexpress, thats only $1,55 a piece if buying bulk and bulk buy gets you free shipping usually too)
- Einhugur PIDevice_MAX7219_LED_MATRIX plugin for Xojo, this is a free plugin (Note that the plugin requires WiringPI to be installed on the machine)
If you are not familiar with working with a breadboard or on how to place a cobbler on the breadboard then click here bellow
Wiring the LED matrix.
We will just be wiring one for now, but you can see on top of the matrix that there are more pins to daisy chain more than one, which we will attempt in a update later when I get more matrixes.
To connect just one such LED matrix then we will be using the pins at bottom of it seen on the picture above.
We connect as follows:
VCC to 5V (Red wire)
GND to GND (Blue wire)
DIN to MOSI (Black wire) (MOSI is listed as GPIO10 (orange marked) on the image bellow but MOSI on the Cobbler.
CS to CE0 (White wire) (CS is listed as GPIO8 (orange marked) on the image bellow but CE0 on the Cobbler)
CLK to SCLK (Purple wire) (SCLK is listed as GPIO11 (orange marked) on the image bellow but SCLK on the cobbler)
The Xojo code:
Note that the plugin PIDevice_MAX7219_LED_MATRIX.rbplugin needs to be installed in your Xojo Plugin directory, and WiringPI needs to be installed on your Raspberry PI.
We put 3 buttons on a window, “Display the letter A”, “Display frame”, “Clear”, and a Slider control with a label infant of it “Intensity:”,
Set slider Line step and Page step to 1.
Set Slider Minimum to 1
Set slider Value to 3
Set slider Maximum to 15
The open event of the window:
Sub Open() PIMatrix = new PIDevice_MAX7219_LED_MATRIX(1) if not PIMatrix.Initialize() then PushButton1.Enabled = false PushButton2.Enabled = false PushButton3.Enabled = false MsgBox "Could not open SPI port" end if End Sub
Action event for the “Display the letter A” button:
Sub Action() PIMatrix.SendASCIILetter(1,Asc("A")) End Sub
Action event for the “Display frame” button:
Sub Action() Dim symbol as new PIDevice_MAX7219_8x8Symbol() // Generate our symbol which will be just a simple frame for y as Integer = 0 to 7 for x as Integer = 0 to 7 if y = 0 or y = 7 or x = 0 or x = 7 then symbol.Part(x,y) = true end if next next PIMatrix.SendSymbol(1,symbol) End Sub
Action event for the “Clear” button:
Sub Action() PIMatrix.Clear(1) End Sub
ValueChanged event for the slider:
Sub ValueChanged() PIMatrix.SetIntensity(me.Value) End Sub
Preparing the Raspberry PI
To be able to run this example then you need to prepare your Raspberry PI.
- WiringPI must be installed. See here how to do that
- SPI must be enabled on the Raspberry PI.
To do that then go in terminal and type sudo sudo raspi-config, then go in advanced and enable SPI and at end of that you reboot.
Running code
Running the code should give you something like this:
And this: