Following my first article on the initial findings and results of my tests on the ATMega8, this article will show basic steps on how to connect a 2x16 characters LCD to the micro chip.

The reason for this is to have a way of debugging the microchip functionality, to output the results, the data and various other stuff. I can tell you this is a must have.

To use a minimum of I/O pins, I've decided to build a 4bit interface with the LCD. The hard part was to write the code, but finally after fixing several bugs and reading a lot of HD44780 documentation, I can say that I've completed a nice flexible code - meaning that you can easily configure the LCD pin connection.

The wiring diagram is below:

The code interface is also attached, not much to say about it here, since it's full of technical details and bit-wise operations, but if anyone has questions feel free to use the comments form at the bottom.
To edit some parameters like frequency or PIN connections (you can change the I/O pins used by the LCD on the ATMega), have a look in lcd.h . Zip attached: (update available, see below).

Function lcd_string2 is very useful for outputting text, since it supports variable parameters similar to printf. So you can use it for outputting misc content: lcd_string2("Hello World! %d\",i);

Here are some pictures with my LCD running:

Next thing to do is to start connecting various sensors.

Update October 16, 2009

LCD Code update to fix some issues in the delay functions. (update available, see below).

Update May 1, 2012

Some time has passed since this article was written, meanwhile I had the chance to improve the code a lot. The issue with character corruption requiring a first reset is fixed, so are other small issues I found. All in one, here is a better solution for you to use.
The LCD PINS are configured as following:
You can change all these in HD44780.h
The speed of your MCU should be configured in aux_globals.h, by default it is set for 16MHz:
#define F_CPU 16000000UL

Here is the code for C:

And here is the code for C++, with the LCD interface as a dedicated class! To compile this code, you will need to set your AVR as C++ compatible. It is easy and well worth the time. See my tutorial here, to learn how to do it. C++ code: LCDTestCPP
This code will work perfectly with 1line, 2Line or 4 Lines HD44780 LCDs.

Update February 21, 2013

I have published two updates for this library. One using direct LCD connections (similar to what I have shown here) and another variant that uses a 74HC164 shift register to reduce the connections to only 3 wires. See it here.

(Visited 3,995 times, 2 visits today)
Tagged on:                 

107 thoughts on “ATmega8 and 2×16 HD44780 LCD

  • September 16, 2012 at 5:36 am

    Hi Radu! I have interfaced LCD with Atmega8…
    But in the starting it shows only blocks…
    But after sorting Vcc with the ground for a while it works well..
    What can be the cause of it….
    And please can u send me the Proper Hardware schematic with the microcontroller…..
    I mean with power supply and all….

  • September 16, 2012 at 8:52 am

    make sure you are using the latest version, see my updates at the bottom of the post. the schematic is in the article, let me know if you need anything else.

  • November 5, 2012 at 11:05 pm

    what’s about the function of lcd_waitbusy?

    static uint8_t lcd_waitbusy(void)

    this function should return a value of type uint8_t but in the file inside the file HDD44780.c the function returns nothing.

  • Pingback: ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 « PocketMagic

  • March 9, 2013 at 5:29 pm

    Hello mate,

    Thanks very much for the tutorial, I’m a complete newbie to this game.

    I’m trying to compile it all in AVR studio 6, but on trying it I’m getting “undefined reference to…” errors with all the lcd_ commands.

    Not sure why, I have the headers linked in there and have even tried putting everything in the same .c file (with the same result) so it doesn’t look like an error caused by the dependency files.


  • September 20, 2014 at 6:37 pm

    Thanks for the tutorial, i m a final year student in one of the Polytechnics in Nigeria.
    I choose to design an microcontroller based automatic vehicle screening gate(ATmega8 with LCD). This gate system allow only lighter weight vehicle to pass through but denied bigger ones frm accessing the drive way. The LCD display either WELCOME/ACCESS DENIED.please i need a writeup to guide me through. Please ur response is highly required as time of submission is fast approaching.
    Thank you.

  • Pingback: Multitester using ATMega8 and HD44780 LCD - PocketMagic

Leave a Reply

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