So you’ve managed to get an LED to flash on your Raspberry Pi, and you are feeling pretty proud of yourself, but what next? Well, great little project to test your programming skills is a Persistence of Vision.
This project uses a pretty simple concept, but you have to pull together a number of different coding skills! So first let’s look at what is involved.
With a project like this it always best to break it down in to manageable chunks, so we are going to break the project down in to 3 parts:
- first get the whole project working inside python, by emulating the POV concept printing to the screen.
- then we are going to adapt our code to turn on and off LEDs
- Finally we are going to look at how we can get our project mobile and embed it in the real world.
Part 1 – Emulating Persistence Of Vision in Python
Before we start coding, let’s look at the some of the programming stuff we are going to need..
Letters represented using a matrix.
In order to print our letters using LEDs we are going to need to map them to a matrix, or grid, like below:
Here we are using a 3 by 5 matrix. This gives us a really basic font. The text you are reading here uses a bigger matrix to print to the screen and a special technique called anti-aliasing(notice the different shades of gray) to display smooth fonts, as can be seen below:
Once the characters are in the matrix, we need to display the characters by turning the LEDs on column by column, with a small pause in between each column.
At about half speed it looks like this:
So how do we get this wokring with just 5 LEDs? Well what we do is move the LEDs as they are lighting, and this creates the persistence of vision effect!
Implementing the matrix in Python
In order to this in Python we are going to use a dictionary to store our letters.
An example of the letter “A” is below:
Using a dictionary means that we can use the letter as the key, and the matrix representation as the value.
Using a letter from the matrix
So we have the letters in the dictionary ready to use, what do we need to do? Well, let’s think about it.
For each column in the letter we need to:
- work out which of the 5 LEDs to turn on and turn on the correct ones.
- Then we need to wait a small amount of time.
- Then move on to the next column in the letter.
First we need to split the letter in to lines:
Then we need to iterate through column in the matrix, lighting the LED ( or in this case just printing the “#” Symbol). In order to do this we need to use two for loops, one nested inside the other.
Since to we need to go through each line for each column, the line by line code needs to be nested inside the column by column code:
for each column in the columns:
for each line in the lines:
#do something here…
This will give us something like this:
Obviously just printing one letter isn’t enough, we need to print a whole word, or words! for let’s put all this code inside another for loop that iterates through each letter in the word or words you want to print!
This is great, but what if we want to print multiple sentences? It’s best to wrap the whole thing up inside a function, that we can call whenever we want.
The complete code will look a bit like this:
Too lazy to type in the letters by hand? Here is a link to the letters file
Part 2 – Getting your POV working on a Raspberry Pi
The next step is to get your POV outputting to LEDs instead of to the screen. First we need to set up the hardware:
Once we have set up the Hardware we need to adjust our code to make the LEDs light up
Import the GPIO module and do some setup
Change the print statements to GPIO outputs:
The complete function will look like this:
Fire up your Raspberry Pi and give the code a go!