Menu

Attiny85 LCD with I2C

October 31, 2017 - Arduino, Attiny
Attiny85 LCD with I2C

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 an Attiny85. 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. 

LCD not working

Some 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.

BroHogan’s Library

More detail here:

Arduino Playground USIi2c – by bHogan

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. 

Required Components

ItemsDescriptionQTYPrice
Attiny85x11$
16x2 LCDx11.5$
I2C Adapter x11$
4.7K Resistorsx20.1$

Installation

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.

Attiny85 pinout

    

Don’t forget to add two 4.7K resistors between SDA,SCL and the 5v supply (required if I2C does not already have them)

SDA SCL Pull up capacitors

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:

VirtualWireM

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

https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

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

Setup Attiny85 core

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
** by
** Edward Comer
** LICENSE: GNU General Public License, version 3 (GPL-3.0)

** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
** https://bitbucket.org/fmalpartida/new-liquidcrystal 

** 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

*/
#include &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;TinyWireM.h&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;
#include &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;USI_TWI_Master.h&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;
#include &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;LCD.h&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;
#include &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;LiquidCrystal_I2C.h&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;

#define I2C_ADDR    0x27 // &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;----- 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;

LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
{
 lcd.begin (16,2); //  &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;----- My LCD was 16x2

 
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home

 lcd.print("SainSmartI2C16x2");  
}

void loop()
{
 // Backlight on/off every 3 seconds
 lcd.setCursor (0,1);        // go to start of 2nd line
 lcd.print(n++,DEC);

}

7. Done! Your Attiny85 LCD is up and running

Attiny85 LCD with I2C working