Challenge

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

AIM:-Interfacing a 16*2 LCD with Arduino using I2C protocol.

  • CODES:-
  • MASTER CODE:-

#include //Header file for I2C communication
#include //Header file for LCD configuration
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)

void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print("MASTER LCD"); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("I2C MASTER ARDUINO"); //Prints I2C ARDUINO in LCD
delay(1500); //Delay for 1.5 seconds
for(int i=0;i<16;i++) //for loop for scroll the display
{
lcd.scrollDisplayLeft(); //lcd function for scroll display left once
delay(100);
}
lcd.clear(); //Clear 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(1,1); //request 1 byte from slave arduino
byte MasterReceive = Wire.read(); //receive a byte from the slave arduino and store in MasterReceive
float potvalue = analogRead(A0); //Reads analog value from A0
byte MasterSend = map(potvalue,0,1023,0,127); //Convert digital value (0 to 1023) to (0 to 127)
Wire.beginTransmission(1); //start transmit to slave arduino
Wire.write(MasterSend); //sends one byte to slave
Wire.endTransmission(); //stop transmitting
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print(">> the Master <<"); //Prints >> Master << at LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("slave val:- "); //Prints SlaveVal: in LCD
lcd.print(MasterReceive); //Prints MasterReceive in LCD received from Slave
Serial.println("Master Receive From Slave"); //Prints in Serial Monitor
Serial.println(MasterReceive);
delay(500);
lcd.clear();
}

  • SLAVE CODE:-

#include //Header file for I2C communication
#include //Header file for LCD configuration
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
byte SlaveReceived = 0 ;

void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print("SLAVE LCD"); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("I2C SLAVE ARDUINO"); //Prints I2C ARDUINO in LCD
delay(1500); //Delay for 1.5 seconds
for(int i=0;i<16;i++) //for loop for scroll the display
{
lcd.scrollDisplayLeft(); //lcd function for scroll display left once
delay(100);
}
lcd.clear(); //Clear LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(1); //Begins I2C communication at pin (A4,A5)
Wire.onReceive(receiveEvent); //I2C interrupts for receive and request Event
Wire.onRequest(requestEvent);
}

void loop()
{
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print(">> the slave <<"); //Prints >> the slave << at LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("master val:- "); //Prints master val:- in LCD
lcd.print(SlaveReceived); //Prints SlaveReceived in LCD received from Master
Serial.println("Slave Receive From Master"); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}

void receiveEvent(int howmany) //This Function is called when Slave receives value from master
{
SlaveReceived = Wire.read(); //Used to read value received from master and store in variable SlaveReceived
}

void requestEvent()
{
float potvalue = analogRead(A0); //Reads analog value from A0
byte SlaveSend = map(potvalue,0,1023,0,127); //Convert digital value (0 to 1023) to (0 to 127)
Wire.write(SlaveSend); //Return value to master
}

  • RESULT:-

  • From above results we can see that,when master potentiometer is varies at that time Slave LCD will show the result of master value.
  • When Slave potentiometer is varies at that time Master LCD will show the result of Slave value.
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.