Using an I2C LCD on Attiny85

34,811

51

32

About: I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit slower and do things I like. Other than my hobbies that involves grassroots medicine in S.E.&P Asia. I have bu...

Some people experience problems. Usually that is because of not having the right library, not having that library installed properly or have it overwritten by an update.
In this link you will find the configuration that works for me on IDE 1.6.9. Put all those files (except the picture) in ONE directory, where also your INO file goes

Using an LCD on a small chip like an attiny85 is not really that hard and till recent I didnt even think it warranted an instructable, but I have received questions about it, so I may as well expand on the process.

With the attiny only having a max of 6 pins available, it goes without saying that it cannot directly control all the pins of the standard Hitachi based LCD's
A little bit over a year ago, I described how to add an LCD to an Attiny or other chip, using only 2 pins. That circuit made use of an HC164 shift register, but as I2C modules for LCD's are extremely cheap and even LCD's with a module already in place also are dirt cheap, one might as well use I2C on the Attiny85.

I2C
The attiny85 can simulate I2C on PB2 (pin 7) (SCL) and PB0 (pin 5) (SDA). The 'Wire' library that is used to read and write bytes from and to the I2C port on the arduino doesnt work on the attiny. It needs the TinyWireM library to act as an I2C master

Library
The standard Arduino library cannot be used for I2C on the Attiny because it does a call to 'Wire.h' and that one is not compatible with the Attiny.
The 'NewLCD' library from Francisco Malpartida is my favorite library, but also that one fails in using I2C for the Attiny because it makes a call to the Wire library. A modification to make it work with Attiny85 can be found here.
The 'Bro Hogan' library however does work. It is basically the same library as the standard arduino LCD library, but it is modified to recognize the Attiny85 and the Attiny2313 and then makes a call to 'TinyWireM' rather than 'Wire'.
Adafruit also provides a libray that works with the Attiny85 and that is described in another instructable. I will be using the Bro Hogan library here.

Avoiding problems
Most problems you may encounter are related to the IDE getting confused regarding the libraries. If you are using the standard Arduino LCD library, best replace it by the Bro Hogan library. If you are using Malpartida's library and want to keep that (as it is a great library), move it out of the way. Grab the entire folder and move it out of your sketchbook/libraries folder. Make sure you have the TinyWireM library installed and make sure your libraries are up to date.
If for whatever reason you cannot or do not want to move the Malpartida library out of the way, either rename the LiquidCrystal_I2C.h and LiquidCrystal_I2C.cpp modules in that library, or put the BroHogan LiquidCrystal_I2C.h and LiquidCrystal_I2C.cpp files in your sketch directory (and change the fishhook call into parenthesis).

Programming the Attiny
I presume you know how to do that. Nevertheless I'll run you through some pitfalls:
Burn the bootloader!!! I am probably saying things most people already know but you'd be surprised how many people dont know.
Now obviously there is no bootloader for the attiny85, but the process of burning the bootloader sets the fuses of the attiny from factory mode, to the mode you want to use it in. So, presuming you use the Arduino as ISP,:
upload the ISP program to your Arduino,
put the attiny in your program shield
Go to 'Tools-Board' -> Choose 'Attiny'
Go to 'Tools-Processor' -> Choose Attiny85
Go to 'Tools-Clock' -> Choose 8Mhz (or 1 if you prefer)
Go to 'Tools-Burn bootloader'
done.

Then upload the program (see next step) to the IDE
Go to 'Sketch-Upload using programmer'
done

"Hey, my IDE says there are updates for some libraries"
The newer versions of the IDE will tell you when there are updates for some of your libraries and often, the Liquid Crystal library is also suggested for an update. Don't If you have the Bro Hogan library, it will be replaced by for instance the YW robot library, while the Malpartida library will probably be completely deleted.

Connecting the LCD.
The I2C module has 4 connections: Vcc, SCL, SDA, Ground.
they connect as follows to the Attiny:
Attiny85 <---> LCD

Vcc (pin8) <-----> Vcc
SCL (pin7) <-----> SCL
SDA (pin5) <-----> SDA
GND (pin4)<-----> GND

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: I2C LCD on Attiny 85: the Program

I am always a bit hesitant to put a program including libraries on instructables as sometimes the so called 'fishhooks' around the library are interpreted as html code and make the name of the library disappear, even after I made sure they were there.
Nevertheless, you will find the code below, but just make sure to compare it to the screenshot in the IDE that I have attached in this step

#include <TinyWireM.h>                 
#include <LiquidCrystal_I2C.h>         

LiquidCrystal_I2C lcd(0x20,16,2);  // set address & 16 chars / 2 lines

void setup()
{

  TinyWireM.begin();                    // initialize I2C lib
  lcd.init();                           // initialize the lcd 
  lcd.backlight(); 
  lcd.clear();  // Print a message to the LCD.
}

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print("Hello World on Attiny85");
  
}

Step 2: Using an I2C LCD on Attiny85: Still Problems

Hey, I did everything you said, but it doesn't work
I realize that is frustrating, but I can give you the assurance that the program and the libraries will work as they have for me.
When I first did this.... months ago, it worked immediately, without any problems. But when I tried this again for the sake of helping someone out (and this instructable) I got error message after error message on compilation.
However, that was on a computer that was pretty loaded with sketches and libraries and processor cores. Fortunately I also had a system that I just freshly put a new Linux on with just the basic 1.6.9 IDE installed, without further libraries added. I put the 85 core on it, put the Bro Hogan I2C code in the library and it immediately compiled without problems. So if you have compilation problems, it is probably because you do not have the proper libraries installed or the IDE gets confused about the libraries.
What compilation errors do you get?
-I just get a compilation error status 1
That means that for whatever reason your IDE couldnt link and compile all the required modules
-It tells me there is no 'int' for my 't_backlight'
It is pulling in the wrong library, most likely the Malpartida library.
-I get a bunch of USI errors
Your TinyWireM library might be corrupt or wrongly installed
-I have the right I2C library, but I suddenly see a LiquidCrystal_I2C.o file that wasnt there before + some other files.
Most likely you inadvertedly let your library manager do an update and your library got replaced

