When I heard about Arduino a couple years ago (yes, I was a bit late to the party) I immediately fell in love with it. The main reason was that it allowed me to try things that otherwise would require a lot of time and effort from a total electronics noob like me. With Arduino, a breadboard, a few LEDs and a couple of cables I was ready to write my very first program – controlling the LED with Arduino. It was fun! But I wanted to do something more interesting. I figured that while flashing a LED is fun displaying something on a screen must be even more fun. Soon I found that it was possible to buy a cheap Nokia 5110 LCD displays on ebay. I had some doubts whether I would be able to make it work but figured out that even if I wouldn’t I could always use it as a key chain or a similar geeky gadget. Indeed, when the display arrived I did have some problems to make it work. First, I was not sure how to connect the display to Arduino. Even though I found a few websites that showed how to do that I had hard time to make it work. I also tried a couple of sample programs that in theory should display something on the display but in practice they did not. Eventually, I found that one of the cables were connected incorrectly and the sequence to initialize the display did not work (at least on my display). Seeing something on the screen was a huge step forward and I finally could think about using the display for my own purpose. I decided that I could do a digital clock as it seemed a reasonably sized project which would still require understanding how the display really works. First I wanted to “design” fonts for the clock. For this I just used graph paper and this reminded me designing sprites for my C-64 in the late eighties. After designing all digits I had to understand how to encode these so that they could be drawn easily on the screen. I found the datasheet for the PCD8544 which is a controller for the display. This was the first time in my life I had to read a datasheet for an electronic component but I was able to understand what the most important things and the datasheet turned out to be an interesting read. Soon, I found what I wanted – the display has 6 rows and 84 columns. Each column in a row has 8 pixels. To draw something on a screen first you need to set a pointer (row and column) and then write a byte (8 bits) to draw the column. When you draw a column the pointer automatically advances to the next column – this is handy since you don’t really have to set the pointer all the time if you draw something like a bitmap. The mechanics of drawing on the screen surprised me. Firstly, I found it kind of similar to advanced graphics modes on 8-bit computers. Secondly, it was interesting to see that the programmer’s convenience totally lost with the low level design of the controller (or may be I am just spoiled by higher level APIs which allow me just to pass x and y to draw a point). After figuring out how drawing looks like encoding my digits was relatively simple. Now that I was able to draw digits on the screen it was the time to implement the clock. Initially I wanted to use the
loop function but I quickly realized that it would not really work for a clock. I did not know how long drawing would take and therefore the clock would not work correctly. I could calculate how much time drawing took and then cut the wait time accordingly but it didn’t feel like the right solution. Obviously, there was one more (and I believe the only correct for this kind of problem) solution – interrupts. The only problem was to set everything up correctly. I spent some time reading Arduino datasheet but things did not really want to work. Fortunately I found this page which helped tremendously. After setting up interrupts things went smoothly and soon I had my digital clock up and running. I actually ended up using both the
loop function and the interrupt. The interrupt just increases the timer while all the drawing takes place in the
loop function. This way I don’t have to worry that drawing takes too much time and when a new interrupt happens the previous one is still being handled. That’s pretty much it. Here is a short video showing the clock in action:
If you want to try my project here is how I connected the Nokia 5110 display to Arduino:
Arduino Uno Nokia 5110 Display 3.3V ------------------ 1-VCC PIN #7 ------------------ 3-SCE PIN #6 ------------------ 4-RST PIN #5 ------------------ 5-D/C PIN #4 ------------------ 6-DNK(MOSI) (SDIN) PIN #3 ------------------ 7-SCLK
I also posted the sketch on my github.
One more thing I always wanted to do but never really have had time to do was to add some buttons to enable setting the clock – at the moment the clock always starts at 00:00. Maybe one day I will come back to this project again (like I did today just to publish it) and will add the buttons…