lcd and i2c - center for initiatives in jewish web viewthis is an 8 bit expander port. the arduino...

8
LCD and I 2 C Overview An LCD module has numerous applications. It can be used to display sensor data, or inform operators of impending situations. A common display module is available from Yourduino.com and numerous other sources. This devices has 2 lines of 16 characters each. This device is for displaying text, it has a backlight that requires considerable current, and its size makes it valuable for larger products. There are some curiosities about this device that can make it a bit difficult to implement. This document will go over these and walk through how to get the unit up and running. I 2 C and the Expander Board The display itself is made by Lumex and requires 16 pins to control and write to the screen. The Arduino has a limited number of digital I/O pins and connecting the display directly is impractical. To make the device useful, suppliers add the LCM1602 expander board. This board incorporates a PCF8574 chip. This is an 8 bit expander port. The Arduino sends 8 bits, one at a time, over the serial interface to this chip. The chip collects them and then puts all 8 out in parallel to the appropriate pins on the display. The expander chip requires an I 2 C interface. I 2 C or Inter- Integrated Circuit is a serial communication protocol developed by Phillips Semiconductor in 1982. More information about this interface is available in numerous sources, and a complete write up for how it works with the display is given at the end of this document. For now, it is enough to know that if you are going to talk to this display, your program must include the "wire" library.

Upload: dinhtu

Post on 03-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LCD and I2C - Center for Initiatives in Jewish Web viewThis is an 8 bit expander port. The Arduino sends 8 bits, one at a time, ... Next comes the backlight pin to turn it on or off

LCD and I2COverviewAn LCD module has numerous applications. It can be used to display sensor data, or inform

operators of impending situations. A common display module is available from Yourduino.com and numerous other sources. This devices has 2 lines of 16 characters each. This device is for displaying text, it has a backlight that requires considerable current, and its size makes it valuable for larger products.

There are some curiosities about this device that can make it a bit difficult to implement. This document will go over these and walk through how to get the unit up and running.

I2C and the Expander BoardThe display itself is made by Lumex and requires 16 pins to control and write to the screen. The Arduino has a limited number of digital I/O pins and connecting the display directly is impractical. To make the device useful, suppliers add the LCM1602 expander board. This board incorporates a PCF8574 chip. This is an 8 bit expander port. The Arduino sends 8 bits, one at a time, over the serial interface to this chip. The chip collects them and then puts all 8 out in parallel to the appropriate pins on the display.

The expander chip requires an I2C interface. I2C or Inter-Integrated Circuit is a serial communication protocol developed by Phillips Semiconductor in 1982. More information about this interface is available in numerous sources, and a complete write up for how it works with the display is given at the end of this document. For now, it is enough to know that if you are going to talk to this display, your program must include the "wire" library.

Liquid Crystal Display LibraryThere are numerous displays on the market, and numerous libraries to run them. One library for comes with the Arduino IDE and is called the LiquidCrystal library. Unfortunately it will not work with this device. In fact, most of the libraries that are available do not work with most of the devices.

The first thing one must do to use this display is to get the correct library on your computer. Begin by going to you Arduino directory - This is usually on the C drive, under Program Files (86) and in the subdirectory Arduino which contains a library directory.

C:\Program Files (x86)\Arduino\Libraries

In this folder are all the libraries that are available for your programs. Each library has a separate folder. In this directory you should find a directory called "LiquidCrystal". Delete, move or rename this folder. It has to be removed before you attempt to install the correct library.

Page 2: LCD and I2C - Center for Initiatives in Jewish Web viewThis is an 8 bit expander port. The Arduino sends 8 bits, one at a time, ... Next comes the backlight pin to turn it on or off

Download the LiquidCrystal_V1.2.1.zip from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

Now we are ready to install the new library. Open the Arduino IDE and go to Sketch -> Import Library -> Add Librarythis will open a window and allow you to add a new library to you library directory. Select theLiquidCrystal_V1.2.1.zip you have downloaded.

