Beginning Arduino: Project 19 – LED Dot Matrix Basic Animation
July 17, 2012
So one of the inspirations I had for restarting this blog was the fact that when I ran into an issue with following Michael McRoberts’ Beginning Arduino book, I couldn’t find a clear solution online. I decided that if I post my solution on this blog then hopefully some future reader of this book will be less confused than I was.
Overall, Beginning Arduino is a great book, but I disagree with the author’s claim that understanding the book requires absolutely zero experience. This may be true for the early chapters, but as the projects become more complex (and inherently more interesting), there is less explanation from the author about why certain things were done or how certain things work. This is an issue when things don’t work the way the book says they should and basic troubleshooting doesn’t find a clear solution.
I ran into this issue the most in Project 19 where I wasn’t sure if my approach was incorrect or if the author was doing something incorrect. I’m not the only one who ran into a similar issue so I will detail my approach below for when anybody else gets stuck on this project so they can have one more source of information.
Below are the components I used in my setup:
- 1x LED Dot Matrix Common Cathode
- 2x Breadboards (linked together to offer more working space)
- 1x Barebones Arduino
- 8x 100 Ω Resistors
- 2x 74HC595 Shift Registers
So for starters, I was using the Barebones Arduino and not the official Arduino Uno as used in the book. The first hurdle I reached was the book asked to use the 3.3V output of the Arduino instead of the standard 5V output. The Barebones Arduino does not have this output unfortunately so I decided to make a voltage divider using a 3.3 kΩ and 1.8 kΩ resistor (taking the voltage across the 3.3 kΩ resistor). This worked in the sense that the matrix lit up, but I ran into issues with flickering lights or some LEDs not turning on at all when a certain number of LEDs were already lit. This issue bothered me for the longest time until I decided to double-check the datasheet for the shift registers. What I found was that the shift registers were rated for anywhere from 2-6V, which was contrary to what the book said. The book mentioned that the shift registers operate at 3.3V, but it turns out they operate perfectly fine at 5V as well. So I removed the voltage divider, and voilà, the matrix was lit up properly as it now had the proper voltage for all the LEDs! I’m not sure why the author wanted to use 3.3V for the matrix, but this is the first big difference I found in this project.
The next major issues I found were in the code. The first thing I want to point out is that I used a common cathode LED matrix as opposed to the common anode matrix suggested by the book. This was mainly because I could only find common cathode matrices on Adafruit and didn’t want to spend time and money looking for a different site that had common anode matrices. Why is this important? The code in the book is written for common anodes and must be modified slightly to make it work for a common cathode (rows are cathodes, columns are anodes). This requires understanding how the dot matrix works to get the multiplexing working properly.
In order to turn an LED on, the anode needs to be HIGH and the cathode needs to be LOW. So in the case of the common cathode matrix, the row needs to be LOW and the column needs to be HIGH for the LED at (row, column) to be on. I also switched around the resistors so they were connected to the columns instead of the rows so the conventional current would flow through the resistor first and then the LED. I don’t think this change is necessary but as part of my troubleshooting I did it as it made sense for consistency.
Even with these changes in mind, I could not get the code to work properly. This is when I found the Hands On Arduino blog and noticed the comment that the author made an error in the bit shifting part of the code. He bit shifted left when he should have bit shifted right. This change still didn’t get my matrix working. I ended up with either flickering lights or incorrect patterns showing. At this point I decided to just do the code from scratch and came up with the code below that did. I explain the code later on.
The beginning of the code is similar to the book where it includes the TimerOne library and initializes all the pins and the pattern to be displayed. The loop function just inverts the pattern every half second to provide the alternating animation. The real difference in the code lies in the screenUpdate function which is called every 10000 microseconds. First of all, instead of the shiftOut (or shiftIt depending on what project you’re looking at) function you write in an earlier project, I used the built-in shiftOut function that is a part of the Arduino library. I’m guessing the author has you write your own function earlier mainly because it’s a good way to learn how the shift register works, but the built-in one does the same thing and therefore has less of a chance of a coding error (especially since the one in the book changes between various projects). In this update function, first the pattern is shifted so it goes to the columns and then the byte value for the row is shifted to go to the proper row for multiplexing. This gets cycled through quickly in order to produce the basic animation of your pattern.
There is a key difference from the book code. The difference is that the LED pattern (led[k]) is not inverted while the row is (~row). This is the main difference in common cathode versus common anode. As you recall from above, the row needs to be LOW when the column is HIGH. The second shift register controls the columns, which is why the pattern is shifted first.
One thing that also doesn’t make sense to me, but works anyway, is that the for loop in the screenUpdate function iterates with a byte from 0 to 8. If you change the byte to int, it still works. But if you change the iteration to be from 0 to 7, which makes more sense to me since led shouldn’t exist, it still works but the last row is very bright. If someone can explain to me why that is the case, it would be greatly appreciated.
Again, I hope this post helps anybody who may be stuck on this project by shedding a little more light on areas the book didn’t explain very well. Let me know in the comments if there are any other issues people may be having.