0% found this document useful (0 votes)
51 views

Arduino LCD Projects

This document provides instructions for connecting and using an LCD display with an Arduino board using I2C communication. It explains that I2C LCDs have fewer connection requirements than standard LCDs. It also describes how to install the LiquidCrystal_I2C library, find the LCD's I2C address, display text on the LCD, and create custom characters for the LCD.

Uploaded by

marta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Arduino LCD Projects

This document provides instructions for connecting and using an LCD display with an Arduino board using I2C communication. It explains that I2C LCDs have fewer connection requirements than standard LCDs. It also describes how to install the LiquidCrystal_I2C library, find the LCD's I2C address, display text on the LCD, and create custom characters for the LCD.

Uploaded by

marta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ARDUINO LCD

This type of LCD is ideal for displaying text and numbers, hence the name ‘character
LCD’.

The I2C LCD that we are using comes with a small add-on circuit mounted on the back of
the module.

This module features a PCF8574 chip (for I2C communication) and a potentiometer to
adjust the LED backlight.

Standard LCDs typically require around 12 connections, which can be a problem if you do
not have many GPIO pins available.

If you look closely at the LCD, you can see the small rectangles that form the individual
characters of the LCD.

Each rectangle is made up of a grid of 5×8


pixels.

How to connect the Lcd display to Arduino Uno


Adjusting the contrast of the LCD
After you have wired up the LCD, you will need to adjust the contrast of the display. On
the I2C module, you will nd a potentiometer that you can turn with a small screwdriver.

Plug in the USB connector of the Arduino to power the LCD. You should see the backlight
light up. Now rotate the potentiometer until one (16×2 LCD) or 2 rows (20×4 LCD) of
rectangles appear. You can tweak the contrast later if needed.

Installing the LiquidCrystal_I2C Arduino library


The LiquidCrystal_I2C library works in combination with the Wire.h library which allows
you to communicate with I2C devices. This library comes pre-installed with the Arduino
IDE.

To install this library, go to Tools > Manage Libraries (Ctrl + Shift + I on Windows) in
the Arduino IDE. The Library Manager will open and update the list of installed libraries.

How to nd the I2C address of my LCD?


Most I2C LCDs ship with the default address ‘0x27’, but it can be di erent depending on
the batch/manufacturer.

If this is the case, you will need to nd the actual address of the LCD before you can start
using it.

If a device is found, it will display the address in the serial monitor.

/*I2C_scanner
This sketch tests standard 7-bit addresses.
Devices with higher bit address might not be seen properly.*/

#include <Wire.h>

void setup() {
Wire.begin();

Serial.begin(9600);
while (!Serial);
Serial.println("\nI2C Scanner");
}

void loop() {
byte error, address;
int nDevices;

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

nDevices = 0;
for (address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);

fi

fi

fi

ff

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\n");
else
Serial.println("done\n");

delay(5000);
}

This example sketch will display the classic ‘Hello World!’ on the rst line of the LCD and ‘LCD
tutorial’ on the second line.

// Include the libraries:


// LiquidCrystal_I2C.h: https://github.com/johnrickman/LiquidCrystal_I2C
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD

// Wiring: SDA pin is connected to A4 and SCL pin to A5.


// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to
(0x27,20,4) for 20x4 LCD.

void setup() {
// Initiate the LCD:
lcd.init();
lcd.backlight();
}
void loop() {
// Print 'Hello World!' on the first line of the LCD:
lcd.setCursor(2, 0); // Set the cursor on the third column and first
row.
lcd.print("Hello World!"); // Print the string "Hello World!"
lcd.setCursor(2, 1); //Set the cursor on the third column and the
second row (counting starts at 0!).
lcd.print("LCD tutorial”);
}

fi

HOW TO DO YOUR OWN CARACTERS - Example in chart 8

byte customChar[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
0b00000
};

#include <LiquidCrystal.h>

// LCD pins <--> Arduino pins


const int RS = 11, EN = 12, D4 = 2, D5 = 3, D6 = 4, D7 = 5;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

byte customChar[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
0b00000
};

void setup()
{
lcd.begin(16, 2); // set up number of columns and rows

lcd.createChar(0, customChar); // create a new custom character

lcd.setCursor(2, 0); // move cursor to (2, 0)


lcd.write((byte)0); // print the custom char at (2, 0)
}

void loop()
{

ARDUINO Ultrasonic sensor and LCD

Create a distance meter with the ultrasonic sensor

You need to include the libraries: Wire.h and LiquidCrystal_I2C

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int trigPin = 9;
const int echoPin = 10;
long duracion;
int distancia;
int ul maDistancia = 0;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
lcd.init();

ti

lcd.backlight();
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duracion = pulseIn(echoPin, HIGH);
distancia = (duracion * 0.034) / 2;
Serial.println(distancia);
if(distancia != ul maDistancia)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distancia: "); // Prints string "Distance" on the LCD
lcd.print(distancia);
lcd.print(" cm");
ul maDistancia = distancia;
}
delay(500);
}

-Bi-rectional visitor counter (maybe you need to chance some pins)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define trigPin 13
#define echoPin 12
// Find LCD address for I2C in this tutorial it is 0x3f
LiquidCrystal_I2C lcd(0x3f, 16, 2);

ti

ti

int counter = 0;
int currentState1 = 0;
int previousState1 = 0;
int currentState2 = 0;
int previousState2 = 0;
int inside = 0;
int outside = 0;

void setup()
{
// initialize the LCD
lcd.begin();
//Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
lcd.setCursor(0, 0);
lcd.print("IN: ");
lcd.setCursor(8, 0);
lcd.print("OUT: ");
lcd.setCursor(0, 1);
lcd.print("Total Inside: ");
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 9){
currentState1 = 1;
}
else {
currentState1 = 0;
}
delay(100);
if(currentState1 != previousState1){
if(currentState1 == 1){
counter = counter + 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
inside = inside +1;}
lcd.setCursor(4, 0);
lcd.print(inside);
if (distance > 9 && distance <= 18){
currentState2 = 1;
}
else {

currentState2 = 0;
}
delay(100);
if(currentState2 != previousState2){
if(currentState2 == 1){
counter = counter - 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
outside = outside +1;}
lcd.setCursor(13, 0);
lcd.print(outside);
lcd.setCursor(14, 1);
lcd.print(counter);
if (counter > 9 || counter < 0){
lcd.setCursor(14, 1);
lcd.print(counter);
delay(100);
lcd.clear();
}
}

You might also like