If you get errors 99% of these have to do with the wrong libraries. Make sure that in your preferences you set 'verbose reporting' and check what libraries are being linked. Are these the correct ones?
Also, whenever you change something in your libraries, close and restart your IDE, otherwise it will just keep using the old versions.

Everything compiled fine, but I just don't see anything on my screen.
Have you used this LCD before and did it work then?
There is a (blue) variable resistor on yr LCD module, try turn that, your contrast may be too low.
Did you make the right connections?
Did you connect the PSU?
Did you actually plug it in a wall socket or USB?

Adafruit has published the TinyLiquidCrystal Library especially for the Attiny85/Trinket.

1 Person Made This Project!

Recommendations

  • Made with Math Contest

    Made with Math Contest
  • Multi-Discipline Contest

    Multi-Discipline Contest
  • Robotics Contest

    Robotics Contest

32 Discussions

None
ErikA45

2 years ago

Followed instructions, but LCD backlight flashes every 2 seconds for about 1/50 of a second, and first row all blocks. When I use LiquidCrystal_attiny (dated 21/03/2015) it works.

6 replies
None
diy_blokeErikA45

Reply 2 years ago

Although I and others have repeated reproducable succes by using the program here, apparently a number of people didnt get it to work.
Might be a number of reasons for that: wrong address, wrong pins, bad luck, difference in libraries or whatever. Nevertheless I am happy you did get it to work with the attiny lib. Thanks for that feedback. Would you mind leaving a link? Thanks :-)

None
ErikA45diy_bloke

Reply 2 years ago

You probably have a point with issue in difference in libraries. Address, pins etc are ok but Arduino is not very selective in library usage, In my case I only have one single library for LiquidCrystal as downloaded from you but I would not be surprised there would be an issue. I have several other computers on which to experiment, amongst them a almost virgin one: I will try there too and post the result.

None
Ehsan BalouchErikA45

Reply 10 months ago

Could you please send the link for Attiny library you used finally to work, I'm facing the same problem. All connections are verified and library is only Hogan's one, but first line shows blocks only.

None
ErikA45Ehsan Balouch

Reply 10 months ago

Used TinyWireM from BroHogan, dated 1/21/2011; and LiquidCrystal_ATtiny v2.0, originally from BroHogan but modified to support ATtiny85. I don't know where I got them from, but type them in Google, and the first you encounter (from PlatisD) is the one. The second one same: TinyWireM BroHogan

None
ErikA45Ehsan Balouch

Reply 10 months ago

Hi, I try not to forget: currently not at my desk, + it was more than a year ago so I need to delve in my archives but for sure I have the info on my pc. If no response in a few weeks plse remind me. Grts, Erik

None
diy_blokeErikA45

Reply 2 years ago

OK would be interested to get your feedback. tnx

None
Thijxx

3 years ago

I get no errors but only black squares. Followed the instructions carefully and tested the LCD with Arduino Nano first.

2016-09-24 20.56.52.jpg2016-09-24 20.56.14.jpg2016-09-24 20.56.04.jpg
10 replies
None
iohyMir FarhadJ

Reply 2 years ago

Exactly the same happens to me, try with 0x3F and again the same problem, any ideas ?

None
Thijxxiohy

Reply 2 years ago

Here is what I did in the end: https://arduino.stackexchange.com/questions/29546/attiny85-i2c-lcd

None
iohyThijxx

Reply 2 years ago

Thanks a lot, finally it is working perfect ! Easy to follow and make it.

IMG_20170701_243708148.jpg
None
diy_blokeiohy

Reply 2 years ago

I am happy you got it working :-)

None
iohydiy_bloke

Reply 2 years ago

Hi, come back again, it is working just after loading the program, but when i disconnect the circuit from the power and reconnect the display does not work properly it is displaying only in the first row crazy data as appears in the pictures. Not doing anything else apart of unplug the usb wire from the computer and after 2 secs. plug again the same wire to the same usb port. Any idea ?

SDA -> Pin5 - Pull-up with 4,7 resistor

SCL -> Pin7 - Pull-up with 4,7 resistor

IMG_20170705_121437510.jpgIMG_20170705_121454080.jpg
None
Thijxxiohy

Reply 2 years ago

I had the same issue for a while. It is probably caused by a poor connection or the code starting the display. Try other jumper cables and try some delay before you print the text. And please try without Arduino connected, just the Tiny powered.

None
iohyThijxx

Reply 2 years ago

Was checking the paperduino (based in Attiny85) design and notice that there are pull-ups around all the pins and also some diode connections (don't understand so much what is the diode function here by my ignorance in electronics) to the pull-up. In any case plug the Attiny to my paperduino and the lcd works perfect not issues.

None
diy_blokeiohy

Reply 2 years ago

I agree withThijx, could be loose connection

I think i did not use any pull-ups as in my case the i2c module had pull ups.

Anyway, I am happy you got it working

None
diy_bloke

3 years ago

If you keep having problems with compiling download my files: Pu them all in one sketch directory (except for the picture file).
Works with IDE 1.6.9

brohogan.pngattiny85_bmp180+brohogan.png