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

5_Using LCD1602 in Arduino

The document provides a guide on using the LCD1602 display with an Arduino UNO R4 Wi-Fi to show temperature and humidity readings from a sensor. It includes wiring diagrams, code examples, and instructions for setting up the display and sensor, as well as integrating a push button to toggle between Celsius and Fahrenheit readings. The document emphasizes the use of the I2C protocol for communication and provides detailed steps for coding and circuit assembly.

Uploaded by

ALL ENERGY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

5_Using LCD1602 in Arduino

The document provides a guide on using the LCD1602 display with an Arduino UNO R4 Wi-Fi to show temperature and humidity readings from a sensor. It includes wiring diagrams, code examples, and instructions for setting up the display and sensor, as well as integrating a push button to toggle between Celsius and Fahrenheit readings. The document emphasizes the use of the I2C protocol for communication and provides detailed steps for coding and circuit assembly.

Uploaded by

ALL ENERGY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

5.

Using LCD1602 in Arduino

Difficult Level:

A. Introduction
Display LCD1602, or 1602-character type liquid crystal display, is a kind of dot matrix module
to show letters, numbers, and characters and so on. It is ideal for displaying text/characters
only, hence the name ‘Character LCD’. The display has an LED backlight and can display 32
ASCII characters in two rows with 16 characters on each row. The I2C module converts the
signals from the Arduino into commands for the LCD. The LCD has 16x2 cells that can display
characters or symbols. Each cell consists of 5x8 dots that can be turned on or off by applying
voltage. The LCD can display different characters or symbols by turning on or off different
combinations of dots.
An I2C LCD1602 consists of a normal LCD1602 and an I2C module that is attached to the
back of the LCD. The I2C module is a chip that can expand the I/O ports of the Arduino using
the I2C protocol. The I2C protocol is a serial communication protocol that uses two wires:
SDA (serial data) and SCL (serial clock). The I2C protocol allows multiple devices to
communicate with each other using only two wires and unique addresses.

B. Display the temperature and humidity in the LCD1602

What you need


• Arduino IDE software
• 1 x Arduino UNO R4 Wi-Fi
• 1 x USB 2.0 cable type A/B
• 1 x I2C LCD 1602
• 1 x Temperature and humidity sensor Module
• 3 x Male-Male Jumper wires
• 4 x Female-Male Jumper wires
• 1 x 52Pi Bread Board

How to wiring circuit diagram?


Arduino UNO R4 WIFI Temperature and humidity sensor

3.3V VCC

GND GND

D2 DATA
Arduino UNO R4 WIFI I2C LCD 1602

5V VCC

GND GND

A4 SDA

A5 SCL

You can look at the circuit connection diagram below to connect our own devices.

Then connect the computer with the Arduino UNO R4 WIFI. Open the Arduino IDE, copy and
paste the code.
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHT11_PIN 2 // Define the pin used to connect the
sensor
#define DHTTYPE DHT11 // Define the type of the sensor

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16


column and 2 rows
DHT dht11(DHT11_PIN, DHT11); // Create a DHT object

void setup()
{
dht11.begin(); // initialize the sensor

lcd.init(); // initialize the lcd


lcd.clear(); // clear the LCD display
lcd.backlight(); // Make sure backlight is on
}

void loop()
{
// wait a few seconds between measurements.
delay(2000);

// read humidity
float humi = dht11.readHumidity();
// read temperature as Celsius
float tempC = dht11.readTemperature();
lcd.clear();

// check if any reads failed


if (isnan(humi) || isnan(tempC)) {
lcd.setCursor(0, 0); //put "Failed" in LCD1602 (0,0)
lcd.print("Failed");
} else {
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Temp: ");
lcd.print(tempC); // print the temperature
lcd.print((char)223); // print ° character
lcd.print("C");

lcd.setCursor(0, 1); // start to print at the second row


lcd.print("Humi: ");
lcd.print(humi); // print the humidity
lcd.print("%");
}
}
Library Inclusion and LCD Initialization: The LiquidCrystal I2C library is included to provide
functions and methods for LCD interfacing. Following that, an LCD object is created using the
LiquidCrystal_I2C class, specifying the I2C address, number of columns, and number of rows.
Inclusion of necessary libraries. This part of the code includes the DHT sensor library and
defines the pin number and sensor type used in this project. Creation of DHT object.
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHT11_PIN 2 // Define the pin used to connect the
sensor
#define DHTTYPE DHT11 // Define the type of the sensor

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16


column and 2 rows
DHT dht11(DHT11_PIN, DHT11); // Create a DHT object
Then we initialize in setup (), such as LCD initialization, DHT initialization.
dht11.begin(); // initialize the sensor

lcd.init(); // initialize the lcd


lcd.clear(); // clear the LCD display
lcd.backlight(); // Make sure backlight is on
The loop () function runs continuously after the setup function. Here, we read the humidity
and temperature values, and print these values to the LCD1602.
Loop(){
……… //print values to the LCD
}
If you do not need to output the temperature and humidity values, you can use this method
to change what you need to output.
lcd.setCursor(0, 1); // start to print at the second row
lcd.print("Hello world"); // what you want to display and
you need clear other display first so that you can get what
you want
Then we can upload our program into the Arduino UNO R4 WIFI. After upload successful, we
can see “temp” and “humi” in the LCD1602.
Innovations
The user can toggle the content displayed on LCD1602 with a button. Based on the previous
project, you need add a Press button and 2 x male to male jumper wires.

