Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Interfacing a 16*2 LCD with two Arduino’s using I2C communication protocol
MASTER (ARDUINO) CODE:
#include<Wire.h>//standard library for I2C communication
#include<LiquidCrystal.h>//Library for LCD display function
LiquidCrystal lcd(2,7,8,9,10,11);//LCD module pins (RS,EN,D4,D5,D6,D7)
void setup()
{
lcd.begin(16,2);//Initialize LCD display
lcd.setCursor(0,0);//setting the cursor at first line of the LCD display
lcd.print("I2C COMMUNICATION ");//printing I2C communication in LCD
lcd.setCursor(0,1);//setting the cursor at second line of LCD Dispaly
lcd.print("START ");//printing the START on the LCD Display
delay(2000);//delay of 2 seconds
lcd.clear();//clears the LCD display
Serial.begin(9600);//Begins Serial Communicationat 9600 Baud rate
Wire.begin();//Begin I2C Communication at pins A4,A5
}
void loop()
{
Wire.requestFrom(8,1);//Requesting 1 byte from slave arduino(8)
byte MasterReceive=Wire.read();//receiving a byte from the slave arduino and store in Masterslave
int potvalue=analogRead(A0);//Reads the analog value from the potentiometer at pin A0
byte MasterSend=map(potvalue,0,1023,0,127);//Converting the digital value (0-1023) to (0-123)
Wire.beginTransmission(8);//start transmit to slave arduino(8)
Wire.write(MasterSend);//send 1 byte converted pot value to slave
Wire.endTransmission();//stop transmitting
lcd.setCursor(0,0);//setting the cursor at first line of LCD
lcd.print(">>THE MASTER<<");//prints the >>THE MASTER<< on LCD Dispaly
lcd.setCursor(0,1);//setting the cursor at second line of the LCD
lcd.print("Slave value:");//prints the Slave value on LCD Dispaly
lcd.print(MasterReceive);//print the MasterReceive in received from Slave
Serial.println("Master received from the Slave");//Printing on Serial Monitor
Serial.println(MasterReceive);// prints the value from the slave
delay(1000);//dealy of 1 second
lcd.clear();// clears the LCD display
}
SLAVE(ARDUINO)CODE:
#include<Wire.h>// Standard library of I2C communication functions
#include<LiquidCrystal.h>//Library of LCD Display
LiquidCrystal lcd(2,7,8,9,10,11);//In LCD Module pins(RS,EN,D4,D5,D6,D7)
byte SlaveReceived=0;// Intializing the SlaveReceived
void setup()
{
lcd.begin(16,2);//Intializing the LCD display
lcd.setCursor(0,0);// setting the cursor at first line of the
lcd.print("I2C COMMUNICATION ");//prints the I2C COMMUNICATION on the LCD display
lcd.setCursor(0,1);// setting the cursor at second line of LCD display
lcd.print("START ");// Prints START on the LCD display
delay(2000);//Delay of 2 seconds
lcd.clear();//Clears LCD display
Serial.begin(9600);//Begin The Serial Communication at 9600 buad rate
Wire.begin(8);//Begins I2C communication with Slave Address as 8 at pins A4,A5
Wire.onReceive(receiveEvent);//Function call when Slave receives value from master
Wire.onRequest(requestEvent);//Function call when Master request value from Slave
}/* It is same as main function */
void loop()
{
lcd.setCursor(0,0);// Setting the cursor at first line of the LCD Display
lcd.print(">>THE SLAVE<<");//Prints THE SLAVE on LCD Display
lcd.setCursor(0,1);//Setting the cursor at second line of the LCD Display
lcd.print("Master value:");// Prints Master value in LCD
lcd.print(SlaveReceived);//prints the SlaveReceved value in LCD received from Master
Serial.println("Slave Received from the Master:");// prints on the serial monitor
Serial.println(SlaveReceived);// It helps to print the value in serial monitor
delay(1000); //delay of 1 second
lcd.clear();//clears the LCD dislpay
}/* It helps to running continuous as infinite times*/
void receiveEvent(int howmany)
{
SlaveReceived=Wire.read();//used to read value received from master and store in Variable of SlaveReceived
}/* This function is called when slave receives value from Master*/
void requestEvent()
{
int potvalue=analogRead(A0);// redas the analog value from the potentiometer
byte SlaveSend=map(potvalue,0,1023,0,127);// convert potvalue digital vaue (0-1023)to (0-127)
Wire.write(SlaveSend);//sends 1 byte converted pot value to master
}/* Function is called when Master wants from slave*/