Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
////////////////////////////MASTER CODE////////////////////////////////////////////////
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define LCD_I2C_ADDRESS 0x27
LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, 16, 2);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop()
{
Wire.beginTransmission(9); // Slave Arduino address
Wire.write("Hello from Master");
Wire.endTransmission();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sent message to Slave");
delay(2000);
}
////////////////////////////SLAVE CODE////////////////////////////////////////////////
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define LCD_I2C_ADDRESS 0x27
LiquidCrystal_I2C lcd(LCD_I2C_ADDRESS, 16, 2);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
Wire.begin(9);
Wire.onReceive(receiveEvent);
}
void loop() {
// Display the received message on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Received Message: ");
lcd.setCursor(0, 1);
lcd.print(receivedMessage);
delay(2000);
}
String receivedMessage = "";
void receiveEvent()
{
while (Wire.available()) {
char c = Wire.read();
receivedMessage += c;
}
}