Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
This sketch will display a series of numbers 0-44 on a LCD screen 20x4 filling each line with consecutive
numbers until it reaches
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#define pins:
#define I2C_ADDR 0x27 // LCD address (make sure you run the I2C scanner to verify our LCD address)
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() {
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
void loop() {
//lcd.autoscroll();
lcd.print(thisChar);
lcd.leftToRight();
delay(500);
}
// set the cursor to (0, 1) on line #2
//lcd.autoscroll();
lcd.print(thisChar);
delay(500);
//lcd.autoscroll();
lcd.print(thisChar);
delay(500);
//lcd.autoscroll();
lcd.print(thisChar);
delay(500);
lcd.noAutoscroll();
lcd.clear();
Rrrrrr4 ______________________
/*
This sketch will display a series of numbers 0-44 on a LCD screen 20x4 filling each line with consecutive
numbers until it reaches
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//#define pins:
#define I2C_ADDR 0x27 // LCD address (make sure you run the I2C scanner to verify our LCD address)
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup() {
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
void loop() {
//lcd.autoscroll();
lcd.print(thisChar);
lcd.leftToRight();
delay(500);
//lcd.autoscroll();
lcd.print(thisChar);
delay(500);
//lcd.autoscroll();
lcd.print(thisChar);
delay(500);
//lcd.autoscroll();
lcd.print(thisChar);
delay(500);
lcd.noAutoscroll();
lcd.clear();
}
______________________
#include <LiquidCrystal.h>
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
void setup() {
lcd.begin(16,2);
delay(500);
void loop() {
lcd.scrollDisplayLeft();
delay(300);
lcd.scrollDisplayRight();
delay(300);
delay(500);
_________________________________________
// LiquidCrystal_I2C.h: https://github.com/johnrickman/LiquidCrystal_I2C
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.
void setup() {
lcd.init();
lcd.backlight();
void loop() {
lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
lcd.setCursor(2, 1); //Set the cursor on the third column and the second row (counting starts at 0!).
lcd.print("LCD tutorial");
_________________
1.
/*I2C_scanner
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial);
Serial.println("\nI2C Scanner");
void loop() {
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
else if (error == 4) {
Serial.print("0");
Serial.println(address, HEX);
if (nDevices == 0)
else
Serial.println("done\n");
delay(5000);
______
2.
// LiquidCrystal_I2C.h: https://github.com/johnrickman/LiquidCrystal_I2C
#include <Wire.h> // Library for I2C communication
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.
void setup() {
lcd.init();
lcd.backlight();
void loop() {
lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
lcd.setCursor(2, 1); //Set the cursor on the third column and the second row (counting starts at 0!).
lcd.print("LCD tutorial");
_______
3.
// LiquidCrystal_I2C.h: https://github.com/johnrickman/LiquidCrystal_I2C
________
4.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.
_____
5.
void setup() {
lcd.init();
lcd.backlight();
_________
6.
void loop() {
lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
lcd.setCursor(2, 1); //Set the cursor on the third column and the second row (counting starts at 0!).
lcd.print("LCD tutorial");
______
7.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
lcd.init();
lcd.backlight();
void loop() {
lcd.clear();
delay(2000);
lcd.clear();
lcd.print("13:45");
delay(2000);
_______
8.
#include <LiquidCrystal_I2C.h>
void setup() {
lcd.init();
lcd.backlight();
lcd.print("Hello World!");
}
void loop() {
lcd.cursor();
delay(500);
lcd.noCursor();
delay(500);
_______
9.
#include <LiquidCrystal_I2C.h>
void setup() {
lcd.init();
lcd.backlight();
lcd.print("blink() example");
void loop() {
lcd.blink();
delay(5000);
lcd.noBlink();
delay(2000);
}
________
10.
#include <LiquidCrystal_I2C.h>
void setup() {
lcd.init();
lcd.backlight();
lcd.print("Blinking text");
void loop() {
lcd.display();
delay(2000);
lcd.noDisplay();
delay(2000);
________
11.
#include <LiquidCrystal_I2C.h>
lcd.init();
lcd.backlight();
lcd.print("Hello World!");
void loop() {
lcd.scrollDisplayLeft();
delay(500);
_______
12.
#include <LiquidCrystal_I2C.h>
void setup() {
lcd.init();
lcd.backlight();
void loop() {
lcd.autoscroll();
lcd.setCursor(20, 0);
delay(500);
lcd.clear();
__________
13.
/* Arduino example code to display custom characters on I2C character LCD. More info:
www.makerguides.com */
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.
byte Heart[] = {
B00000,
B01010,
B11111,
B11111,
B01110,
B00100,
B00000,
B00000
};
byte Bell[] = {
B00100,
B01110,
B01110,
B01110,
B11111,
B00000,
B00100,
B00000
};
byte Alien[] = {
B11111,
B10101,
B11111,
B11111,
B01110,
B01010,
B11011,
B00000
};
byte Check[] = {
B00000,
B00001,
B00011,
B10110,
B11100,
B01000,
B00000,
B00000
};
byte Speaker[] = {
B00001,
B00011,
B01111,
B01111,
B01111,
B00011,
B00001,
B00000
};
byte Sound[] = {
B00001,
B00011,
B00101,
B01001,
B01001,
B01011,
B11011,
B11000
};
byte Skull[] = {
B00000,
B01110,
B10101,
B11011,
B01110,
B01110,
B00000,
B00000
};
byte Lock[] = {
B01110,
B10001,
B10001,
B11111,
B11011,
B11011,
B11111,
B00000
};
void setup() {
lcd.init();
lcd.backlight();
lcd.createChar(0, Heart);
lcd.createChar(1, Bell);
lcd.createChar(2, Alien);
lcd.createChar(3, Check);
lcd.createChar(4, Speaker);
lcd.createChar(5, Sound);
lcd.createChar(6, Skull);
lcd.createChar(7, Lock);
lcd.clear();
lcd.print("Custom Character");
}
// Print all the custom characters:
void loop() {
lcd.setCursor(0, 1);
lcd.write(0);
lcd.setCursor(2, 1);
lcd.write(1);
lcd.setCursor(4, 1);
lcd.write(2);
lcd.setCursor(6, 1);
lcd.write(3);
lcd.setCursor(8, 1);
lcd.write(4);
lcd.setCursor(10, 1);
lcd.write(5);
lcd.setCursor(12, 1);
lcd.write(6);
lcd.setCursor(14, 1);
lcd.write(7);
_________
14.
byte Heart[] = {
B00000,
B01010,
B11111,
B11111,
B01110,
B00100,
B00000,
B00000
};
________
15.
lcd.createChar(0, Heart);
lcd.createChar(1, Bell);
lcd.createChar(2, Alien);
lcd.createChar(3, Check);
lcd.createChar(4, Speaker);
lcd.createChar(5, Sound);
lcd.createChar(6, Skull);
lcd.createChar(7, Lock);
_______
16.
lcd.setCursor(0, 1);
lcd.write(0);
_____________________
terry@yourduino.com */
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
#include <LiquidCrystal_I2C.h>
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
lcd.print("Hello, world!");
delay(1000);
lcd.setCursor(2,1);
lcd.print("From YourDuino");
delay(1000);
lcd.setCursor(0,2);
lcd.setCursor(0,3);
delay(2000);
lcd.print("http://YourDuino.com");
delay(8000);
// Wait and then tell user they can start the Serial Monitor and type in characters to
lcd.setCursor(0,1);
{
{
if (Serial.available()) {
delay(100);
lcd.clear();
lcd.write(Serial.read());
/* ( THE END ) */
________________
#include <Wire.h>
// LCD geometry
void setup()
void loop()
__________________________________________
On previous tutorials on our website, we have covered the use of several displays, LCDs, and TFTs, with
diverse Arduino boards. From Nokia 5110 LCD display to different types of OLEDs, the reason for the
tutorials has been to ensure that, as a reader, you know how to use many of the most popular displays
so this help you make the best choice when trying to select the perfect display for your project. For
today’s tutorial, we will continue in that line and examine how to use the 20×4 I2C Character LCD
Display with Arduino.
A summary of some of the features of the LCD display is provided in the table below.
To demonstrate how to use this display, we will build a real-time clock which will display date and time
on the LCD. To generate and keep track of date and time, we will use the DS3231 Real time clock. We
covered the use of the DS3231 RTC module in the tutorial on DS3231 based Real-time Clock, you can
check it out to learn more about its use with the Arduino.
REQUIRED COMPONENTS
Arduino Uno
Jumper Wires
Breadboard
Power Banks
The exact component used for this tutorial can be bought via the links attached and the power bank is
only required to run the Arduino when not connected to the computer. You can replace this with a 9V
battery and a center-positive power jack.
SCHEMATICS
Since the display and the real-time clock are both I2C devices, they will be connected to the same pins
on the Arduino. For the Arduino Uno, the I2C pins are located on Pin A5 (SCL) and A4 (SDA). This may
differ on any of the other Arduino boards. Connect the components as shown in the schematics below;
Schematics
To make the connections, even more easier to follow, the pin connections of the components is
described below.Arduino – LCD
GND - GND
5v - VCC
A4 - SDA
A5 - SCL
GND - GND
5v - VCC
A4 - SDA
A5 - SCL
With the connections all done, we can now proceed to write the code for the project.
CODE
To write the code for this project, we will use three main libraries; the DS1307 Library to easily interface
with the DS3231 module, the liquid crystal I2C library to easily interface with the LCD display, and the
Wire library for I2C communication. While the Wire library comes built into the Arduino IDE, the other
two libraries can be downloaded and installed via the links attached to them.As mentioned during the
introduction, our task for today is to obtain time and date information from the RTC module and display
on the LCD. As usual, I will do a breakdown of the code and try to explain some of the concepts within it
that may be difficult to understand.We start the code by including the libraries that will be used. After
which we create an object of the Liquid crystal library, with the I2C address of the LCD as an argument.
The I2C address can be obtained from the seller or as described in our tutorial on using the 16×2 LCD
display to ESP32.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS1307RTC.h>
Next, we create a set of variables which comprises of byte arrays that represent custom characters to be
created and displayed. The custom characters are usually 5pixels in width and 8 pixels in height,
representing each box in the rows or columns of the LCD. The byte array represents which pixels of the
box to be turned on or off.
byte verticalLine[8] = {
B00100,
B00100,
B00100,
B00100,
B00100,
B00100,
B00100,
B00100
};
byte char2[8] = {
B00000,
B00000,
B00000,
B11100,
B00100,
B00100,
B00100,
B00100
};
byte char1[8] = {
0b00000,
0b00000,
0b00000,
0b00111,
0b00100,
0b00100,
0b00100,
0b00100
};
byte char3[8] = {
0b00100,
0b00100,
0b00100,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000
};
byte char4[8] = {
0b00100,
0b00100,
0b00100,
0b11100,
0b00000,
0b00000,
0b00000,
0b00000
};
Next, we write the void setup function and start by initializing the library using the lcd.begin() function,
with the first argument representing the number of columns, and the second argument representing the
number of rows. After this, the CreateCustomCharacters() function is called to convert the char variables
created above into characters that can be displayed on the LCD. One of the characters created is then
used to create a UI/frame which is displayed using the printFrame() function.
void setup()
lcd.begin(20,4);
createCustomCharacters();
printFrame();
With that done, we proceed to the voidloop() function.The idea behind the voidloop function is simple.
We create a variable “tm” to hold time elements and then call the RTC.read() function such that its
response is stored in tm. This is all done within an if statement which prints the time and date value
stored in tm, if a response is received from the rtc. If a response is not received, the else statement is
executed.
void loop()
{
tmElements_t tm;
if (RTC.read(tm)) {
printDate(5,1,tm);
printTime(6,2,tm);
} else {
if (RTC.chipPresent()) {
} else {
delay(9000);
To make the code easy to read and portable, this was all done with functions.The first function is the
printTime() which breaks down the time data stored in the “tm” variable to extract seconds, minutes
and hour values. These values are then displayed on the LCD using the lcd.print() function.
String seconds,minutes;
lcd.setCursor(character,line);
lcd.print(tm.Hour);
lcd.print(":");
if(tm.Minute<10)
{
minutes = "0"+String(tm.Minute);
lcd.print(minutes);
}else
lcd.print(tm.Minute);
lcd.print(":");
if(tm.Second<10)
seconds = "0"+String(tm.Second);
lcd.print(seconds);
}else
lcd.print(tm.Second);
The printDate function is similar to the printTime function. It extracts date information from the variable
tm and uses the lcd.print() function to display it.
lcd.setCursor(character,line);
lcd.print(tm.Month);
lcd.print("/");
lcd.print(tm.Day);
lcd.print("/");
lcd.print(tmYearToCalendar(tm.Year));
}
Other functions include the createCustomCharacters() and the printFrame() functions. The
createCustomCharacters() function, as the name implies, is used to create custom characters using byte
arrays. The function takes two arguments; the character number, and the variable to in which the byte
array for that character is stored. Only 7 characters can be created at once as such the character number
is usually between 1 and 7.
void createCustomCharacters()
lcd.createChar(0, verticalLine);
lcd.createChar(1, char1);
lcd.createChar(2, char2);
lcd.createChar(3, char3);
lcd.createChar(4, char4);
The printFrame() function, on the other hand, was used to create a sort of user interface for the project.
it makes use of the characters created above. Each of the custom characters created is displayed using
the lcd.write(byte(x)) function with x being the character number of the character to be displayed. The
characters are positioned on the LCD using the lcd.setCursor() function which takes numbers
representing the column and row on which the character is to be displayed, as arguments.
void printFrame()
lcd.setCursor(1,0);
lcd.print("------------------");
lcd.setCursor(1,3);
lcd.print("------------------");
lcd.setCursor(0,1);
lcd.write(byte(0));
lcd.setCursor(0,2);
lcd.write(byte(0));
lcd.setCursor(19,1);
lcd.write(byte(0));
lcd.setCursor(19,2);
lcd.write(byte(0));
lcd.setCursor(0,0);
lcd.write(byte(1));
lcd.setCursor(19,0);
lcd.write(byte(2));
lcd.setCursor(0,3);
lcd.write(byte(3));
lcd.setCursor(19,3);
lcd.write(byte(4));
The complete code for the project is available below and also attached under the download section
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS1307RTC.h>
byte verticalLine[8] = {
B00100,
B00100,
B00100,
B00100,
B00100,
B00100,
B00100,
B00100
};
byte char2[8] = {
B00000,
B00000,
B00000,
B11100,
B00100,
B00100,
B00100,
B00100
};
byte char1[8] = {
0b00000,
0b00000,
0b00000,
0b00111,
0b00100,
0b00100,
0b00100,
0b00100
};
byte char3[8] = {
0b00100,
0b00100,
0b00100,
0b00111,
0b00000,
0b00000,
0b00000,
0b00000
};
byte char4[8] = {
0b00100,
0b00100,
0b00100,
0b11100,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup()
lcd.begin(20,4);
createCustomCharacters();
printFrame();
void loop()
tmElements_t tm;
if (RTC.read(tm)) {
printDate(5,1,tm);
printTime(6,2,tm);
} else {
if (RTC.chipPresent()) {
} else {
delay(9000);
}
delay(1000);
String seconds,minutes;
lcd.setCursor(character,line);
lcd.print(tm.Hour);
lcd.print(":");
if(tm.Minute<10)
minutes = "0"+String(tm.Minute);
lcd.print(minutes);
}else
lcd.print(tm.Minute);
lcd.print(":");
if(tm.Second<10)
seconds = "0"+String(tm.Second);
lcd.print(seconds);
}else
lcd.print(tm.Second);
}
lcd.setCursor(character,line);
lcd.print(tm.Month);
lcd.print("/");
lcd.print(tm.Day);
lcd.print("/");
lcd.print(tmYearToCalendar(tm.Year));
void printFrame()
lcd.setCursor(1,0);
lcd.print("------------------");
lcd.setCursor(1,3);
lcd.print("------------------");
lcd.setCursor(0,1);
lcd.write(byte(0));
lcd.setCursor(0,2);
lcd.write(byte(0));
lcd.setCursor(19,1);
lcd.write(byte(0));
lcd.setCursor(19,2);
lcd.write(byte(0));
lcd.setCursor(0,0);
lcd.write(byte(1));
lcd.setCursor(19,0);
lcd.write(byte(2));
lcd.setCursor(0,3);
lcd.write(byte(3));
lcd.setCursor(19,3);
lcd.write(byte(4));
void createCustomCharacters()
lcd.createChar(0, verticalLine);
lcd.createChar(1, char1);
lcd.createChar(2, char2);
lcd.createChar(3, char3);
lcd.createChar(4, char4);
DEMO
As usual, go over the schematics to be sure everything is connected as it should be, then connect the
Arduino board to your PC and upload the code to it. Ensure all the libraries have been installed to avoid
errors.With the upload done, you should see the time and date is displayed on the LCD as shown in the
image below.
Demo
Different projects, come with different screen requirements. If you need to display a large amount of
information and the size is not a constraint, the 20×4 I2C display is definitely one of the options you
should consider.That’s it for today’s project guys, thanks for reading along. Feel free to reach me via the
comment section with any question about the project.The video version of this tutorial is available on
youtube.
_____________________________________________
In this page we have got two Arduino sketch. There are various code to display data on LCD2004-I2C.
/*
* Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
* Updated by Ahmad Shamshiri on July 08, 2018 at 19:14 in Ajax, Ontario, Canada
* for Robojax.com
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
void setup()
lcd.begin();
lcd.backlight();
lcd.print("Hello, world!");
void loop()
{
// Do nothing here...
This code prints values from variable or sensors with texts on the same line or in multiple lines.
/*
* Updated by Ahmad Shamshiri on July 08, 2018 at 09:20 in Ajax, Ontario, Canada
* for Robojax.com
*
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
void setup() {
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0); //
lcd.setCursor (0,1); //
lcd.setCursor (0,1); //
delay(1000);
delay(1000);
lcd.setCursor (0,1); //
void loop() {
char VoltageStr[5];
dtostrf(v, 5, 3, VoltageStr );
delay(100);
/*
*
* Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
void setup()
lcd.begin();
void loop()
while (1) {
if (blinking) {
lcd.clear();
lcd.noBlink();
blinking = false;
} else {
lcd.clear();
lcd.setCursor(0,1);
lcd.blink();
blinking = true;
delay(4000);
/*
*
* It code will allow you to enter character on the serial monitor and display it on this screen
* Updated by Ahmad Shamshiri on July 08, 2018 at 09:20 in Ajax, Ontario, Canada
* for Robojax.com
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
void setup()
lcd.begin();
lcd.backlight();
lcd.print("Robojax.com Test");
void loop()
lcd.clear();
lcd.setCursor(0,1);
lcd.setCursor(13,1);
if (Serial.available()) {
delay(100);
// Write all characters received with the serial port to the LCD.
lastChar =Serial.read();
Serial.print("Entered: ");
Serial.println(lastChar);
}//while end
}// if end
lcd.write(lastChar);// display last entered character
delay(1000);