How to wiring circuit diagram?


Arduino UNO R4 WIFI Temperature and humidity sensor

3.3V VCC

GND GND

D2 DATA

Arduino UNO R4 WIFI I2C LCD 1602

5V VCC

GND GND

A4 SDA
A5 SCL

Arduino UNO R4 WIFI Button

D7

GND

To connect a push button to an Arduino, follow these steps:

⚫ Grab the Components: You'll need an Arduino board, a push button switch, a resistor
(typically 10kΩ), or just use 220Ω resister instead, jumper wires, and a breadboard
(optional).

⚫ Understanding the Push Button: A push button switch typically has four legs or pins.
Two of these pins are connected internally, and the other two are also connected
internally. Pressing the button connects the pairs of pins together.

⚫ Wiring the Push Button:

Connect one leg of the push button to a digital pin on the Arduino (e.g., pin 2).
Connect the other leg of the push button to the ground (GND) pin on the Arduino.
Connect one end of the resistor to the same digital pin on the Arduino where you
connected the push button.
Connect the other end of the resistor to the 5V pin on the Arduino.

⚫ Coding:

In your Arduino sketch, define the pin to which the push button is connected as an input.
You may also want to enable the internal pull-up resistor for the input pin to ensure it reads
a stable value when the button is not pressed.
⚫ Writing Code:

Read the state of the input pin connected to the push button using the digitalRead() function.
You can then use conditional statements to perform actions based on the state of the button
(pressed or not pressed).
Here's a basic example code:
const int buttonPin = 2; // The digital pin connected to the
push button
int buttonState = 0; // Variable to store the state of the
button
void setup() {
pinMode(buttonPin, INPUT); // Set the button pin as an input
digitalWrite(buttonPin, HIGH); // Enable internal pull-up
resistor
Serial.begin(9600); // Initialize serial communication
}

void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);

// Check if the button is pressed


if (buttonState == LOW) {
Serial.println("Button pressed!");
// Add your code here to perform actions when the button is
pressed
delay(500); // Debouncing delay
}
}

Let’s take a look at the actual wiring diagram of the combination experiment, but it is
recommended to refer to the wiring table for wiring.

Then connect the computer with the Arduino UNO R4 WIFI. Open the Arduino IDE, copy and
paste the code.
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#define DHT11_PIN 2 // Define the pin used to connect
the sensor
#define DHTTYPE DHT11 // Define the type of the sensor
const int BUTTON_PIN = 7; // the number of the pushbutton pin

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16


column and 2 rows
DHT dht11(DHT11_PIN, DHT11); // Create a DHT object

int lastButtonState = 0; // the previous state of button


int currentButtonState = 0; // the current state of button
int state = 0;

void setup() {
dht11.begin(); // initialize the sensor
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to
input pull-up mode
lcd.init(); // initialize the lcd
lcd.clear(); // clear the LCD display
lcd.backlight(); // Make sure backlight is on
currentButtonState = digitalRead(BUTTON_PIN);
}

void loop() {
delay(500); //Refresh the temperature and
humidity data every 0.5s
lastButtonState = currentButtonState; // save the
last state
currentButtonState = digitalRead(BUTTON_PIN); // read new
state

// read humidity
float humi = dht11.readHumidity();
// read temperature as Celsius
float tempC = dht11.readTemperature();
// read temperature as Fahrenheit
float tempF = dht11.readTemperature(true);
if (isnan(humi) || isnan(tempC) || isnan(tempF)) {
lcd.setCursor(0, 0); //put "Failed" in LCD1602 (0,0)
lcd.print("Failed");
}
if (state == 0) {
display_tempC(tempC, humi); //display temperature and
humidity
} else {
display_tempF(tempF, humi); //display Fahrenheit
temperature and humidity
}
if (currentButtonState == 0 && lastButtonState == 1)
{ //when the button is pushed
state = !state; //change state and
display another temperature
}
}

void display_tempF(float temp, float humi) {


lcd.clear(); //clear the last display, and update
the LCDdisplay
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Fahr: ");
lcd.print(temp); // print the Fahrenheit
temperature °F
lcd.print((char)223); // print ° character
lcd.print("F");

lcd.setCursor(0, 1); // start to print at the second row


lcd.print("Humi: ");
lcd.print(humi); // print the humidity
lcd.print("%");
}
void display_tempC(float temp, float humi) {
lcd.clear(); //clear the last display, and update
the LCDdisplay
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Temp: ");
lcd.print(temp); // print the temperature °C
lcd.print((char)223); // print ° character
lcd.print("C");

lcd.setCursor(0, 1); // start to print at the second row


lcd.print("Humi: ");
lcd.print(humi); // print the humidity
lcd.print("%");
}
Note: you need to remember to install the library.
In the loop () function, First I set a delay to make sure it refreshes every 500ms. Then read the
status of the button, read the data of the temperature and humidity sensor. A status bit is set
to toggle the output of the screen, and when the button is pressed next, the status bit changes.
if (state == 0) {
display_tempC(tempC, humi); //display temperature and
humidity
} else {
display_tempF(tempF, humi); //display Fahrenheit
temperature and humidity
}
Then, upload the code into your Arduino UNO R4 WIFI. After you finished upload, you can
push the button to change the temperature from Fahrenheit temperature and humidity to
Celsius temperature and humidity. (Sometimes you need to keep press several seconds then
release it, because we need to debounce for the button)

Click the button.


Congratulations!

You have already got this new skills!!! and let’s get into next chapter!

You might also like