5_Using LCD1602 in Arduino
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.
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
void setup()
{
dht11.begin(); // initialize the sensor
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();
3.3V VCC
GND GND
D2 DATA
5V VCC
GND GND
A4 SDA
A5 SCL
D7
GND
⚫ 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.
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);
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
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
}
}
You have already got this new skills!!! and let’s get into next chapter!