Now you will have a new LiquidCrystal library displayed when you go to Sketch -> Import Library

Connecting the ArduinoThere are 6 pins on the LCM1602 board - 4 on the left are connected to the Arduino. The 2 at the other end of the board supply power to the backlight and there should be small black shorting jumper connecting these two pins. If it's gone, you can you a wire to connect them, or solder then together.

The other 4 pins are GND, VCC, SDA and SCL. GND and VCC should be obvious. VCC should be 5 volts - If you use only 3.3 volts, the characters on the display will be dim. SDA is the serial data line and must be connected to A4 on the Arduino. SCL is the serial clock line an must be connected to A5. That said, things are pretty simple from here.

Writing a ProgramThis is the one we are going to be using. It is interesting to note that the example programs in this library directory will not work for a variety of reasons which will hopefully become clear as we go through a program that does work. You can copy and paste it to a sketch. This program will allow us to look at some of the functions of the LiquidCrystal_I2C library. This program probably has all the commands you will need. If you need something special, contact me and we can work through it together.

This program will allow you to types something in the serial monitor and have it come up on the display.

#include <Wire.h> // Comes with Arduino IDE#include <LiquidCrystal_I2C.h>

Page 3: LCD and I2C - Center for Initiatives in Jewish Web viewThis is an 8 bit expander port. The Arduino sends 8 bits, one at a time, ... Next comes the backlight pin to turn it on or off

// set the LCD address to 0x27 for a 16 chars 2 line display// Set the pins on the I2C chip used for LCD connections:// addr, en,rw,rs,d4,d5,d6,d7,bl,blpolLiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C addressvoid setup() /*----( SETUP: RUNS ONCE )----*/{ Serial.begin(9600); // Used to type in characters lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight // everything before this is mandatory. do not alter.// ------- Quick 3 blinks of backlight ------------- for(int i = 0; i< 3; i++) { lcd.backlight(); delay(250); lcd.noBacklight(); delay(250); } lcd.backlight(); // finish with backlight on //-------- Write characters on the display ------------------// NOTE: Cursor Position: (CHAR, LINE) start at 0 lcd.setCursor(0,0); //Start writing at character 0 on line 0 (the first line) lcd.print("Hello, world!"); delay(1000); lcd.setCursor(0,1); // Start writing at character 0 on line 1 (the next line) lcd.print("Hi User "); delay(8000);

// Wait and then tell user they can start the Serial Monitor and type in characters to// Display. (Set Serial Monitor option to "No Line Ending") lcd.clear(); lcd.setCursor(0,0); //Start at character 0 on line 0. 16 character limit lcd.print("Use Serial Mon"); lcd.setCursor(0,1); lcd.print("Type to display"); }/*--(end setup )---*/void loop() /*----( LOOP: RUNS CONSTANTLY )----*/{ { // when characters arrive over the serial port... if (Serial.available()) { delay(100); // wait a bit for the entire message to arrive lcd.clear(); // clear the screen for (int ch = 0; Serial.available() > 0; ch++) // read all the available characters one at a time { if (ch == 15)

Page 4: LCD and I2C - Center for Initiatives in Jewish Web viewThis is an 8 bit expander port. The Arduino sends 8 bits, one at a time, ... Next comes the backlight pin to turn it on or off

lcd.setCursor(0,1); lcd.write(Serial.read()); // display each character to the LCD } } }}/* --(end main loop )-- */

Line by Line Through the Program

#include <Wire.h> // Comes with Arduino IDE#include <LiquidCrystal_I2C.h>

These two lines add the include files associated with the Wire and the LiquidCrystal_I2C libraries. Wire is need to use the I2C interface. We don't actually have any wire commands, but the LiquidCrystal library will call various functions in this library so it is needed.

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

This line is called a constructor. LiquidCrystal_I2C is a class - a group of functions and variables that we will want to use. When you incorporate a class, your program needs to make a copy of it and give the copy a name. In this case, I call the copy "lcd". The name, like variable names, is arbitrary. I could have called it anything that starts with a letter and does not include any spaces or strange characters. I chose "lcd" and whenever I want to use one of the functions in the LiquidCrystal_I2C class, I will precede the call with "lcd."

