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:

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.

Pinout of Common Cathode LED Matrix (BL-M12A881UR-11)

Pinout of Common Cathode LED Matrix (BL-M12A881UR-11)

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.

// LED Dot Matrix Test
#include <TimerOne.h>
int latchPin = 8; //Pin connected to Pin 12 of 74HC595 (Latch)
int clockPin = 12; //Pin connected to Pin 11 of 74HC595 (Clock)
int dataPin = 11; //Pin connected to Pin 14 of 74HC595 (Data)
byte led[8]; // 8 element unsigned integer array to store the sprite
void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  led[0] = B11111111; // enter the binary representation of the image
  led[1] = B10000001; // into the array
  led[2] = B10111101;
  led[3] = B10100101;
  led[4] = B10100101;
  led[5] = B10111101;
  led[6] = B10000001;
  led[7] = B11111111;
  Timer1.initialize(10000);
  Timer1.attachInterrupt(screenUpdate);
}
void loop()
{
  for(int i=0; i<8; i++)
  {
    led[i] = ~led[i];
  }
  delay(500);
}
void screenUpdate()
{
  byte row = B10000000;
  for(int i=0; i<9; i++)
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin,clockPin,LSBFIRST,led[i]);
    shiftOut(dataPin,clockPin,LSBFIRST,~row);
    digitalWrite(latchPin, HIGH);
    row = row >> 1;
  }
}

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[8] 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.

About these ads

13 Responses to “Beginning Arduino: Project 19 – LED Dot Matrix Basic Animation”

  1. [...] here’s another issue I found in the Beginning Arduino book I mentioned in a previous post. I was trying to work out Project 37, but I kept getting the same temperature reading over and [...]

  2. Anonymous said

    To answer your questions, int and byte are different data types. int is 16 bit number that maxes out at 65,535 if used as unsigned int and looks like 1111 1111 1111 11111 in binary. A byte is 8 bit number up to 255 and looks like 1111 1111 in binary.

  3. jjp said

    Hello, I too had problems with proj. 19…First I thought why did the author choose 3.3 volts? Was it a typo? Then I looked further and saw the resistors used were 100 ohms..If you assume the LED’S required about 2 volts to lite that would be about 13 mills. At 5 volts supply that would go up to 30 mills..Thats pushing the 595 chip..Like you said the 595′s will work just fine with 5V but you would have to increase the resistor value to a safe current level..I chose 1k since I had them in stock..That would be at 5v abut 3 mills current.
    The Matrix I used instead of a common cathode row was a common anode row device..I had to rewire so that the rows became columns and the columns became rows…The code(sketch in the book simply did not work.. I found this website and your revised code, and it works just like your blog shows..Thank you very much..I do not fully understand the software at this point but generally understand what it is trying to do..Hence the Beginning Arduino book..

    PS Now that your code works I will try to troubleshoot the book code..
    Thanks again,
    jjp

    • Ankur said

      Glad I could be of help, jjp! Classes have taken up my time now so I haven’t had a chance to try out many other chapters in the book so let me know if you find something else in the book that should be posted here.

      Also, if you ever figure out how led[8] exists in the screenUpdate function, let me know because that’s still bothering me :)

  4. jjp said

    Not sure if this is it or not but the 595 data sheet says in essence that if the clocks are together the shift register is one bit ahead of the storage register..As near as I can tell the both clocks are not tied together but are clocked from a master clock in the arduino.This is just speculation on my part but the only thing that I can see that could account for a one bit shift..
    Good luck on your studies..
    jjp

  5. jjp said

    I finally cracked the problem (project 19 works with the author’s code(…almost….Here is a reply I poster in the arduino forum..

    Yippee!!! It is solved..For any one else having problems heres what I did..
    First let me explain that my matrix is different than the authors matrix..His was an 8×8 with the Rows as common cathodes..That means that to lite row 1 you would need positive voltage on the column 1(anode) and ground Row1 cathode..Mine was just the opposite..I needed positive voltage on Row 1 and ground Column 1..In my case I just rewired the Fritz drawing so the Rows were sent to the columns and the columns were sent to the rows..After all the matrix is dumb and as long as you feed the proper voltages at the appropriate time it knows no difference..
    I hope that is clear.. Next the author published an error in his code
    It was row=row <>1; // bitshift right

    After I made the change, it still did not work correctly..My matrix flashed all LED’s and appeared to flicker every half second..
    Now down to the void that deals with data
    Was:
    // if the value of DataOut and (logical AND) a bitmask
    // are true, set pinState to 1 (HIGH)
    if ( dataOut & (1<<i) ) {
    pinState = HIGH;
    }
    else {
    pinState = LOW;
    }

    TO

    // if the value of DataOut and (logical AND) a bitmask
    // are true, set pinState to 1 (HIGH)
    if ( dataOut & (1<<i) ) {
    pinState = LOW;
    }
    else {
    pinState = HIGH;
    }
    In other words just reversed the data polarity from HIGH to LOW and LOW to HIGH
    I guess I should really adjust the comments to suit but I am so happy I can hardly type…Hehe

    I don't know if this is a result of my matrix or just another code error..BTW the authors above bitshift correction above is essential..
    This has been a three week battle but persistence pays off..GoodLuck!!
    jolphil

  6. jjp said

    Whoops..A typo
    should be row=row >> 1; // bitshift right

  7. jjp said

    Quote:
    Ankur said
    October 5, 2012 at 2:25 pm

    Good find! Looks like it’s about time for the author to issue a new edition :)

    Yes, but until then I think that either the book publisher or author should have an online collection of known errors that one can go to to verify a mistake or not..That would be very helpful and would cost much less than a new edition..Oh Well..
    jjp

  8. Stephen said

    Ankur & jpp – Thank you very much. I have been stuck on project 19 for a week and a half now. First I found the Hand on Arduino blog with the explanation for the row=row>>1 correction. I couldn’t get any further so I started searching the web for any other blogs that talked about this project. Obviously, I found yours. I got both your code and the books code to work. I also know a lot more about 8×8 LED Matricies.

    Ankur – Do you plan to continue documenting any of the other projects in this book?.

    slf

    • Ankur said

      slf, I haven’t considered it recently as I have been busy with classes/internship and such, but I may in the future if I find some downtime. I’m glad I was able to help since that was one of the main reasons for the post. I’m sure there are other projects in that book that have similar issues so it would be good to document them at some point. I hope you’re learning a lot with the Arduino! It’s quite a powerful platform.

      On Sun, Jul 14, 2013 at 4:29 PM, Mind over Material wrote:

      > ** > Stephen commented: “Ankur & jpp – Thank you very much. I have been > stuck on project 19 for a week and a half now. First I found the Hand on > Arduino blog with the explanation for the row=row>>1 correction. I couldn’t > get any further so I started searching the web” >

  9. Thanks for sharing this post with us…its really very nice and use full information….Animated LED Poster

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: