Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Arduino LCD playground | LCD 4-bit library

An old 4-bit LCD library (now redundant)

Do not use this library

Please note that from Arduino 0016 onwards the official LiquidCrystal library built into the IDE will also work using 6 Arduino Pins in 4 bit mode. It is also faster and less resource hungry, and has more features. This LCD4bit library dates from 2006 when the official library only worked in 8 bit mode. It is effectively redundant.

What was it? Why?

This was an unofficial, unmaintained Arduino library which allowed your Arduino to talk to a HD44780-compatible LCD using only 6 Arduino pins. It was neillzero's conversion of the code from Heather's original Arduino LCD tutorial which required 11 Arduino pins.

Nowadays, it is recommended to use the LiquidCrystal library that comes with Arduino IDE.

Download the old library! (includes an example sketch).

Compatibility

This library should work with all HD44780-compatible devices. It has been tested successfully with:

  • Xiamen GDM1602K (from Sparkfun)
  • Display Elektronik DEM 16101H (16x1) and DEM 16217SYH (16x2) (from Maplin).
  • Display 20x4 (KS0066U-00PCC et KS0065BPCC controllers, HD44780 code compatible) usable with Jonathan's modified library to get all 4 lines working: (From LEXTRONIC France) (this version also adds some optimizations and WriteCGRAM to use user-defined characters). Read more on 20x4 modifications.
  • Display 16x2 (KS0066 controller, black & white, no back-light, HD44780 code compatible). First line is working, but both lines are greatly dimmed when displayed at the same time (I think it's a problem in my circuit, not in the library)
  • Hantronix HDM16216L-B display, 16 characters X 2 lines.
  • Tianma TM404A, 40 characters x 4 lines, first 2 lines are working, for third and fourth line a modification is necessary, see LCD4BitLibrary-tm404a
  • Everbouquet MC0802A-SGR, little 8x2 (from Farnell)
  • DMC16249 2x16 display from Optrex
  • DE-LM001, 8 characters x 2 lines (from Sure Electronics)
  • Emerging Display Technologies Corporation display # 24210
  • Sharp LM40A21, 40x2, using HD44780A00. Mostly worked, adding a delay(2) after the CMD_HOME in cursorTo() fixed periodically garbled display problem. The HD44780 datasheet (pdf) I looked at specifies a 1.52ms delay for the Return home command at nominal f_osc of 270kHz, but it appears that this value could be as high at 2.16ms at low end of f_osc range, 190Khz.
  • POWERTIP PC-4002B A, 40x2
  • Tinsharp TC1602A-09T, 16x2
  • Displaytech 162B, 16x2 (from RS)
  • ECM1602A (16x2, S6A0069 based)
  • Batron BTHQ21608VSS (16x2, based on the Samsung KS0070BP-00CC)
  • DEM16217 2x16 from Segor (Datasheet)
  • NHD-0216K1Z-NSB-FBW-L (Newhaven Display 16x2)
  • Dalian YM1602C (edited code to move Enable pin from 2 to 11)
  • Hitachi LM071L
  • Winstar WP2002A-Y-JCS 20x2
  • Tinsharp Electronics TC1602D-02(R) 16x2 (added 2nd Apr 2009)
  • Fordata Electronic FDCC1602G-NLYFTW-R
  • Samsung 16T202DA1 (16x2 VFD Display)
  • Samsung 20T202DA1E (20x2 VFD) Unused pins can be left floating
  • AZ Displays ACM 1602K Series LCD Module (16x2 Display)
  • add your tested device here

Installation

Install exactly as you would the LiquidCrystal library in the original LCD tutorial. For a basic explanation of how libraries work in Arduino read the library page.

  • download 4-bit LCD library, "LCD4Bit", here.
  • Unzip the files and place the whole LCD4Bit folder inside your arduino-0006\lib\targets\libraries folder. (The following path should exist afterward: arduino-0006\lib\targets\libraries\LCD4Bit\LCD4Bit.cpp )
  • Start the Arduino program and check to make sure LCD4Bit is now available as an option in the Sketch menu under "Import Library".
  • You might also want to make a copy of the example sketch in LCD4Bit/examples/LCD4BitExample/LCD4BitExample.pde and confirm that it compiles and runs correctly.

How to use it

The library is intended to be a 4-bit replacement for the original LCD tutorial code and is compatible with very little change. Here's what you must do after the setup described in the original tutorial:

  • In your circuit (1): You must tie the LCD's R/W pin to ground. (This frees another arduino pin for you). See this forum post for more troubleshooting.
  • In your circuit (2): remove the 4 lower bits of the data bus between the arduino and the LCD. We only use DB4~7.

The pin assignments for the data pins are hard coded in the library. You can change these but it is necessary to use contiguous, ascending Arduino pins for the library to function correctly. To change this behavior to be able to use any Arduino pins, change these lines:

for (int i=DB[0]; i <= DB[3]; i++) {
    digitalWrite(i,val_nibble & 01);

to

for (int i=0; i <= 3; i++) {
    digitalWrite(DB[i],val_nibble & 01);

  • In your code: The constructor now requires to know if your display is 1 or 2 lines. e.g.
    LCD4Bit lcd = LCD4Bit(1);

Example

#include <LCD4Bit.h> 
LCD4Bit lcd = LCD4Bit(1);   //create a 1-line display.
lcd.clear();
delay(1000);
lcd.printIn("arduino");

There's also a working, commented example sketch included in the download, in LCD4Bit/examples/LCD4BitExample/LCD4BitExample.pde

Extra functions

I also added a couple of functions to stimulate ideas, but you might want to delete them from your copy of the library to save program space.

//scroll entire display 20 chars to left, delaying 50ms each step
lcd.leftScroll(20, 50);

//move to an absolute position
lcd.cursorTo(2, 0); //line=2, x=0.

Troubleshooting

If you have never had your LCD working, I suggest you start with the original arduino LCD tutorial, using all 8-bits in the data-bus. Once you are sure your display is working, you can move on to use the 4-bit version.

Using 4-bit LCD alongside interrupts

Source repository

I've created a googlecode project to maintain the source, at https://code.google.com/archive/p/arduino-4bitlcd

This does not yet have changes from other contributors.

You can get the source from svn anonymously over http using this command-line: svn checkout https://arduino-4bitlcd.googlecode.com/svn/trunk/ arduino-4bitlcd

Modifying the library

See this forum post. Specifically, note that you should delete the library's .o file after change, so that it will be recompiled.

On using and creating libraries

TODO:

LCD4Bit development notes

See also

the main LCD playground page and the original LCD tutorial.

A speed tuned version with assembler: LCD4Bit with Assembler = Highspeed LCD