We have done this before. When we use the Serial monitor, we call routines that are in the Arduino class. In this case, the constructor is inside the Arduino.h include file and is added automatically. We proceed all Serial class functions with "Serial." This tells the program where to find the function we want to use. Now when we want to use LiquidCrystal_I2C functions we will tell it to look in the "lcd" class.

Just as when we call a function, we need to supply the necessary variables that the function needs to operate - When we construct a class we also need to give it the necessary values inside parenthesis. If we look in the LiquidCrystal_I2C.h file we can find the model for the constructor and we will find out what each of these values mean.

LiquidCrystal_I2C::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 = POSITIVE )

Notice there are 10 variables - The first one is the I2C address that talks to the Expander chip. All I2C addresses are 7 bits. All PCF8574 expander chips have addresses that start 0100???B. The last three bits are set by how the chip is wired on the board. If you get a meter and look at pins 1, 2, and 3 you will find they are all tied to VCC so they are all 1s, making our address 0100111 or 0x27.

Page 5: LCD and I2C - Center for Initiatives in Jewish Web viewThis is an 8 bit expander port. The Arduino sends 8 bits, one at a time, ... Next comes the backlight pin to turn it on or off

When I2C sends a byte to the slave device, it sends 8 bits. The next 8 bits tell us which pins on the LCD display are connected to which bits in the sequence. En - enables the device, Rw determines whether we are sending data or reading a status, Rs tells whether we are sending a command or sending a character. The next 4 bits are the actual data that is to be sent. Next comes the backlight pin to turn it on or off and finally the word POSITIVE - This says the backlight turns on when the backlight bit is high.

Okay, that's a lot of stuff, but I hope it helps you understand how absolutely important it is to get this constructor exactly right.

Serial.begin(9600); // Used to type in characters lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight

Serial.begin(9600); we are very familiar with. The begin() call tells the program to turn on the Serial Monitor - The next line also calls a begin() function, but it is totally different than the first. Serial.begin() is in the Serial class whereas lcd.begin() is a totally different function from the lcd class.

Serial.begin() requires one variable, the baud rate.lcd.begin() requires two variable, the first is the number of characters on the line, the second is the number of lines.

for(int i = 0; i< 3; i++) { lcd.backlight(); delay(250); lcd.noBacklight(); delay(250); } lcd.backlight(); // finish with backlight on

This block of code shows how to turn the backlight off and on - it will blink it 3 times with 1/4 second delays between each toggle.

lcd.setCursor(0,0); //Start writing at character 0 on line 0 (the first line)

This line shows how to position the cursor - When we write to the display, we send one character at a time. The character is written at the location of the cursor then the cursor is advanced one space. The first variable is the position on the line, 0 to 15. The second is the line, 0 or 1 for our display. This call positions that cursor at the left most character on the top line.

lcd.print("Hello, world!");

This line sends a string to the display. This sends each character until the string is complete. Just as with Serial.print() we can send strings, numbers or variables.

Page 6: LCD and I2C - Center for Initiatives in Jewish Web viewThis is an 8 bit expander port. The Arduino sends 8 bits, one at a time, ... Next comes the backlight pin to turn it on or off

lcd.clear();

This instruction will erase all the characters on the display to give us a blank place to write data.

Next we move to the loop() function to look at some more instructions that will be useful. This section of the code should be reasonable to understand.

if (Serial.availible())

The if block only runs when the operator enters a string from the PC and hits return.

for (int ch = 0; Serial.available() > 0; ch++) // read all the available characters one at a time

The for loop is possibly a little different than we normally see. We initialize the "ch" variable, but the comparison section does not involve the "ch" variable. This loop runs until all the character are read out of the Serial buffer.

if (ch == 15) lcd.setCursor(0,1);

This if statement moves us to the next line if we have filled up the first one.