Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
# Interfacing a 16*2 LCD with Arduino using I2C protocol
- Objective: To send a message from a slave device to a master device using the Inter-Integrated Circuit (I2C) communication protocol on an Arduino platform, and subsequently display the message on a 16x2 LCD screen.
- Circuit
The analog pins A4 and A5 on the Arduino are used for I2C communication. Digital pin 2 is connected to the RS pin of the LCD, and digital pin 3 is connected to the E pin. Digital pins 4, 5, 6, and 7 are connected to the data pins DB1, DB2, DB3, and DB4 of the LCD. The LCD is powered with 5V and ground from the Arduino. A potentiometer is used to turn the LCD on and off.
#Code
// MASTER
#include <LiquidCrystal.h> // Header for LCD functions
#include <Wire.h> // Header for I2C communication
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Pins connected to the LCD
char MasterReceive[16]; // String to store the received message
void setup() {
Serial.begin(9600);
Wire.begin(); // Initialize I2C communication
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
Wire.requestFrom(8, 16); // Request 16 characters from slave at address 8
lcd.setCursor(0, 0); // Move cursor to the beginning
lcd.print("MASTER"); // Display "MASTER" on the LCD
lcd.setCursor(0, 1); // Move cursor to the next line
int i = 0;
while (Wire.available()) { // Check for available bytes
MasterReceive[i] = Wire.read(); // Read character by character
i++;
}
lcd.print(MasterReceive); // Display the received message
delay(1000); // Wait for 1 second
Wire.end(); // End I2C transmission
}
// SLAVE
#include <LiquidCrystal.h> // Header for LCD functions
#include <Wire.h> // Header for I2C communication
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Pins connected to the LCD
void setup() {
Serial.begin(9600);
Wire.begin(8); // Initialize I2C communication as slave with address 8
Wire.onRequest(send); // Set up the send function to be called on request
lcd.begin(16, 2); // Initialize the LCD
lcd.clear(); // Clear the display
}
void loop() {
lcd.setCursor(0, 0); // Move cursor to the beginning
lcd.print("SLAVE"); // Display "SLAVE" on the LCD
lcd.setCursor(0, 1); // Move cursor to the next line
lcd.println("Message sent "); // Display message on the LCD
}
void send() {
char ch[17] = "Slave Message "; // Create message string
Wire.write(ch); // Send message to master
}
# Output:
The message sent from slave is received by master and displayed in 16*2 LCD. The communication between the master and slave arduino was implemented using I2C protocol.