How to Use 16x2 Lcd and I2C Module With Arduino
by Eli S in Circuits > Arduino
2943 Views, 5 Favorites, 0 Comments
How to Use 16x2 Lcd and I2C Module With Arduino
this is a tutorial on how to use a 16x2 lcd and I2C module with Arduino! you will learn what it is and how it works, how to connect it, and how to code it! feel free to comment if you have any questions, I can probably help you out. so, grab your lcd and let's get started!
Supplies
Materials needed
1: Arduino nano board - you can use any Arduino board, but this one is least expensive. link: Amazon.com: Mini Nano V3.0 ATmega328P Microcontroller Board w/USB Cable for Arduino : Electronics
2: 16x2 lcd with I2C module - what we're learning about today. link: Amazon.com: Sun Founder IIC I2C TWI 1602 Serial LCD Module Display Compatible with Arduino R3 Mega 2560 16x2 : Electronics
3: F-F jumper wires - use these if you're using the nano board, otherwise get F-M jumpers instead. link: Amazon.com: Hellotronics Breadboard Jumper Wires 22AWG Pack of 120 Pieces F/F More Red and Black Jumpers, Square Head 0.1'' 10 Colors (15CM, F/F) : Electronics
4: power supply for Arduino board - use any one you want, but use battery to make the project more mobile, and make sure to get a harness that will work with the Arduino board if you use battery. link: Amazon.com: Energizer MAX 9V Batteries (2 Pack), 9 Volt Alkaline Batteries - Packaging May Vary : Health & Household
Tools Needed
1: laptop or desktop computer to write code on - use any one you want, but it cannot be a phone or iPad.
What Is a 16x2 Lcd With I2C Module?
A 16x2 lcd with I2C module is two different things put together, a 16x2 lcd and an I2C module. let's start off learning about the 16x2 lcd. lcd stands for liquid crystal display, and there used in a lot of different electronics to display information. in fact, I bet you could find a gadget in your house that has a lcd in it! a 16x2 lcd means it has two lines and each line has 16 characters. it has 16 pins on the back that are used to connect it to your Arduino. I will post a separate tutorial on how to use 16x2 lcd's without the I2C module soon. now let's talk about the I2C module! the I2C module is a module that goes on the back of a lcd and simplifies the connection a bit. it reduces the 16 pins down to 4 which makes it a LOT easier to connect. it also has a potentiator built right in to, so we don't need to put a separate one in to adjust the contrast. I2C modules have their own address but we will talk more about that when we get to coding.
How to Connect the I2C Module
Now let's talk about how to connect it! as we learned before there are four pins on the I2C module. the names ae as follows: vcc, gnd, sda, and scl. the vcc pin is 5v constant power; this will connect to the 5v pin on the Arduino or the 5v rail on the breadboard. the gnd pin is ground or negative; it will connect to the ground pin on the Arduino or the ground rail on the breadboard. the sda in is one of two communication pins, and where it gets connected will very. however, in all my projects it will get connected to the A4 pin on the Arduino. scl is the second of the two communication pins, and like sda where it gets connected will vary, but in all my projects it will get connected to the A5 pin on the Arduino.
to connect your 16x2 lcd with I2C module to any Arduino board, connect a red jumper to vcc, and black one to gnd, and a blue one to sda and scl. then you can follow the schematic I have included or follow the written instructions below.
vcc on module to 5v on Arduino board
gnd on module to gnd on Arduino board
sda on module to A4 on Arduino board
scl on module to A5 on Arduino board
awesome, you have connected you I2C module to the Arduino board!
Coding
now let's code your I2C module and start displaying information!
so, you may remember how we mentioned a I2C address in step one, so let's talk about that a little more now. each I2C module has an address, every address is not unique, but they can be different depending on which manufacture you got your module from. we need to specify the address in the code though, so we can run a script on the Arduino board that tells you the address in serial monitor. now I want to touch briefly on cyber-safety here because I know a lot of instructibles include the code for the project as a file that can be downloaded, but I want to encourage you not to do that. whale I can assure you that the files I post are safe, I cannot assure you that they have not been tampered with after they were posted. we cannot tell if hackers have modified files I posted, and you don't want to get any sort of virus whale trying to hook up an lcd. so, for safety I would highly recommend you just copy the code and paste it in your Arduino instead of downloading it; it only takes a few munities longer, but it could save your computer. ok, back to finding the address. so, copy the code below and then paste the code into your Arduino and plug in your board with the lcd connected. then open serial monitor by clicking on tools and then serial monitor. now upload the code and you should see it say found address for 2x27 or something like that (see photo for what it will say exactly). now that number is the address, so remember it or write it down!
here is the code for the address finding script:
// I2C address finder code
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("I2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found"); // print "no devices found" if no I2C modules are connected.
else
Serial.println("done");
delay(5000); // wait 5 seconds for next scan
}
ok, so now that you have the address lets learn how to actually code the lcd! copy and paste the code below into your Arduino, and then read on to see how it works.
here is the code for the lcd:
#include <LiquidCrystal_I2C.h> // includes the I2C library
LiquidCrystal_I2C lcd(0x27, 16, 2); //set the address for the I2C module, and the lines and characters per line.
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight(); // turn on the backlight
}
void loop()
{
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Hello World"); // print message at (0, 0)
lcd.setCursor(2, 1); // move cursor to (2, 1)
lcd.print("how to use 16x2 "); // print message at (2, 1)
delay(2000); // display the above for two seconds
lcd.clear(); // clear display
lcd.setCursor(3, 0); // move cursor to (3, 0)
lcd.print("lcd with I2C"); // print message at (3, 0)
lcd.setCursor(0, 1); // move cursor to (0, 1)
lcd.print("module"); // print message at (0, 1)
delay(2000); // display the above for two seconds
}
ok, so we need to download the I2C library here is the link to the file you need: LiquidCrystal I2C - Arduino Reference
download the latest version, and then open you radio and select sketch and then library's and then add.zip library. then select the downloaded file and click open. awesome, now let's define the code! line one includes the library that enables communication with the I2C module. under setup, icd.int initializes the lcd whale lcd.backlight turns on the backlight. under loop, lcd.clear clears the display, which you need to do when you are using a lcd. lcd.setcursor(0,0); sets the courser to character 0, line 0. this function is setting where the following text will be printed, try moving where the text will be printed on the lcd and play around for a bit! lcd.print(""); will print the text in the brackets on the screen. this text is customizable, but make sure it will fit on the screen. delay(2000); will hold the text on the display for two seconds, you can adjust this time to whatever you want. the rest of the code is repeat of the above functions, try adding more messages and shorter delays! when you have the code just how you want it, upload and the lcd should start displaying content after a few seconds!