Instructables

How to control a 16x2 LCD using an AVR ATtiny2313

Picture of How to control a 16x2 LCD using an AVR ATtiny2313
If you are just getting into microcontrollers there's alot to know as you have likely found out. There's alot of good online tutorials for the Atmel AVR's and chances are you have see a few of those by now. I recently got into microcontrollers and didn't know jack about it. I wanted to have some early success and get my AVR to do something cool (other than blink leds).

I struggled through some terrible blog posts about how to control a common 16x2 LCD with the popular H44780 driver. I had to combine and tweak code from 3 places to get it working.  Anyway, over the past couple months I figured it out and since no one else has this covered on Instructables, I thought I would share my approach and see if you can get some early successes which will keep you diving deeper in to geekery.

There's an embedded video and pic of what this project will look like when you are done.

Here's what you will need:
1 - Atmel AVR ATtiny2313 microcontroller
1 - breadboard w/ 5VDC source (a 7805 will work with a 100uF & 0.1uF Cap)
1 - 16x2 Character LCD Display H44780 driver (available on Ebay for ~$6 or Sparkfun for ~$14)
1 - 16pin  0.1" spacing male header
1 - 330 resistor for LED backlight
1 - 1K for pull down on interrupt switch
2 - 10K resistor  for reset and counter interrupt pull ups
2 - small "breadboardable" momentary switches for reset & counter interrupt
1 - 1.5K resistor for contrast setting (could use a potentiometer), value may vary depending on display
1 - AVR ISP Programmer (see assumptions)
1 - soldering iron & solder
~10 - jumper wires for breadboard


Here's my assumptions:

- You have a AVR ISP programmer of some type (in my case the Atmel AVRISP mkII, but USBtinyISP should also work)
- I am assuming you have already gone through the flashy led tutorials and are somewhat familiar with some of the lingo like PORTs and binary numbers a little.
- I am doing everything in C code using WinAVR GCC.
- I am running the display in 8bit mode which isn't as pin efficient, but it is easier to teach.
- I am assuming you know how to solder
- I am assuming you are using the ATtiny2313, but you could put this code on any ATmega as well, as long as you rename the PORTs respectively. I'm not sure if this will work for Arduino's.
- My set up is using Atmel AVRStudio 4.17 with WinAVR 20090313 GCC C-compiler on a 32bit Window's Vista laptop. ( this is the toughest part because you may be using AVRdude or CodeVision which means my code may not directly work for you, but you will still learn something)
- My approach and my code may not be the most efficient or elegant way to run an LCD, but it does work and I can guarantee that when you see that display work for the first time, it's exciting regardless of how pretty your code is.

With all that said, lets get started!



 
Remove these adsRemove these ads by Signing Up
ofir603 days ago

Hey! Great tutorial, I'm planning to use it for my 16x2 on an ATtiny84.

I got a question though, I got only 6 pins available for the LCD and I saw somewhere that it is possible to use only 6 pins.

That some of the data bits are not needed, is it true? How can I do it?

Thanks.

ROMA4cm. made it!7 days ago

If someone need ASM code to be short as it can be then please upload modification of Roznerd code in ASM to display just "Yo". I needed LCD for other purposes than Winner game and it would be spent of time to coppy Winner game. My code works on ATMEGA48 but as with Roznerd code, you can make it run on other AVR such as Attiny2313. Maybe you also must change call instruction to rcall if some AVR is not supported call. Good luck!

100_1135.jpg
LCD.rar170 KB

Also please delete "call ledon" line and ledon proc. It was just using for debuging code! Thanx

ROMA4cm.8 days ago

Super instruction! I run it on ATMega48 with 1MHz because I want to build nAmeter. But I was encountered problems because I mess with wires 8 and 9 which goes from IC.. Be carefull with theese ones o_O!!! Anyway great instruction! 2 hours and all was working!

100_1134.jpg
tolesh1 year ago
Thanx man!, I'm new in all this electronic stuff but it's a great explanation....I'll try to do it soon. Cheers from Mexico
purpulhaze3 years ago
This would be perfect for using in a trivia type handheld game.
thanx
Love your instructions - you really helped me get going. Thank you. I ran into trouble a couple times though - I found some terrible avr-objcopy flags on some other website that stymied me for days. The AVRFreaks finally figured out the problem - I wasn't copying the .data section into the hex file! Would you consider adding a little section with an example Makefile or even a simple gcc/objcopy inline example so noobs like me don't make the same mistake again?

Also, I've taken what you taught me and created my own instructable:
http://www.instructables.com/id/AVR-LCD-Namebadge/

Cheers!
WVvan3 years ago
Thanks for the instructable. I''m trying to follow your instructions but I think there is a problem with the bottom two pinouts on the lcd diagram LCD_Schematic.png. Shouldn't one of those go to ground?
roznerd (author)  WVvan3 years ago
Thanks for the comment. Good Catch! The VSS is the GND pin and VDD is the +5V. I have updated the schematic in the ZIP and in Step 3. I hope you have luck getting it working. Thanks - Roznerd