Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Embedded c - Offline batch 2 - Kirubhakaran
//master code
#include //library for I2C protocol
#include //library for LCD display
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);//initialize the LCD
byte x=0;//global variable declaration
void setup()
{
lcd.begin(16, 2);
//to begin the LCD display
lcd.print("Master Arduino");
//to denote the master in lcd
delay(2000);
lcd.clear();
Wire.begin();
//to begin the I2C Comunication from master
}
void loop()
{
Wire.requestFrom(1, 1);
//to request data from slave
x=Wire.read();
//to read data from the slave
Wire.beginTransmission(1);//to send data to the slave
float temp = analogRead(A0);
byte tmp = map(temp,0,1023,0,127);
Wire.write(tmp);
Wire.endTransmission();//to stop transmitting data to slave
lcd.setCursor(0, 0);
//set cursor in first column first row
lcd.print("data frm slave");
//to print in the LCD display
lcd.setCursor(0, 1);
//set cursor in first column second row
lcd.print(x);
//to print the data from slave
delay(500);
lcd.clear();
//to clear the LCD screen
}
//slave code
#include //library for I2C protocol
#include //library for LCD display
LiquidCrystal lcd(12, 11, 2, 3, 4, 5);//initialize the LCD
byte y=0;//global variable declaration
void setup()
{
lcd.begin(16, 2);
//to begin the LCD display
lcd.print("Slave Arduino");
//to denote the slave in LCD
delay(2000);
lcd.clear();
Wire.begin(1);
//to begin the I2C Comunication from slave
Wire.onRequest(rqst);//to send data to master when called
Wire.onReceive(rcv);//to recieve data from master when given
}
void loop()
{
lcd.setCursor(0, 0);
//set cursor in first column first row
lcd.print("data frm master");
//to print in the LCD display
lcd.setCursor(0, 1);
//set cursor in first column second row
lcd.print(y);
//to print the data from master
delay(500);
lcd.clear();
//to clear the LCD screen
}
void rcv(int value)
{
y=Wire.read();
}//to receive data from the master
void rqst()
{
float temp = analogRead(A0);
byte tmp = map(temp,0,1023,0,127);
Wire.write(tmp);
//to write the data to the masters call.
}

