Using an I2C LCD with Arduino

Lets be honest, Arduino is a great platform. You can easily do very interesting things, but to go beyond flashing leds, or having a computer around to read your program's output from the serial monitor seems like a very big obstacle for many people. LCD screens are cheap and easily available, but they usually come with a big overhead in GPIO pins to make them work.

This is not a problem if you need only one or two pins to interface with the outside world, but the moment you need a lot sensors or other devices, this can become a problem.

A very attractive solution is the I2C Bus.

I2C is a two wire data transport bus that works in a master-slave configuration. Every slave has a unique address that the master can use to send commands or retrieve data from the slave device.

You can learn more about the actual protocol here: https://www.i2c-bus.org/

The Arduino ecosystem has support for the protocol in the form of the Wire.h library,as well as through certain pins on the development boards that provide access to the bus.

On the Arduino UNO, these pins are A4 and A5, as well as the dedicated SCL and SDA pins on the top of the board, near the USB connector. It is very important to note that I2C does not operate on the same principals as a serial UART. There are no dedicated Receive and Transmit Pins, and you also DO NOT Swap SDA and SCL on the slave and master. [On a Serial UART, it is normal to connect Rx to Tx and Tx to Rx -- DO NOT DO THIS WITH I2C ]

SDA is your DATA Line, and SCL is the CLOCK line.

There are many I2C interface modules available these days that allow you to interface a character LCD with your Arduino, or almost any other microprocessor that has i2c interface peripheral hardware. These modules are around a dollar (1USD) a piece and very easy to use.

A link to a module that I use myself is available here : https://th.cytron.io/p-i2c-module-for-character-lcd?search=i2c&description=1&src=search.list

Or if you want an already assembled unit, here :https://th.cytron.io/c-electronic-components/c-led-lcd/c-lcd/p-i2c-1602-serial-lcd-for-arduino-and-rpi?src=category

Another resource that you will need to make this work, is the Liquid_Crystal_I2C library for the Arduino IDE. The Wire.h library is already built-in, but this provides low-level hardware access to the i2c peripherals. It is much easier to use the dedicated Liquid Crystal library to get you started as quick as possible.

It is available for download here: https://cdn.instructables.com/ORIG/FVH/K8OQ/J8UH0B9U/FVHK8OQJ8UH0B9U.zip

Ok, let's get started with connecting and using our i2c LCD Module.

Please refer to the images of the I2C LCD Module, in particular the back view, as this is where we will make our connections to the module. This particular LCD module needs 5v to operate correctly. It is also a good idea to power it from an external 5v source if you have a lot of other external components, so that you do not overload your computer's usb port, possibly damaging it ;)

The pinouts from top to bottom is:

GND (Ground ) - Connect to any GND pin on your Arduino

VCC (+5v) - Connect to the +5v pin on your Arduino, or power externally with +5v

SDA - Connect to SDA pin on your Arduino

SCL - Connect to SCL pin on your Arduino

Double check your connections, and then start up your Arduino IDE. Do not connect power or the USB cable to your Arduino board yet.

Click on Sketch -> Include Library -> Add .ZIP Library and browse to the zipped library that you have downloaded before. Click on Ok, and the Arduino IDE will add the library to its list of available libraries.

Now, connect your Arduino board to the computer with the USB cable. Do not power the Arduino with external power at the same time! You will damage your computer and or Arduino or both !

Use a small flat screwdriver to adjust the contrast of the LCD so the you can see clearly. You should adjust is so that the black blocks are just visible.

You are now ready to start coding !

Type the following code into the Arduino IDE:

/*

* I2C LCD Example

* Maker and IOT Ideas 2020

*/


#include <Wire.h>

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);

/*

* LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs,

uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7,

uint8_t backlighPin, t_backlighPol pol);

*/



void setup() {

lcd.begin(16,2); // Initialize the LCD, 16 charachers, 2 rows

lcd.backlight(); // switch on the backlight

// put your setup code here, to run once:

lcd.setCursor(0,0); // Set cursor to Position 0, Row 0

lcd.print("Maker IOT Ideas");

lcd.setCursor(0,1); // Set cursor to Position 0, Row 1

lcd.print("@makeriot2020");

}


void loop()
{
}

Compile and Upload your code to the Arduino, the result should be as in the picture below.

Well done. You have done it! Now, lets look at some more functions that the library expose to use to control the LCD. Many tutorials often overlook this part, but I believe it is quite important to tell you about all of them, so that you can have greater control over your LCD, with the minimum of effort.

lcd.backlight() -> Switch on the backlight

lcd.nobacklight -> Switch off the backlight

lcd.on() ->Switch on the display, and the backlight

lcd.off() ->Switch off the display

lcd.clear() ->clear the entire display

lcd.clear(start-char,line,number of characters)

example lcd.clear(6,0,4) -> this means clear 4 characters,

starting at character 7 on line 0


(Remember, counting starts at 0 )

That concludes this very long, but hopefully informative tutorial.

I hope that you learned something.