Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Interfacing a 16x2 LCD with Two Arduinos Using I2C Communication Protocol.
Requirements:
- 2 Arduino boards (e.g., Arduino Uno)
- 16x2 LCD with I2C module
- Jumper wires
- Breadboard
- Pull-up resistors (optional, if not included in your I2C module)
Circuit Connections:
Master Arduino:
- Connect SDA (A4 for Uno) and SCL (A5 for Uno) to corresponding pins on the Slave Arduino.
- Connect GND and VCC to common ground and power.
Slave Arduino:
- Connect SDA and SCL to the LCD module’s SDA and SCL pins.
- Connect the LCD module’s VCC and GND to the Arduino's 5V and GND.
Ensure all GND pins are connected together for a common ground.
PROGRAM:#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address (0x27), 16 columns, 2 rows
// Variables
String receivedMessage = ""; // To store the message received by the Slave
// Role Selection (Uncomment one line based on the role)
// #define MASTER
#define SLAVE
void setup() {
#ifdef MASTER
// Master setup
Wire.begin(); // Initialize I2C as Master
Serial.begin(9600);
Serial.println("Master Ready");
#endif
#ifdef SLAVE
// Slave setup
Wire.begin(8); // Initialize I2C as Slave with address 8
Wire.onReceive(receiveEvent); // Register receive event
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
lcd.setCursor(0, 0);
lcd.print("Waiting...");
#endif
}
void loop() {
#ifdef MASTER
// Master logic: Send a message to the Slave
Wire.beginTransmission(8); // Start communication with Slave (address 8)
Wire.write("Hello, Slave!"); // Send the message
Wire.endTransmission(); // Stop communication
Serial.println("Message sent to Slave");
delay(1000); // Wait 1 second
#endif
#ifdef SLAVE
// Slave logic: Display the received message on the LCD
if (receivedMessage != "") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(receivedMessage);
receivedMessage = ""; // Clear the message after displaying
}
#endif
}
// Slave Event Handler
void receiveEvent(int bytes) {
#ifdef SLAVE
receivedMessage = ""; // Clear the buffer
while (Wire.available()) {
char c = Wire.read(); // Read each character
receivedMessage += c;
}
#endif
}
OUTPUT:
Master Arduino
Master Ready
Message sent to Slave
Message sent to Slave
Message sent to Slave
.......
slave Arduino
Hello, Slave!