Challenge

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

Embedded Offline-Batch2

Master code:

//Master device
//Header to use LCD functions
#include
// Header to use I2C communication
#include
//pin connected to LCD
#include
LiquidCrystal lcd(12,11,5,4,3,2);

void setup()
{
lcd.begin(16,2);//(16,2)lcd clum and row
lcd.setCursor(0,0);
lcd.print(" Hai iam Deva");
lcd.setCursor(0,1);
lcd.print("I2C ARDUINO 2 ARDUINO");
for (int i=0;i<=16;i++);
{
lcd.scrollDisplayLeft();
delay(100);
}
delay(2000);
lcd.clear();
Serial.begin(9600);
Wire.begin();// to begin I2C communication
}
void loop()
{
Wire.requestFrom(1,1);//function call by the slave 1
byte MR=Wire.read();//Read the slave 1 converted value
float MV=analogRead(A0);
byte MS=map(MV,0,1023,0,127);
Wire.beginTransmission(1);
Wire.write(MS);
Wire.endTransmission();
// to move the cursor to the begining(when to start the display)
lcd.setCursor(0,0);
// to display in LCD
lcd.print(">> The Master <<");
//to move the cursor to the next line
lcd.setCursor(0,1);
lcd.print("SlaveVal:");
lcd.print(MR);
Serial.print("Master Received from Slave");
Serial.print(MR);
//delay 1 sec
delay(600);
lcd.clear();
}

Slave code:

//Slave 1
#include
#include
//pin connected to LCD
LiquidCrystal lcd(12,11,5,4,3,2);
byte SR=0;

void setup()
{
lcd.begin(16,2);//(16,2)lcd clum and row
lcd.setCursor(0,0);
lcd.print("Hai iam Deva");
lcd.setCursor(0,1);
lcd.print("I2C ARDUINO 2 ARDUINO");
delay(2000);
lcd.clear();
Serial.begin(9600);
Wire.begin(1);// to begin I2C communication
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}

void loop()
{
lcd.setCursor(0,0);
lcd.print(">> the Slave <<");
lcd.setCursor(0,1);
lcd.print("MasterVal:");
lcd.print(SR);
delay(600);
lcd.clear();
}
void receiveEvent(int howMany)
{
// Wire.requestFrom(2,1);
SR=Wire.read();
}
void requestEvent()
{
//Reading analog data
float SV = analogRead(A0);//read the temp output value the hold the volt
byte SS = map(SV,0,1023,0,127);//10 byte converted to 8byte 
//writing converted data
Wire.write(SS);
}

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.