How to Add an LCD with I2C to Your Attiny85
I will start by saying that this project was result of many days of trial and error. In general it is easy to connect an I2C adapter to an LCD and drive it with a micro-controller. But this was not the case for me! My I2C adapter was refusing to cooperate with any common library. The screen was either flickering or it was displaying white squares.
I will not pretend that I know what was wrong, but based on what I read all these days I conclude to this:
Many Chinese I2C adapters have PCF8574T chip installed with the pins swapped. So my guess is that this lib is setting up the pins correctly.
Other people told me that the pins are defined the same way as are in other libraries, so if anyone know exactly what is going on feel free to post it in comments. One of the main libraries that are out there is build by BroHogan and I will add a link here just as reference. This library didn’t work for me.
More detail here:
At some point it came to me that I had a library that was working for this specific LCD with Arduino as my controller. So I start searching my computer for this code, and managed to make my LCD work with this one. Keep reading for the solution.
1.First connect the hardware together. We are going to use TinyWireM library, so we have to use pin 5 for SDA and pin 7 for SCL of Attiny.
Don’t forget to add two 4.7K resistors between SDA,SCL and the 5v supply (required if I2C does not already have them)
Note: to get the LCD’s address you must use an I2C scanner. I used this one: I2CScanner and find out that my address is 0x27.
2. Download the following libraries:
You can download this lib from Arduino IDE by selecting: Sketch -> Include Library -> Manage Libraries… Then search for the lib and install it
New LiquidCrystal by Francisco Malpartida
Download the repository from here: NewLiquidCrystal, decompress the file and place the folder under D:\userdata\<user>\My Documents\Arduino\libraries
Alternative link: fmalpartida-new-liquidcrystal
Note: Please make sure that there are no other LiquidCrystal libraries in this folder or under the path C:\Program Files (x86)\Arduino\libraries. Usually in program files there is a LiquidCrystal lib. Just delete it!
More info about this lib can be found at repository’s home page: Bitbucket
3. Install Attiny Core
Add the following link in Arduino IDE at File-> Preferences-> Additional Boards Manager URLs
Then go to Tools-> Board-> Board Manager, search for “attiny” and install the manager (I used version 1.0.2)
Restart Arduino IDE
More details are available here: highlowtech
4. We will use an Arduino as programmer for Attiny so we have to upload the following sketch to it
To program your Attiny connect it to Arduino based on the following schematic
5. Setup Attiny manager in Arduino IDE
I used an arduino nano as programmer and selected “Arduino as ISP” for that reason. Also make sure that all the board setting are the same as in the image bellow. Board: Attiny85, Processor: Attiny85, Clock: Internal 8MHz (it is very important to select internal clock), Port: whatever port arduino is connected to
6. Upload the LCD code to Attiny.
Make sure that you set the correct I2C address.
** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
** based on https://bitbucket.org/celem/sainsmart-i2c-lcd/src/3adf8e0d2443/sainlcdtest.ino
** Edward Comer
** LICENSE: GNU General Public License, version 3 (GPL-3.0)
** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
** Modified - Ian Brennan ianbren at hotmail.com 23-10-2012 to support Tutorial posted to Arduino.cc
** Written for and tested with Arduino 1.0
** NOTE: Tested on Arduino Uno whose I2C pins are A4==SDA, A5==SCL
#define I2C_ADDR 0x27 // <<----- Add your address here. Find it from I2C Scanner
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
lcd.begin (16,2); // <<----- My LCD was 16x2
// Switch on the backlight
lcd.home (); // go home
// Backlight on/off every 3 seconds
lcd.setCursor (0,1); // go to start of 2nd line
7. Done! Your Attiny85 LCD is up and running
Let me know if you experience any problems with the installation and I will try to guide you through it.