Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
MASTER :
#include<LiquidCrystal.h>
#include<Wire.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup()
{
Wire.begin();
lcd.begin(16,2);
lcd.print("Master Unit :");
}
void loop()
{
Wire.requestFrom(8,14); // Read response from Slave
String A = ""; // Add characters to string
while(Wire.available()){
char Write = Wire.read();
A += Write;
}
lcd.setCursor(0,1);
lcd.print(A); // Print to LCD screen
delay(100);
}
SLAVE :
#include<Wire.h>
String sendMassage = "Slave received";
void setup()
{
Wire.begin(8);
Wire.onRequest(sendData); // Function to run when data requested from master
}
void loop()
{
delay(100);
}
void sendData()
{
byte Massage[14]; // Setup byte variable in the correct size
for (byte i = 0; i < 14; i++) // Format sendMassage as array
{
Massage[i] = (byte)sendMassage.charAt(i);
}
Wire.write(Massage,sizeof(Massage)); // Send Massage back to Master
}