Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
master code
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
char MasterReceive[16];
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
Wire.requestFrom(8, 16);
lcd.setCursor(0, 0);
lcd.print("MASTER");
lcd.setCursor(0, 1);
int i = 0;
while (0 < Wire.available()) {
MasterReceive[i] = Wire.read();
i++;
}
lcd.print(MasterReceive);
delay(1000);
Wire.end();
}
slave code
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
void setup() {
Serial.begin(9600);
Wire.begin(8);
Wire.onRequest(send);
lcd.begin(16, 2);
lcd.clear();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("SLAVE");
lcd.setCursor(0, 1);
lcd.println("Message sent");
}
void send() {
char ch[17] = "Slave Message";
Wire.write(ch);
}