Вы находитесь на странице: 1из 56

/*

This sketch will display a series of numbers 0-44 on a LCD screen 20x4 filling each line with consecutive
numbers until it reaches

the end and then it starts again.

Code used for learning purposes developed by: www.HackerspaceLA.org | @hackerspacela

Code modified from Adafruit at: https://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll

This example code is in the public domain: http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll

*/

// include the library code:

#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 Rs_pin 0 // Assign pins between I2C and LCD

#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);

// initialize the library with the numbers of the interface pins

void setup() {

// set up backlight and turn on module:

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);

lcd.setBacklight(HIGH);

// set up the LCD's number of columns and rows:

lcd.begin(20, 4); //My LCD is 20x4

void loop() {

// set the cursor to (0, 0) on line #1

lcd.setCursor (0, 0);

//lcd.autoscroll();

// print from 0 to 14:

for (int thisChar = 0; thisChar < 15; thisChar++) {

lcd.print(thisChar);

lcd.leftToRight();

delay(500);

}
// set the cursor to (0, 1) on line #2

lcd.setCursor (0, 1);

// set the display to automatically scroll:

//lcd.autoscroll();

// print from 0 to 14:

for (int thisChar = 15; thisChar < 25; thisChar++) {

lcd.print(thisChar);

delay(500);

// set the cursor to (0, 1) on line #3

lcd.setCursor (0, 2);

// set the display to automatically scroll:

//lcd.autoscroll();

// print from 0 to 14:

for (int thisChar = 25; thisChar < 35; thisChar++) {

lcd.print(thisChar);

delay(500);

// set the cursor to (0, 1) on line #4

lcd.setCursor (0, 3);

// set the display to automatically scroll:

//lcd.autoscroll();

// print from 0 to 14:


for (int thisChar = 35; thisChar < 45; thisChar++) {

lcd.print(thisChar);

delay(500);

// turn off automatic scrolling

lcd.noAutoscroll();

// clear screen for the next loop:

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

the end and then it starts again.

Code used for learning purposes developed by: www.HackerspaceLA.org | @hackerspacela

Code modified from Adafruit at: https://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll

This example code is in the public domain: http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll

*/

// include the library code:

#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 Rs_pin 0 // Assign pins between I2C and LCD

#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);

// initialize the library with the numbers of the interface pins

void setup() {

// set up backlight and turn on module:

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);

lcd.setBacklight(HIGH);

// set up the LCD's number of columns and rows:

lcd.begin(20, 4); //My LCD is 20x4


}

void loop() {

// set the cursor to (0, 0) on line #1

lcd.setCursor (0, 0);

//lcd.autoscroll();

// print from 0 to 14:

for (int thisChar = 0; thisChar < 15; thisChar++) {

lcd.print(thisChar);

lcd.leftToRight();

delay(500);

// set the cursor to (0, 1) on line #2

lcd.setCursor (0, 1);

// set the display to automatically scroll:

//lcd.autoscroll();

// print from 0 to 14:

for (int thisChar = 15; thisChar < 25; thisChar++) {

lcd.print(thisChar);

delay(500);

// set the cursor to (0, 1) on line #3


lcd.setCursor (0, 2);

// set the display to automatically scroll:

//lcd.autoscroll();

// print from 0 to 14:

for (int thisChar = 25; thisChar < 35; thisChar++) {

lcd.print(thisChar);

delay(500);

// set the cursor to (0, 1) on line #4

lcd.setCursor (0, 3);

// set the display to automatically scroll:

//lcd.autoscroll();

// print from 0 to 14:

for (int thisChar = 35; thisChar < 45; thisChar++) {

lcd.print(thisChar);

delay(500);

// turn off automatic scrolling

lcd.noAutoscroll();

// clear screen for the next loop:

lcd.clear();

}
______________________

#include <LiquidCrystal.h>

//Inisialisasi pin lcd 16x2 ke arduino

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

void setup() {

// put your setup code here, to run once:

lcd.begin(16,2);

lcd.print("Hallo, welcome to my post");

delay(500);

void loop() {

// put your main code here, to run repeatedly:


for(int i =0; i<24;i++){

lcd.scrollDisplayLeft();

delay(300);

for(int i =0; i<50;i++){

lcd.scrollDisplayRight();

delay(300);

delay(500);

_________________________________________

/* I2C LCD with Arduino example code. More info: https://www.makerguides.com */

// 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, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.

void setup() {

// Initiate the LCD:

lcd.init();

lcd.backlight();

void loop() {

// Print 'Hello World!' on the first line of the LCD:

lcd.setCursor(0, 0); // Set the cursor on the first 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");

_________________

1.

/*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);

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);

______

2.

/* I2C LCD with Arduino example code. More info: https://www.makerguides.com */

// 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, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.

void setup() {

// Initiate the LCD:

lcd.init();

lcd.backlight();

void loop() {

// Print 'Hello World!' on the first line of the LCD:

lcd.setCursor(0, 0); // Set the cursor on the first 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");

_______

3.

// 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

________

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() {

// Initiate the LCD:

lcd.init();

lcd.backlight();

_________

6.

void loop() {

// Print 'Hello World!' on the first line of the LCD:

lcd.setCursor(0, 0); // Set the cursor on the first 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");

______

7.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {

lcd.init();

lcd.backlight();

void loop() {

lcd.clear();

lcd.print("Monday"); //Print the string "Hello World!"

delay(2000);

lcd.clear();

lcd.print("13:45");

delay(2000);

_______

8.

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

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>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

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>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

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>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);


void setup() {

lcd.init();

lcd.backlight();

lcd.print("Hello World!");

void loop() {

lcd.scrollDisplayLeft();

delay(500);

_______

12.

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);

void setup() {

lcd.init();

lcd.backlight();

void loop() {

lcd.autoscroll();

lcd.setCursor(20, 0);

for (int x = 0; x < 10; x++) {


lcd.print(x);

delay(500);

lcd.clear();

__________

13.

/* Arduino example code to display custom characters on I2C character LCD. More info:
www.makerguides.com */

// Include the library:

#include <LiquidCrystal_I2C.h>

// Create lcd object of class LiquidCrystal_I2C:

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.

// Make custom characters:

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() {

// Initialize LCD and turn on the backlight:

lcd.init();

lcd.backlight();

// Create new characters:

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);

// Clear the LCD screen:

lcd.clear();

// Print a message to the lcd:

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.

// Make custom characters:

byte Heart[] = {

B00000,

B01010,

B11111,

B11111,

B01110,

B00100,

B00000,

B00000

};

________

15.

// Create new characters:

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);

_____________________

/* YourDuino.com Example Software Sketch

20 character 4 line I2C Display

Backpack Interface labelled "YwRobot Arduino LCM1602 IIC V1"

Connect Vcc and Ground, SDA to A4, SCL to A5 on Arduino

terry@yourduino.com */

/*-----( Import needed libraries )-----*/

#include <Wire.h> // Comes with Arduino IDE

// Get the LCD I2C Library here:

// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

// Move any other LCD libraries to another folder or delete them

// See Library "Docs" folder for possible commands etc.

#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/

/*-----( Declare objects )-----*/

// set the LCD address to 0x27 for a 20 chars 4 line display

// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol

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

/*-----( Declare Variables )-----*/

void setup() /*----( SETUP: RUNS ONCE )----*/

Serial.begin(9600); // Used to type in characters

lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight

// ------- Quick 3 blinks of backlight -------------

for(int i = 0; i< 3; i++)

lcd.backlight();

delay(250);

lcd.noBacklight();

delay(250);

lcd.backlight(); // finish with backlight on

//-------- Write characters on the display ------------------

// NOTE: Cursor Position: Lines and Characters start at 0


lcd.setCursor(3,0); //Start at character 4 on line 0

lcd.print("Hello, world!");

delay(1000);

lcd.setCursor(2,1);

lcd.print("From YourDuino");

delay(1000);

lcd.setCursor(0,2);

lcd.print("20 by 4 Line Display");

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

// Display. (Set Serial Monitor option to "No Line Ending")

lcd.setCursor(0,0); //Start at character 0 on line 0

lcd.print("Start Serial Monitor");

lcd.setCursor(0,1);

lcd.print("Type chars 2 display");

}/*--(end setup )---*/

void loop() /*----( LOOP: RUNS CONSTANTLY )----*/

{
{

// when characters arrive over the serial port...

if (Serial.available()) {

// wait a bit for the entire message to arrive

delay(100);

// clear the screen

lcd.clear();

// read all the available characters

while (Serial.available() > 0) {

// display each character to the LCD

lcd.write(Serial.read());

}/* --(end main loop )-- */

/* ( THE END ) */

________________

#include <Wire.h>

#include <hd44780.h> // main hd44780 header

#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header


hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry

const int LCD_COLS = 20;

const int LCD_ROWS = 4;

void setup()

// Print a message to the LCD

lcd.print("Hello, World!"); // 1st row

lcd.print("welcome"); // 2nd row

lcd.print("it's a nice day!"); // 3rd row

lcd.print("How are you?"); // 4th row

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.

20×4 I2C LCD Display


The 20×4 LCD display is essentially a bigger (increased number of rows and columns) version of the 16×2
LCD display with which we have built several projects. The display has room to display 20 columns of
characters on 4 rows which makes it perfect for displaying a large amount of text without scrolling. Each
of the columns has a resolution of 5×8 pixels which ensures its visibility from a substantial distance.
Asides its size, the interesting thing about this version of the display being used for today’s tutorial is the
fact that it communicates via I2C, which means we will only require 2 wires asides GND and VCC to
connect the display to the Arduino. This is possible via the Parallel to I2C module coupled to the display
as shown in picture below. The I2C module can also be bought individually, and coupled to the 16 pins
version of the display.

I2C module for LCD display

A summary of some of the features of the LCD display is provided in the table below.

20×4 I2C Display Features

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

The following components are required for this project.

Arduino Uno

20×4 I2C LCD Display

DS3231 RTC Module

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

The DS3231 is connected in the same way;Arduino – DS3231

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>

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

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.

void printTime(int character,int line, tmElements_t tm)

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.

void printDate(int character,int line, tmElements_t tm)

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>

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

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);

void printTime(int character,int line, tmElements_t tm)

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);
}

void printDate(int character,int line, tmElements_t tm)

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.

_____________________________________________

Library and Arduino code for LCD2004 display with I2C

Arduino code for LCD2004 with I2C module

In this page we have got two Arduino sketch. There are various code to display data on LCD2004-I2C.

You need to download the Liquid Crystal library.

Get Library from Gethub

Get Library from Robojax.com

Character Generator (GetHub)

Character Generator (2nd source)

Arduino Code for LCD2004 with I2C Hello World sketch

This code prints simple Hello World text on screen

/*
* 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

* Get this code from Robojax.com

* Watch video instruction for this code at:https://youtu.be/DKmNSCMPDjE

*/

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 20 chars and 4 line display

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()

// initialize the LCD

lcd.begin();

// Turn on the blacklight and print a message.

lcd.backlight();

lcd.print("Hello, world!");

void loop()
{

// Do nothing here...

Arduino Code for LCD2004 with I2C Robojax Custom sketch

This code prints values from variable or sensors with texts on the same line or in multiple lines.

/*

* * Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

* This code is basic usage of LCD2004 display with I2C

* It will display text in 4 lines each with 20 characters

* It Displays multiple float values and text on the same line

* Updated by Ahmad Shamshiri on July 08, 2018 at 09:20 in Ajax, Ontario, Canada

* for Robojax.com

* Get this code from Robojax.com

* Watch video instruction for this code at:https://youtu.be/DKmNSCMPDjE

*
*/

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 20 chars and 4 line display

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {

// initialize the LCD,

lcd.begin();

// Turn on the blacklight and print a message.

lcd.backlight();

lcd.clear();

lcd.setCursor (0,0); //

lcd.print("Robojax LCD2004 Test");

lcd.setCursor (0,1); //

lcd.print("Please Wait - 3");

lcd.setCursor (0,1); //

delay(1000);

lcd.print("Please Wait - 2");

delay(1000);

lcd.setCursor (0,1); //

lcd.print("Please Wait - 1");


delay(1000);

void loop() {

// Robojax.com LCD2004 with I2C custom code

lcd.clear();// clearn previous values from screen

lcd.setCursor (0,0); //character zero, line 1

lcd.print("LCD2004 I2C Example"); // print text

lcd.setCursor (4,1); //character 4, line 2

lcd.print("Robojax.com"); // print text

lcd.setCursor (0,2); //character 0, line 3

lcd.print("Voltage: "); // print text

float v = 8.254;// define or get voltage

char VoltageStr[5];

dtostrf(v, 5, 3, VoltageStr );

lcd.setCursor (9,2); //character 9, line 3

lcd.print(VoltageStr); // print voltage

lcd.setCursor (14,2); //character 14, line 3

lcd.print("V"); // print V at the end of voltage

lcd.setCursor (0,3); //character 0, line 4

lcd.print("X: "); // print x


float xdeg = -123.87;// define or get x degree (just example)

lcd.setCursor (3,3); //character 8, line 3

lcd.print(xdeg); // print xdeg value

lcd.setCursor (12,3); //character 12, line 4

lcd.print("Y: "); // print Y

float ydeg = 32.8;// define or get y degree (just example)

lcd.setCursor (15,3); //character 15, line 4

lcd.print(ydeg); // print ydeg value

delay(100);

}// loop end

Arduino Code for LCD2004 with I2C Blinking sketch

This code will display blinking cursor on the screen.

/*

*
* Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

* This code is basic usage of LCD2004 display with I2C

* It will display blinking cursor on screen

* Get this code from Robojax.com

* Watch video instruction for this code at:https://youtu.be/DKmNSCMPDjE

*/

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 20 chars and 4 line display

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()

// initialize the LCD

lcd.begin();

void loop()

bool blinking = true;


lcd.cursor();

while (1) {

if (blinking) {

lcd.clear();

lcd.print("No cursor blink");

lcd.noBlink();

blinking = false;

} else {

lcd.clear();

lcd.setCursor(0,1);

lcd.print("Your name: ");

lcd.blink();

blinking = true;

delay(4000);

Arduino Code for LCD2004 with I2C Serial monitor sketch

This code prints characters you type on the serial monitor.

/*
*

* Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library

* This code is basic usage of LCD2004 display with I2C

* 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

* Get this code from Robojax.com

* Watch video instruction for this code at:https://youtu.be/DKmNSCMPDjE

*/

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 20 chars and 4 line display

LiquidCrystal_I2C lcd(0x27, 20, 04);

char lastChar ='_';

void setup()

lcd.begin();

lcd.backlight();

lcd.print("Robojax.com Test");

// Initialize the serial port at a speed of 9600 baud


Serial.begin(9600);

void loop()

lcd.clear();

lcd.setCursor(0,1);

lcd.print("Enter Letter: ");

lcd.setCursor(13,1);

// If characters arrived over the serial port...

if (Serial.available()) {

// Wait a bit for the entire message to arrive

delay(100);

// Write all characters received with the serial port to the LCD.

while (Serial.available() > 0) {

lastChar =Serial.read();

Serial.print("Entered: ");

Serial.println(lastChar);

}//while end

}// if end
lcd.write(lastChar);// display last entered character

delay(1000);

}// loop end

If you found this tutorial helpful, please support me so

Вам также может понравиться