Saturday 10 March 2018

16 x 2 I2C LCD

I2C LCD

LCDs are commonly used in projects to display various information of the system. The most common LCD is 16x2 that can display a total of 32 characters. However the use of the normal LCD is a hassle because it involves many wires, roughly about 11 pins of the LCD need to be wired. Instead I2C LCD can be used and only 4 wires are required: GND, 5V, SDA and SCL.



The address used by the I2C LCD can be found using I2CScanner program. The common address is 0x27 or 0x3F.  Check out the program at the link below.
https://playground.arduino.cc/Main/I2cScanner

The library for the LCD can be downloaded from:
https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/

The code below displays " Hello Arduino " and  the number of objects detected by the  optical sensor which is of NPN type ( active low). Active low means that when the sensor detects an object the output of the sensor goes to logic 0 ( LOW) and when it does not detect, it becomes logic 1 (HIGH).


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x3F, 2, 1, 0, 4, 5, 6, 7);

int inPin = 7;     // sensor signal pin connected to digital pin 7
int count=0;

 void setup()
{
  // Begin the LCD with the number of columns and rows
  lcd.begin(16, 2);
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.setCursor(0, 0);   // lcd.home() does the same
  lcd.print ("Hi Arduino!"); // Set cursor at beginning of second row
  lcd.setCursor(0, 1);
  lcd.print(count);
  }

 void loop()
  {
    // Set cursor at beginning column and row:
   
    val = digitalRead(inPin);     // read the input pin
    if (val==LOW)
   {
    count++;
    lcd.print(count);
   }
   else
   {
    // do nothing
   }
  }


LCD


The solution above can also be implemented using the normal LCD and the Arduino standard LCD library but this requires more wiring.
The variable resistor output is used to control the contrast of the LCD and the 220 ohm resistor is to limit the current to the backlight LED inside the LCD. Important pins of the LCD are listed below with their functions:

VSS is connected to ground
VDD is 5V supply
Vo is for the contrast adjustment by the variable resistor
 RS - Register Select (RS must be connected to Arduino port)
R/W -  GND  ( always connected to GND because we always write to LCD)
En is enable pin and must be connected to Arduino port
DB4 -DB7 are data lines and must be connected to Arduino.
A  - Anode ( connected to 5V by 220 ohms resistor
K  - Cathode ( connected to GND)


Code below is taken from Arduino examples.

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to

LiquidCrystal lcd(3, 5, 10, 11, 12, 13);
// 3 (RS), 5 (EN), 10 - 13 is for data lines
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);

}




No comments:

Post a Comment