Challenge

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

Answer-->

Dear sir/mam,

I have attached the code along with circuit diagram

Please note that, i have attached two different photos;

  1. One shows that master sends msg to slave and same displayed on slave LCD
  2. Other photo shows, master and slave sends msg to each other and same displayed on each other LCD's.

I have attached the code for 1st one only

Thank you

CODE:-

Master Code:

#include<Wire.h> //include library wire.h
#include<LiquidCrystal.h> //include library for LCD
LiquidCrystal lcdM(3,2,4,5,6,7); //configure LCD (en,rs,D4,D5,D6,D7)
int sense1; //declaring variables
int temp1;


void setup() //setup function
{
pinMode(A0,INPUT); //defining A0 pin as input pin
lcdM.begin(16,2);
lcdM.setCursor(0,0); //set LCD cursor
lcdM.print("Welcome"); //prints on LCD
lcdM.setCursor(0,1);
lcdM.print("To Master");
Serial.begin(9600); //start of serial communication
delay(1500); //delay of 1.5 sec
lcdM.clear(); //clears LCD screen
lcdM.setCursor(0,0);
lcdM.print("Loading..");
delay(1500);
Wire.begin(); //start of I2C communication

}

void loop() //infinite loop starts
{
sense1=analogRead(A0); //reads the input value at pin A0
temp1=sense1*(5000/1024.0); //converts the value into temperature
temp1=(temp1-500)/10;
byte MasterSend=temp1;
Wire.requestFrom(8,1); //request one byte from slave address 8
byte MasterRecieve1= Wire.read(); //receive a byte from slave and store into masterReceive1
Wire.beginTransmission(8); //starts transmission to slave
Wire.write(MasterSend); //sends value to slave
Wire.endTransmission(); //stops transmission
lcdM.setCursor(0,0);
lcdM.print("<<MASTER>>"); //prints on LCD of master
lcdM.setCursor(0,1);
lcdM.print("Master msg: ");
lcdM.print(MasterSend); //prints msg sent to slave
delay(500);
lcdM.clear();


}

------------------------------------------------------------------------------------------

Slave Code:-

#include<Wire.h> // include wire.h library
#include<LiquidCrystal.h>
int sense2;
int temp2;
LiquidCrystal lcd1(3,2,4,5,6,7);
byte SlaveRecieved=0;


void setup() //setup function
{
pinMode(A0,INPUT); //defining pin A0 as input
lcd1.begin(16,2);
lcd1.setCursor(0,0);
lcd1.print("Welcome");
lcd1.setCursor(0,1);
lcd1.print("To slave");
Serial.begin(9600);
delay(1500);
lcd1.clear();
lcd1.print("Loading..");
delay(1500);
lcd1.clear();
Wire.begin(8); //Begins I2C communication with slave address 8
Wire.onReceive(receiveEvent); //function call when slave recieves a msg from master
Wire.onRequest(requestEvent); //function call when master request msg from slave

}

void loop(void) //start of infinite loop
{
lcd1.setCursor(0,0);
lcd1.print("<<SLAVE>>"); //prints on LCD
lcd1.setCursor(0,1);
lcd1.print("Master msg :");
lcd1.print(SlaveRecieved); //prints data/msg received from master on LCD
delay(200); //delay of 200msec
lcd1.clear();

}
void receiveEvent(int howMany){ //This function is called when slave recieves a msg from master
SlaveRecieved=Wire.read(); //reads the received data/msg
}

void requestEvent() //This function is called when master wants data from slave
{
sense2=analogRead(A0); //reads the data at pin A0
temp2=sense2*(5000/1024.0);
temp2=(temp2-500)/10;
byte slaveSend=temp2;
Wire.write(slaveSend); //sends the data to master
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.