Challenge

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

Overview of the Project:

In this project implementing the Code for “Interfacing a 16*2 LCD with two Arduino’s (one for Master and one for Slave) using Inter-Integrated Circuit (I2C) communication protocol” by sending the message to LCD using I2C communication protocol and display the message on LCD screen.

Goals:

1)Communication between Arduino and 16*2 LCD using I2C protocol.

2)Displaying the message sent by Master and received by slave i.e. LCD display.

Key Highlights of the Project:

1)Using the inter-integrated circuit communication protocol, transferring the data i.e. the message we want to display is sent to the LCD.

2)Two Arduino’s connected to the LCD display and the communication is executed using I2C protocol between Arduino and the LDC display.

Deliverables of this project:

1)Communication between the Arduino’s and LCD using I2C.

2)The message passed from master to slave and displayed on the 16*2 LCD.

Code:-

MASTER CODE:-

// Master Code
#include<LiquidCrystal.h>//Library for LCD display function
#include<Wire.h> //header for I2C Communication
LiquidCrystal lcd(12,11,5,4,3,2);//Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
int val1;
int portvalue;
void setup()
{
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print("Project1 Code"); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("I2C 2 ARDUINO"); //Prints I2C ARDUINO in LCD
delay(10000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin();//To begin I2C Communication
}
void loop()
{
Wire.requestFrom(1,1);//Request sent From master to slave-1
val1=Wire.read(); //Reading the request and store at X
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
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 (8)
Wire.write(MasterSend); // sends one byte converted POT value to slave
Wire.endTransmission(); // stop transmitting
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print(">> Master <<"); //Prints >> Master << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print("SlaveVal:"); //Prints SlaveVal: in LCD
lcd.print(val1); //Prints MasterReceive in LCD received from Slave
Serial.println("Master Received From Slave"); //Prints in Serial Monitor
Serial.println(val1);
delay(5000);
lcd.clear();

}

SLAVE CODE:-

#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(12,11,5,4,3,2); //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("Project1 Code"); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("I2C 2 ARDUINO"); //Prints I2C ARDUINO in LCD
delay(10000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(1); //Begins I2C communication with Slave Address as 8 at pin (A4,A5)
Wire.onReceive(receiveEvent); //Function call when Slave receives value from master
Wire.onRequest(requestEvent); //Function call when Master request value from Slave
}
void loop(void)
{
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print(">> Slave <<"); //Prints >> Slave << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print("MasterVal:"); //Prints MasterVal: in LCD
lcd.print(SlaveReceived); //Prints SlaveReceived value in LCD received from Master
Serial.println("Slave Received From Master:"); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(5000);
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() //This Function is called when Master wants value from slave
{
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte SlaveSend = map(potvalue,0,1023,0,127); // Convert potvalue digital value (0 to 1023) to (0 to 127)
Wire.write(SlaveSend); // sends one byte converted POT value 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.