Liquid Crystal Displays for the Arduino Ben Kuo, KK6FUT Pete Juliano, N6QW We have been gently urged by the QQ Editors to include LCD displays in our Arduino projects as there is a certain allure, charm and magic to a “visually changing scene”. Seemingly it is a reasonable request and one would think easy to implement which of course in reality may be reasonable but is not easy. The CW Sender Part III has an LCD Display in addition to the Computer display. N6QW owned a small computer manufacturing business and was amazed at how much behind the scenes coordination took place when building a computer from parts and pieces. Video cards while differing in capabilities all used a standard interface. The same applies to motherboards, hard disk drives and on and on. There were some exceptions but for most situations any board, any card any power supply was the rule. Thus was the basis of the terms “plug and play”. Unfortunately such universal plug and play does not exist with the Arduino and the various available LCD’s, LCD interfaces and LCD Libraries. Thus this short piece is an attempt to clarify what may be involved in implementing an LCD into an Arduino project. The basic element needed for LCD implementation is to understand the three legs of the stool: 1) the hardware, 2) the software environment and 3) programming the software The Hardware: • LCD selection offers many opportunities, to varying degrees, for displaying various data in small areas of the front panel. Common LCD types range from a 16 X 1 which means there are sixteen characters on a single line which of course is self- limiting. At the other end of the spectrum is the 20 X 4 which means there are 20 characters per line with a total of four lines. In between are the 8X2 the 12X2 and 16x2 displays. These are further subdivided into non-backlit and backlit devices. The non-backlit are marginal in that the screen can only be seen when the light hitting the front face is “just right” and certainly not good under low light conditions. With the backlit there are optional background colors and as well as character colors. A blue background with white lettering sure looks cool. The green background with black lettering is a step up from a red background with black lettering which can be fatiguing after long viewing periods! There of course is a price spread, with the 16X1 non-backlit (NBL) being less expensive as compared to the 20X4 backlit (BL) units which are the higher end devices. Typically the 16X1 NBL are less than $5 and the 20X4 BL ones are in the $15 range. Our personal bent is the 16X4 or 20X4 BL type, the reason being is that it is better to have more than adequate display area versus having not enough. That said more lines more complex! There is however a magic decoder ring when evaluating what type of LCD to purchase and that is the type of on board controller that is built into the LCD electronics. The LCD’s with the HD44780 controller are the most common and any other type may not work with the libraries that are available. Thus look carefully at what is the display controller before flashing the plastic! In summary we view the best option is either the 16X4 or the 20X4 Back Lit with the HD44780 controller chip. [There is another reason we are suggesting this type of LCD is that in the future a joint KK6FUT/N6QW SSB transceiver project will have an Arduino driven DDS and the 4 line display is required for that radio.] Units manufactured by Seiko, Samsung, Hitachi, Hantronix have the HD44780 controller and will work with the libraries. • LCD interconnections to the Arduino are either of the Parallel type or the Serial I2C. Most of the inexpensive LCD’s, regardless if they are 16X1 or 20X4, out of the box, are the parallel interface type and that would require at least 6 of the Arduino digital pins to implement. This of course really gobbles up a substantial part of the digital I/O capability. Not to worry as many of the Arduinos, such as the UNO R3, have what is called an I2C Serial interface that can be ported out of Analog Pins A4 and A5 or as in the case of the Arduino Leonardo are brought out as separate pins. These two pins are Clock (SCL) and Data (SDA). Using a separate small interface board generically called a “back pack” which is either connected to the back of the LCD display or permanently soldered to the LCD converts the two wire I2C Serial Data to the 6 pin Parallel Data needed to drive the display. Figure 1 shows one of the typical backpacks that enable the two wire interface (actually 4 wires if you also count the +5 VDC and GND.) Thus precious digital pins on the Arduino are preserved for other uses. This seems like a no-brainer BUT for the fact that the backpacks are not all standard and require different I2C addresses and different libraries. This being the case it undoubtedly will require some experimentation to get your LCD to play. Remember not easy? Later in this piece a matrix is provided to help decode some of the I2C addresses and libraries which are two the biggest players. Virtually most any parallel LCD (with the HD 44780) can be plugged into the back pack and will function once the library and I2C address issues are resolved. Backpacks sold by Jameco, ADA Fruit, Marlin P. Jones, Sparkfun as well as many listed on the major auction sites will work FB. Caveat Emptor: Make sure they are + 5 Volts DC, have 16 pins for the LCD and 4 pins that connect to the Arduino, and that the I2C address is documented. N6QW purchased a bargain backpack on an auction site and the documentation stated any of eight I2C addresses would work. He had to test all eight before he found that the last one actually worked. Remember the not easy comment at the beginning! Note some backpacks have programming solder pads where whatever is specified as the I2C address such as A0, A1 or A2 must have that same corresponding pads solder bridged to work in the circuit. Definitely not plug and play! The Software Environment: • Stating the exact library that will be used for the project is critical; but since there are many libraries, it will drive you nuts trying to figure which is which. In order for the display to work the hex file in the library must be initially stated as: #include or #include. These are the two most common we have seen and used. Another statement needed for the Arduino to “wire in” the I2C and LCD is: #include . Just a note these three library items must be downloaded from various websites with Arduino Site yielding the Wire and LiquidCrystal.h libraries and LiquidCrystal_I2C.h library can be found at the Ada Fruit site. All three must be present in the Arduino library on your computer. Simply inserting these statements in the sketch will cause the compiling to fail. The library must be present on your computer for everything to work! • Designating the LCD I2C address in the setup and initiation is a critical step. Some backpacks only require that you provide the address which may range from 0x20 to 0x27 depending on who manufactured the backpack. Others may have an address such as A0, A1 or A2. While other still at the same time you give the I2C address that you must also specify the LCD type where an entry might be (0x27, 16,4);. A formal subsequent sketch statement might read like LiquidCrystal_I2C lcd(0x27,16,4); • Understanding how data is actually displayed requires more than plug and play. It is not enough to simply connect an Arduino and hope that “Hello World” is displayed. Code has to be written that says start writing at position 0 on line 0. Yes the first line is line 0 and line four would be 3. The first character is printed at position 0 and the last at position 15. To actually center the “Hello World” you must count the number of characters and spaces which would be 11 and then one has to add “filler” so that the wording is more or less centered. Translated that means in addition to the two words and a space that leaves 5 spaces for filler which are blanks so we would be required to tell the computer to lcd.print(“ Hello World “); assuming the first position of the first line was designated as the starting point. Thus there are two blank spaces, the word Hello, a space, the word World and three blank spaces. In the case where you might have a changing display with a fixed display like Code Speed = XX. The “Code Speed =” would be addressed in the set up and essentially always be done at power on and remain the same. By the way that would only leave four spaces to enter the actual code speed which means a blank, the XX and a blank. The XX part of the display would be constantly changing and thus the sketch must define what is fixed and where is that displayed as well as the location on the display of the variable that is changing. Programming the Software • Note we will provide specific code example that can be put into the software but first want to cover some basics of things that must be in place for the LCD to have allure, charm and magic. 1. At the initiation state the #include and the #include 2. The appropriate I2C address [LiquidCrystal_I2C lcd(0x27,16,2);] 3. In setup [lcd.init();] Start the lcd 4. In setup [lcd.backlight();] Turn on the backlight 5. In setup [lcd.setCursor(0,0);] Start at the 1st Character 1st Line 6. In setup [lcd.print(" KK6FUT & N6QW ");] 7. In setup [lcd.setCursor(1,0);] Start at the 1st Character 2nd Line 8. In setup [lcd.print(“CODE SPEED = “);] This leaves 4 spaces 9. In void [lcd.setCursor(1,12);] Places cursor for the value 10. In void [lcd.print(val);] Prints the code speed in wpm The LCD will now display, more or less centered, on the first line reads KK6FUT & N6QW and the second line reads CODE SPEED and the Value. You will have to experiment with the LCD, the backpack and the code – our experience has definitely shown that when you pick and choose “bargain” displays, boards and other hardware it is not a simple plug in.