Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
//master code
// Header to use LCD functions
#include<LiquidCrystal.h>
// Header to use I2C communication
#include<Wire.h>
//pins connected to LCD
LiquidCrystal lcd(2,3,4,5,6,7);
void setup()
{
Serial.begin(9600);
//To begin I2C communication
Wire.begin(8);
//To call the send function
Wire.onRequest(send);
//To start the LCD display
lcd.begin(16,2);
//To clear the display
lcd.clear();
}
void loop()
{
//To move the cursor to the begining
lcd.setCursor(0,0);
//To display in LCD
lcd.print("SLAVE");
//To move the cursor to the next line
lcd.setCursor(0,1);
//To display in LCD
lcd.println("Mssage sent");
}
void send()
{
//creating string
char ch[17]="Slave Message ";
//To send message to Master
Wire.write(ch);
}
//slave code
// Header to use LCD functions
#include<LiquidCrystal.h>
// Header to use I2C communication
#include<Wire.h>
//pins connected to LCD
LiquidCrystal lcd(2,3,4,5,6,7);
//String to get the message
char MasterReceive[16];
void setup()
{
Serial.begin(9600);
//To begin I2C communication
Wire.begin();
lcd.begin(16,2);
lcd.clear();
}
void loop()
{
//8 address 16 characters
Wire.requestFrom(8,16);
//To move the cursor to the begining
lcd.setCursor(0,0);
//To print in display
lcd.print("MASTER");
//To move the cursor to the next line
lcd.setCursor(0,1);
int i=0;
//To check the number of bytes available
while(0<Wire.available())
{
//To print character by character
MasterReceive[i]=Wire.read();
i++;
}
//To print the received message
lcd.print(MasterReceive);
//Delay for 1 second
delay(1000);
//To end the I2C transmission
Wire.end();
}