I2C Arduino Code for LCD Displays & Adaptor Modules
Click Here to watch our YouTube Video for a full description
To use an I2C ready LCD Display or I2C Backpack Adaptor Module with Arduino you will need the Wire Library and a Library for I2C LCD.
In our video, we used the NewLiquidCrystal Library. Wire is already included in the Arduino IDE but you will need to download NewLiquidCrystal - click here to download:
NewLiquidCrystal
UPDATE:
Since that video was made, we have switched to using the hd44780 Library:
Created by Bill Perry 2016-07-02
bperrybap@opensource.billsworld.billandterrie.com
This example code is unlicensed and is released into the public domain
Read more about this library here:
https://www.arduino.cc/reference/en/libraries/hd44780/
and the documentation on gitHub is here:
https://github.com/duinoWitchery/hd44780
- The best way to get the library is to open your Arduino software and go to the menu:
Sketch/Include Library/Manage Libraries
In the library manager, search for hd44780 and then download it - it will soon be ready to use and will appear in your libraries menu.
The example sketches will then also be available in the Open file dialogue.
- This library will autodetect your I2C displays and you do not need to specify the address of the device.
- If you want to use more than one display, they need to have different addresses (more on this in the video) and this library can either autodetect them or you can specify the addresses (detail on this is in the gitHub documentation and in the example sketches.
Here's a short Arduino Sketch you can copy and paste to show the basic set up of an LCD Display using the hd44780 library:
/*
WWW.Q26.CO.UK
I2C hd44780 Library Demo Code
********************************************************************************
Acknowledgements:
Created by Bill Perry 2016-07-02
bperrybap@opensource.billsworld.billandterrie.com
This example code is unlicensed and is released into the public domain
----------------------------------------------------------------------------
*/
//Libraries
#include <Wire.h>
#include <hd44780.h> //replaces LiquidCrystal
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
//LCD Display initialise:
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void setup() {
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
lcd.setBacklight(HIGH);
//Initial Display
lcd.setCursor(0, 0);
lcd.print("Hello World");
delay(3000);
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("hd44780 Library");
lcd.setCursor(0, 1);
lcd.print("...working here");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("COls & ROWS");
lcd.setCursor(0, 1);
lcd.print("16 & 2");
delay(3000);
}