Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Interfacing an LCD with I2C on Arduino

In this article, we will explore how to interface an LCD display with an Arduino using the I2C protocol. The I2C protocol simplifies the wiring and communication process, making it easier to connect multiple devices with fewer pins. This is particularly important for projects with limited GPIO pins or complex wiring requirements. By using an I2C LCD, you can significantly reduce the number of pins needed to control the display from 6 to just 2.

Project: In this project, we will create a simple system that displays text on an LCD screen using the I2C interface. The objective is to demonstrate how to initialize the LCD, send text to be displayed, and update the display dynamically. This project will be useful for anyone looking to add a user interface to their Arduino projects, such as displaying sensor readings, status messages, or any other relevant information.

Components List:

  • Arduino Uno (1)
  • I2C LCD Display (16x2) (1)
  • Jumper Wires (Male to Female) (4)
  • Breadboard (1)
  • USB Cable for Arduino (1)

Examples:

// Include the Wire library for I2C communication
#include <Wire.h>

// Include the LiquidCrystal_I2C library for the LCD
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // Initialize the LCD
  lcd.begin();
  // Turn on the backlight
  lcd.backlight();
  // Print a message to the LCD
  lcd.setCursor(0, 0); // Set cursor to column 0, line 1
  lcd.print("Hello, World!");
  lcd.setCursor(0, 1); // Set cursor to column 0, line 2
  lcd.print("I2C LCD Display");
}

void loop() {
  // Example of dynamic content update
  lcd.setCursor(0, 1); // Set cursor to column 0, line 2
  lcd.print(millis() / 1000); // Display the number of seconds since the Arduino was reset
  delay(1000); // Update every second
}

Explanation:

  1. Include Libraries: The Wire.h library is used for I2C communication, and the LiquidCrystal_I2C.h library is used for controlling the LCD.
  2. Initialize LCD: The LiquidCrystal_I2C lcd(0x27, 16, 2); line initializes the LCD with the I2C address 0x27 and specifies a 16x2 display.
  3. Setup Function: In the setup() function, the lcd.begin(); initializes the LCD, and lcd.backlight(); turns on the backlight. The lcd.setCursor(0, 0); and lcd.print("Hello, World!"); lines set the cursor to the first row and print the message. Similarly, the second message is printed on the second row.
  4. Loop Function: The loop() function demonstrates how to dynamically update the display. The lcd.setCursor(0, 1); sets the cursor to the second row, and lcd.print(millis() / 1000); prints the number of seconds since the Arduino was reset. The delay(1000); ensures the display updates every second.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.