I2C (TWI) Communication Between Arduino and an LCD Screen

Yesterday I started this project and I just now finished it.  Here’s the end result, a screen that can print anything I want:

From a hardware standpoint, these two devices have to be connected together in a certain way.  The screen needs a 3.3V power voltage to turn on, which the Arduino conveniently provides.  The screen also needs to be grounded, so that’s the black wire to the Arduino.  I2C works by using two more lines, SDA and SCL (for the data you want to send and the clock timing for that data, respectively).  SDA is the A4 pin and SCL is the A5 pin on the Arduino Uno.  So once you hook those 4 wires up to their right slots on each component, you’re good to go other than setting two jumpers on the side of the screen to configure the screen for I2C and not some other protocol (I may try a different protocol later on to see if I can get it to work, such as UART).

From a software standpoint, the Arduino has some great library files for I2C.  The basic one I used was “Wire.h”.  Basically, for any I2C communication, you have to start with a Wire.begin(), and then a Wire.beginTransmission([address of peripheral you want to talk to]);.  In our case, the screen requires a 0×48 address.  After that, you’re good to write whatever you want to the screen with a Wire.write().

Essentially, from looking at the documentation for the Arduino and the LCD screen itself, anything that is sent from the micro-controller to the screen will be printed to the screen, unless it encounters a 28 (or in hex, 0x1B, which is what I used in my code).  This special character is called “an escape character”, and allows you do fancy things, such as turn on a cursor, the backlight, or reset the screen.  After the escape character you need a “[” character and then you can do all sorts of fancy things given by the table found in the datasheet for the screen:

As you can see from my code below, I used a couple of these special instructions (to clear the screen when you first turn on the Arduino, and a cursor just for kicks and giggles):

/*
Ben Schoeler - 6/4/2012

Writing to a screen using I2C

This example shows how to write to a screen given an address
And other important instructions per the datasheet.
*/
#include "Wire.h" //gives us handy I2C functions
#define lcd_address 0x48 //found in the datasheet

void setup()
{
Wire.begin(); //required to begin talking
Wire.beginTransmission(lcd_address); //required to know what to talk to

Wire.write(0x1B); //escape instruction
Wire.write("[j"); //instruction to clear the screen
Wire.write(0x1B); //escape instruction
Wire.write("[0h");//insruction to wrap line at 16 characters
Wire.write(0x1B); //escape instruction
Wire.write("[2c");//instruction to turn the cursor on and blink

//what we actually want to write to the screen
Wire.write("This is a test of I2C protocol");

Wire.endTransmission(); //required to stop the talking
}

void loop()
{
//this is empty because I don't want to repeat anything
}

Ultimately I want to use this screen as a user interface for a keylocking mechanism. More on that later.

Posted in LCD