Exercise 9 - Connecting a LCD (l2C) screen

1.      INTRODUCTION

A liquid-crystal display (LCD) is a flat-panel display or other electronic visual display that uses the light-modulating properties of liquid crystals. Liquid crystals do not emit light directly. Because this, it is frequently that these screens are backlit (by LED).

In this exercise LCD have 16*2(16 columns and 2 rows) dimensions. It communicates with Arduino by I2C communication. Thanks to it, you are going to need four pins instead of 16 pins if you use parallel communication.


2.      HOW TO CONNECT?

Materials:

·         Arduino UNO board

·         Some wire Jumpers

·         LCD  with I2C converter

In I2C communication exist four wires: ground, power supply (5V), SDA(Data), SCL(Clock). The last two are connected to pull-up resistors, this means that I2C communication is activated by 0 instead of 1.It isn't relevant for this exercise because Arduino have this resistor inside it.

The connection is trivial, ground and power supply to GND and 5V respectively. In Arduino UNO I2C, pins are A4 and A5 (A4 to SDA and A5 to SCL).


EXERCISE_A (Scan_screen_address)

EXERCISE_B (Print_a_message)



3.      PROGRAMMING

 

EXERCISE_A (Scan_screen_address)

To use this program is necessary to install LiquidCrystial_I2C library, because the native library for LCD screens in Arduino does not support I2C communication. To install the library, download it and copy it in the libraries folder inside Arduino installation folder e.g. C:\ProgramFiles\Arduino\libraries.

Is important to know the I2C Address of the screen. First, you must upload to Arduino I2C scanner code with the LCD screen connected. Secondly, in Serial monitor you can see the address.

I2C scanner code:

#include <Wire.h>

 

void setup()

{

  Wire.begin();

 

  Serial.begin(9600);

  Serial.println("\nI2C Scanner");

}

 

void  loop()

{

  byte error,address;

  int nDevices;

 

  Serial.println("Scanning...");

 

  nDevices=0;

  for(address=1;address<127;address++)

  {

    // The i2c_scanner uses the return value of

    // the Write.endTransmisstion to see if

    // a device did acknowledge to the 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("Unknow error at address 0x");

      if(address<16)

        Serial.print("0");

      Serial.println(address,HEX);

    }    

  }

  if(nDevices==0)

    Serial.println("No I2C devices found\n");

  else

    Serial.println("done\n");

 

  delay(5000);           // wait 5 seconds for next scan

}

 

EXERCISE_B (Print_a_message)

 

This is the program to test the LCD screen, you can find some examples on File\examples  in Arduino IDE.

First, you must configure the type of screen with LiquidCrystal_I2C( I2C address, number of columns, number of rows). Some examples of lcd.”function()” functions are:

·         begin() => Set the LCD display in the correct begin state, must be called before anything else is done.

·         clear() => Remove all the characters currently shown. Next print/write operation will start

 from the first position on LCD display.

·         home() => Next print/write operation will start from the first position on the LCD display.

·         backlight()

·         noBacklight()

·         print()

·         setCursor(column,row)

To see more functions, go to LiquidCrystal_I2C.h in library folder.

On the setup, LCD is initialized before the backlight is turned on and the message is printed.

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

 

// Set the LCD address to 0x27 for a 16 chars and 2 line display

LiquidCrystal_I2C lcd(0x27,16,2);

 

int i=0;

 

void setup()

{

  // initialize the LCD

  lcd.begin();

 

  // Turn on the blacklight and print a message.

  lcd.backlight();

  lcd.print("Im alive! ^_^");

  delay(1000);

}

 

On loop a variable “i” is incremented one by one each 1000 milliseconds. It is printed on LCD screen.

 

void loop()

{

  lcd.setCursor(0,1);

  lcd.print("                ");//Print 16 spaces to Erase row

  i=i+1;

  lcd.setCursor(0,1);

  lcd.print(i);

  lcd.print("sec");

  delay(1000);

}


Last modified: Tuesday, 21 January 2020, 10:24 AM