Lesson 3

LCD Displays

>> Jump to: LCD Module

LCD ModuleBack to Top

Using the LCD Module with I2C

Ensure you download and install this updated driver if you get errors.
https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/LiquidCrystal_V1.2.1.zip

If nothing appears on the LCD screen after uploading a sketch, adjust the screw on the back - it is a brightness control.



In the previous lesson, you learned about using the serial monitor to output data.

Time to cut-the-cord (figuratively, not literally).
The LCD module lets you write 16 characters to the screen on each line.
There are two lines, and referencing the lines is done by starting at character 0 and line 0.



Start at first character, first line (remember, counting starts at zero).
Write "Hello World!" to the top line.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

void setup() {
  lcd.begin(16,2);
}

void loop() {
  delay(500);
  lcd.setCursor(0,0);
  lcd.print("Hello World!");
  delay(500);
  lcd.clear();
}


Add in the following code...
It will move to first character, second line, then write "This is line 2".
lcd.setCursor(0,1);
lcd.print("This is line 2");


Clear the screen
lcd.clear();


To make the "backlight" turn on:
lcd.backlight();


To make the "backlight" turn off:
lcd.noBacklight();


To make the backlight flash turn on and off repeatably, you can adjust the delay between each change.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

void setup() {
  lcd.begin(16,2);
}

void loop() {
  delay(500);
  lcd.noBacklight();
  delay(500);
  lcd.backlight();
}


Creating a Custom Character

Bytes are made of bits.
Every 1 in the code below turns on a pixel.
Every 0 turns off a pixel.
The code below draws a complete box with every pixel turned on.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

//Custom Character
byte myByte[8] = {
  B11111,
  B11111,
  B11111,
  B11111,    
  B11111,
  B11111,
  B11111,  
  B11111
};

void setup() {
  lcd.begin(16,2);
  lcd.createChar(0, myByte); //create custom byte as byte 0)
}

void loop() {
  lcd.setCursor(0,0);
  
  //Write our custom byte (ie: 0)
  lcd.write(byte(0));
  delay(500);
  lcd.clear();  
  delay(500);
}


Adding more than one Custom Character
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

//Custom Character 0
byte myByte0[8] = {
  B00100,
  B11100,
  B00100,
  B00100,    
  B00100,
  B00100,
  B00100,  
  B11111    
};

//Custom Character 1
byte myByte1[8] = {
  B01110,
  B10001,
  B00001,
  B00010,    
  B00010,
  B00100,
  B01000,  
  B11111    
};

void setup() {
  lcd.begin(16,2);
  lcd.createChar(0, myByte0); //create custom byte as byte 0)
  lcd.createChar(1, myByte1); //create custom byte as byte 1)
}

void loop() {
  lcd.setCursor(0,0);
  
  //Write our custom byte (ie: 0 or 1)
  lcd.write(byte(0));
  lcd.write(byte(1));
  delay(500);
  lcd.clear();  
  delay(500);
}


Creating a "bouncing" Custom Character
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

//Custom Character
byte myByte[8] = {
  B01110,
  B11111,
  B11111,
  B11011,    
  B11011,
  B11111,
  B11111,  
  B01110    
};

int counter = 0;
int line = 0;

void setup() {
  lcd.begin(16,2);
  lcd.createChar(0, myByte); //create custom byte as byte 0)
}

void loop() {
  //Reset counter at 16 or greater
  if(counter >= 16) {
    counter = 0;
  }
  lcd.setCursor(counter,line);
  
  //Alternate top and bottom lines
  if(line == 0) {
    line = 1;
  }
  else {
    line = 0;
  }
  
  //Write our custom byte (ie: 0)
  lcd.write(byte(0));
  delay(500);
  lcd.clear();  
  delay(500);
  counter++;
}





How to draw a smiley face

byte smiley[8] = {
  B00000,
  B10001,
  B00000,
  B00000,
  B10001,
  B01110,
  B00000,
  B00000,
};