Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
MASTER CODE
#include <Wire.h>//including library for 12_c communication
#include <LiquidCrystal.h>//library for lcd display
LiquidCrystal lcd(12,11,5,4,3,2);//function declaration
char snd;//global variable snd for information to be send
void setup()
{
lcd.begin(16,2);//to begin the lcd communication
Wire.begin();//to begin the 12_c communication
lcd.print("SEND: ");//word "send"is displayed in the lcd screen
lcd.print("HI AJITH");//word "ajith"is displayed in the lcd screen
Wire.beginTransmission(8);//initializing the 12_c communication
Wire.write("HI AJITH");//sending the message by 12_protocol
Wire.endTransmission();//ending the 12c_transmission
}
void loop()
{
}
SLAVE CODE
#include <Wire.h>//including library for 12_c communication
#include <LiquidCrystal.h>//library for lcd display
LiquidCrystal lcd(12,11,5,4,3,2);//function declaration
char rcv;//declare the variable for receiving
void setup()
{
lcd.begin(16,2);////to begin the lcd communication
Wire.begin(8);//initialize the 12_c communication with slave adress "8"
lcd.clear();//clear the lcd
lcd.setCursor(0,0);//setcursor at the beginning of the lcd
Wire.onReceive(data_rcv);//trigerring the "data_rcv" function
}
void loop()
{
}
void data_rcv(int num_bytes)//function for reading the data received by 12_c communication
{
lcd.print("RCV: ");//display lcd with message "RCV"
while(Wire.available())//checking if any data is available
{
rcv=Wire.read();//recive in the rcv variable
lcd.print(rcv);//print that message recived on lcd
}
}