4-Bit LCD

My motivation in explaining this simple kind of project is that I’ve looked for but have been unable to find any source code that quickly explains how an LCD can be interfaced in 4-bit mode to an 8-bit microcontroller. I know it isn’t rocket science and that any pertinent data can be derived from datasheets, but that can be kind of confusing to someone who has never programmed in a low level language like C before and is especially difficult to those who have never programmed at all. Almost every line is commented, and there is even support for using the standard printf function to write directly to the LCD with the printf command.

The source provided below is written in C and is compiled using the free GNU AVR-GCC compiler. Testing was conducted on two of Atmel’s AVR line of 8-bit microcontrollers: the ATmega8, and ATmega128. With a little work it can be ported for use with PIC series microcontrollers from Microchip or even any other microcontroller on the market.

The LCD controller is HD44780 (standard Hitachi interface) compatible and has 4 lines of 20 characters each — see the picture below. Such LCD screens are easy to find either through a surplus store or an eBay vendor. Try not to spend more than $20 (including shipping) on these non-graphical LCDs because otherwise you are spending way too much.

4x20 LCD

The lines on the screen can be a little misleading; both lines 1 and 3, and both lines 2 and 4 are treated as though they are one line each and that the display were really 2 lines of 40 characters each. As a result, after 20 characters have been written on each line the display wraps unexpectedly. The first character after line 1 will end up on line 3 instead of line 2, and the first character after line 2 will end up on line 4 instead of line 3. If a character is written after the end of line 3 it will end up at the beginning of line 2. The next write after line 4, though, writes to line 1 as anticipated. Luckily, this is accounted for in software as the microcontroller keeps track of the current position of the cursor. The reason I bring this up is if you planned on using a 1 or 2 line display you would not suffer from this phenomena and thus could comment out any unneeded lines of code.

To interface the display, at least 6 I/O lines are required to run in 4-bit mode (7 if there is a need to read any data from the LCD), and at least 10 I/O lines in 8-bit mode (11 if reading data). Don’t have or don’t feel like using at least 6 I/O lines? The code fits into an implementation built around I2C — with a little hardware modification and design. For more information on that please check out the I2C LCD (I haven’t completed this page as of yet).

In both 4 and 8-bit modes it can be awkward to connect an LCD to a development board without having wires going everywhere if there is not a 16-pin LCD header readily available. Furthermore, if a 3.3V microcontroller is used to drive the output data pins then an additional 5.0V step-down voltage regulator and capacitors will be needed (assuming an input of greater than 6.5V is used). While some of the Hitachi LCDs can run with an input source voltage between 2.7V and 5.5V (HD44780U), it turns out that the screen I own (HD44780S) only has the ability to handle a 5V input. To solve this issue, I designed an LCD Header PCB. Look at that project page if that is the design route you wish to take for it has pictures of the finished design and also a schematic for possible PCB production or wire-wrapping/cabling.

Source File Packages:

4bit_lcd.tar.gz    [8,898 bytes]
4bit_lcd.zip       [11,731 bytes]

Individual Source Files:

lcd_4bit.c         [9,829 bytes]
lcd_4bit.h         [4,811 bytes]
delay.c            [3,588 bytes]
delay.h            [2,506 bytes]
makefile           [10,490 bytes]
main.c             [1,091 bytes]

Datasheets and Miscellaneous PDFs:

HD44780S/U Datasheet – [399kB]

    Data sheet for the controller. This document painfully details everything needed to start writing to and reading from any HD44780-compatible LCD screen.

Common LCD Commands – [10kB]

    A list of the most commonly used LCD commands and their respective execution times.

Defining Custom LCD Characters – [65kB]

    A fairly extensive guide on how to create your own custom characters.

Display Character Position and Character Address – [15kB]

    Quick reference guide to figuring out which memory address corresponds to which character on a variety of different size LCD screens.

Hantronix Specification Sheet – [165kB]

    A quick reference guide to everything needed to get up and running with an LCD.

13 Responses to 4-Bit LCD

  1. Roberto Alvarado says:

    I really appreciate your kindness. There is no other website with as much detailed information regarding LCDs.
    Thanks and God Bless

  2. Alan says:

    what i need to change to using it on PIC18?
    redefine port?

    thanks!

  3. sb says:

    Thanks it really helped me!

  4. shreepathi says:

    Thanks for the code.

  5. Thomas says:

    You Rock I couldnt find this anywhere else

  6. Eric Gorder says:

    Thank you very much for giving quality info without any strings attached!

    You are kind.

    God bless,
    Eric G

  7. Maulik Patel says:

    Really Appreciated.

    Thanks a lot.

  8. satya.v says:

    i very thank full you giving complete information about lcd interfacing

  9. Christopher Botsheane says:

    Thank you very much. your code is very detailed and the comments are on point. Took me forever to get an LCD running before I came here

  10. knight23 says:

    Hi,

    Where is defined the function BV(), that is used in function writeNibbleToLCD()?
    I can’t find her.

    Knightd23

  11. LESLIE says:

    i am using 6502 microprocessor (brain won’t learn anything else cause there is nobody
    out there who knows how to teach anymore…all ‘they’ do is cocaine, meth, marijuana,
    etc). so i need to use a LCD (1-line 16-character 14 or 16 pin) with my perfected 6502
    sbc projects. eg: i have a capacitance meter that uses 555 timer chip. COUNT is placed
    in sram & used to calculate result 6522 io chip inputs & outputs all data. i would like to
    see RESULTS displayed onto lcd. DO YOU KNOW how to do this ? thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>