Challenge

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

/* Master Device for

I2c Communication*/

#include<Wire.h> //inbuilt function for wire communication
#include<LiquidCrystal.h> //inbuilt function for liquidcrystal display

LiquidCrystal obj(13, 12, 5, 4, 3, 2); // creating object function for lcd and arguments as ground, power, contrast, Rgstr select, Rgstr Read/write, enable, pins, lcd cathode, lcd anode
void setup() //initializing fucntion for starting code
{
Wire.begin(1); //beginning wire commuincation at address 1
obj.begin(16, 2); //beginning lcd display 16*2
obj.setCursor(0, 0); //calling setcursor for setting cursor at 1 box 1 line
obj.print("Master"); //o/p statement
obj.setCursor(0, 1);//calling setcursor for setting cursor at 1 box 2 line
obj.print("Device-1"); //o/p statement
delay(3000); //delaying for 3000 milli seconds
obj.clear(); //clearing all the characters in lcd
Wire.onRequest(To_Slave); //function sending function to slave on requesting with address

}
void loop() //function made changes for arduino board
{
Wire.requestFrom(2, 2); //requesting data from address 2 and with a quantity of 2
obj.begin(16, 2); //again beginning lcd display with 16*2
while(Wire.available() > 1) //iterating over data present in Wire address function() greater than 1
{
int c =Wire.read(); //storing read data using read.data function in c
obj.setCursor(0, 0); //calling setcursor for setting cursor at 1 box 1 line
obj.print(c); //o/p statement for c which is a variable containing data in above statement
obj.print("->");//display statement
obj.print("Received");//o/p statement
obj.setCursor(0, 1);//calling setcursor for setting cursor at 1 box 2 line
obj.print("From Slave");//o/p statement
}
delay(5000);//delaying for 3000 milli seconds
}


void To_Slave() //called function for slave
{
int state = analogRead(A0); //storing read data from analog pin A0 and storing in state
byte store = map(state, 0, 1023, 0, 127); //mapping the values from 0-1023, 0-127 and storing in store
Wire.write(store); //writing store data for wire address
}

/* Slave Device

to receive execute communicatio from master*/

#include<Wire.h> //inbuilt function for wire communication
#include<LiquidCrystal.h>//inbuilt function for liquidcrystal display

LiquidCrystal obj(13, 12, 5, 4, 3, 2);// creating object function for lcd and arguments as ground, power, contrast, Rgstr select, Rgstr Read/write, enable, pins, lcd cathode, lcd anode
void setup() //initialize function for startup
{
Wire.begin(2); //beginning function for wire communication
Serial.begin(9600);//beginning serial monitor @ data rate of 9600
obj.begin(16, 2); //beginning lcd @ 16*2
obj.setCursor(0, 0); //setting cursor at 1 line
obj.print("Slave"); ////o/p stetement
obj.setCursor(0, 1); //setting cursor at 2 line
obj.print("Device-2"); //o/p statement
delay(2000); //delaying for 2000 milliseconds
obj.clear(); //clearing display
Wire.onRequest(To_Master); //function sending function to slave on requesting with address

}
void loop() //function made changes for arduino board
{
obj.begin(16, 2);// again after delay beginning lcd @ 16*2
Wire.requestFrom(1, 2); //requesting data from address 2 and with a quantity of 2
obj.setCursor(0, 0);//setting cursor at 1 line
obj.print("Receiving..");//o/p statement
obj.print("->");//o/p statement
while(Wire.available()>1)//iterating over data present in Wire address function() greater than 1
{
int c =Wire.read(); //storing read data using read.data function in c
obj.print(c); //o/p the read value in above statement
}
obj.setCursor(0, 1); //setting cursor at 2 line
obj.print("From Master"); //o/p statement
obj.print(""); //empty o/p
delay(5000); //delaying for 5000 milli seconds
}
void To_Master()//called function for Master
{
int state = 10;//storing read data from analog pin A0 and storing in state
Wire.write(state);//Wire value for wire address
}

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.