"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)

Tuesday, 14 August 2012

Arduino: playing with LCD displays

My experimenting with the Arduino board is continuing even if I'm not gone so far from trying provided example sketches. That's why I'm writing so little about it. I recently got a couple of LCD display modules based on the Hitachi HD44780 chip, that is the most widely used character LCD standard.

Setting up the circuit

Connection the Arduino to a LCD display module is quite easy, four wires carry the data while two more (Enable and R/S) handle the control signals. Of course you can use the Arduino digital pins you prefer to carry the task but using pins 5,4,3 and 2 for data signals and pins 12 and 11 fro R/S and Enable pin respectively will let you test the provided examples without modification. Apart from connecting wires the only extra components needed are a couple of trimmers (small variable resistors) for LCD contrast and back-light intensity adjustment. Here how the whole circuit looks like (not different from what you can find on Arduino site examples.)

Hello-worlding” ... and a little more

A good Arduino motto could be “There is a library for it”, among the many libraries provided with Arduino could not miss, of course, one for devices as widely used as LCD displays. Once the library is initialized, with the pins used in the circuit, writing on the display is just matter of simply calling “write()” commands. A hello-world example sketch is already provided with Arduino software showing the basics of LCD display writing.
I tried to go a little further by playing with the HD44780 ability to define a handful of custom character in order to play a little Pacman-like animation on the display. Custom character are supported by Arduino's LCD library and can be defined by a eight-by-five bit matrix:

I then defined a “fill” function to put the display in its initial status (filled with “pills”)

and an “anim()” function to do all the animation, and previous position clearing, work:

The sketch's “setup()” method so just initialize the custom characters and the display and calls the fill function while the main “loop()” method calls the animation function and update the Pacman position. Here is the full sketch code:

Here is, at last the whole thing running: as usual the real circuit is a lot messy than what I draw on a computer …
It looks a lot like the games I used to write with my first computer when I was a teenager … may be this is why I love Arduino.