Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
//master
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
sendMessage("Hello");
delay(2000);
}
void sendMessage(String message) {
Wire.beginTransmission(8); // Use the I2C address of the slave
Wire.write(message.c_str());
Wire.endTransmission();
}
//slave
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
Wire.begin(8); // Use a unique I2C address for the slave
Wire.onReceive(receiveEvent);
lcd.begin(16, 2); // Set up the number of columns and rows on the LCD.
}
void loop() {
// Your slave Arduino loop code
}
void receiveEvent() {
String message = "";
while (Wire.available()) {
char c = Wire.read();
message += c;
}
displayMessage(message);
}
void displayMessage(String message) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
}