Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
circuit diagram

master
#include<Wire.h> //Library for I2C communication functions
#include<LiquidCrystal.h> //Library for LCD functions
LiquidCrystal lcd(2,7,8,9,10,11); //define lcd module pins(rs,en,D4,D5,D6,D7)
void setup()
{
lcd.begin(16,2); // initialize LCD display
lcd.setCursor(0,0); //set cursor at first line of display
lcd.print("voltage"); //prints in LCD
lcd.setCursor(0,1); //set cursor at second line of display
lcd.print("I2C Arduino to Arduino"); //prints in LCD
delay(2000);
lcd.clear(); //clears lcd display
Serial.begin(9600); //begins serial communication at 9600 baud rate
Wire.begin(); //begins I2C communication at pin A4,A5
}
void loop()
{
Wire.requestFrom(8,1); //requests 1 byte from slave arduino
byte MasterReceive = Wire.read(); //receives byte from slave and stores in MasterReceive
int potvalue = analogRead(A0); //reads analog value from potentiometer
byte MasterSend = map(potvalue,0,1023,0,127); //converts digital value 0-1023 to 0-127
Wire.beginTransmission(8); // start transmit to slave
Wire.write(MasterSend); //sends one byte converted pot value to slave
Wire.endTransmission(); //stops transmitting
lcd.setCursor(0,0); //sets cursor at first line of display
lcd.print(">> Master <<"); //prints in lcd
lcd.setCursor(0,1); //sets cursor at second line of display
lcd.print("SlaveVal:"); //prints Slave value in lcd
lcd.print(MasterReceive); //prints MasterReceive in lcd received from slave
Serial.println("Master Received Frome Slave"); //prints in Serial monitor
Serial.println(MasterReceive);
delay(5000);
lcd.clear();
}
slave
#include<Wire.h> //Library for I2C communication functions
#include<LiquidCrystal.h> //Library for LCD functions
LiquidCrystal lcd(2,7,8,9,10,11); //define lcd module pins(rs,en,D4,D5,D6,D7)
byte SlaveReceived = 255;
void setup()
{
lcd.begin(16,2); //initializes LCD display
lcd.setCursor(0,0); //sets cursor at first line of lcd
lcd.print("voltage"); //prints in lcd
lcd.setCursor(0,1); //sets cursor at second line of lcd
lcd.print("I2C to Arduino"); //prints in lcd
delay(5000);
lcd.clear(); //clears lcd display
Serial.begin(9600); //begins serial communication at 9600 baud rate
Wire.begin(8); //begins I2C communication with slave arduino
Wire.onReceive(receiveEvent); //function call when slave receives value from master
Wire.onRequest(requestEvent); //function call when master request value from slave
}
void loop()
{
lcd.setCursor(0,0); //sets cursor at first line of lcd
lcd.print(">> Slave <<"); //prints in lcd
lcd.setCursor(0,1); //sets cursor at second line of lcd
lcd.print("MasterVal:"); //prints in Master value in lcd
lcd.print(SlaveReceived); //prints Slaverecieved value in lcd received from master
Serial.println("Slave Received From Master"); //prints in serial monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent(int howMany) //function is called when slave receives value from master
{
SlaveReceived = Wire.read(); //used to read value received from master
}
void requestEvent() //function is called when Master wants value from slave
{
int potvalue = analogRead(A0); //reads analog value from pot (0-8V)
byte SlaveSend = map(potvalue,0,1023,0,127); //converts pot digital value
Wire.write(SlaveSend); //sends one byte converted pot value to master
}
output
