Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
PROJECT
Aim 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:
- Communication between Arduino and 16*2 LCD using I2C protocol.
- Displaying the message sent by Master and received by slave i.e. LCD display.
Key Highlights of the Project:
- Using the inter-integrated circuit communication protocol, transferring the data i.e. the message we want to display is sent to the LCD.
- 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:
- Communication between the Arduino’s and LCD using I2C.
- The message passed from master to slave and displayed on the 16*2 LCD.
Expected Outcomes:
- Students will get the Knowledge of I2C communication Protocol and the Arduino.
- The Hardware connection of all components used in this project.
- Displaying the message sent by master (Arduino) and received by the slave (16*2 LCD display).
Master Code
//Master
#include<Wire.h> //Library for I@C Communication
#include<LiquidCrystal.h> //Library for LCD display functional
LiquidCrystal lcd(12,11,5,4,3,2); //Define LCD Module Pins (RS, E, D4, D5, D6, and D7 )
void setup()
{
lcd.begin(16,2); //initializes the LCD display with 16 columns and 2 rows.
lcd.setCursor(0,0); //sets the cursor to the first column of the first row.
lcd.print("skill lync"); //prints the string "skill lync" on the LCD display.
lcd.setCursor(0,1); //sets the cursor to the first column of the second row.
lcd.print("I2C ARDUINO 2 ARDUINO"); //prints the string "I2C ARDUINO 2 ARDUINO" on the LCD display
for(int i=0; i<16; i++) //for loop , Repeating 16 times
{
lcd.scrollDisplayLeft(); //scrolls the entire display to the left by one character
delay(100); //every 100 milliseconds
}
delay(2000); //delay for 2 sec
lcd.clear(); // clears the LCD display.
Serial.begin(9600); //initializes serial communication at a baud rate of 9600.
Wire.begin(); //initializes the I2C communication at pin(A4,A5)
}
void loop()
{
Wire.requestFrom(8,1); //requests 1 byte of data from the slave device with the address 8
byte MasterReceive = Wire.read(); // stores the received 1 byte of data in the MasterReceive variable.
int potvalue = analogRead(A0); // reads the value of the potentiometer connected to analog pin A0 and stores it in the potvalue variable.
byte MasterSend = map(potvalue,0,1023,0,127); //maps the range of values (0 to 1023) to the range of bytes (0 to 127)
//or Convert digital value (0 to 1023) to (0 to 127) and stored in the MasterSend variable
Wire.beginTransmission(8); //begins an I2C transmission to the slave device with the address of 8,
Wire.write(MasterSend); //sends one byte converted POT value to slave
Wire.endTransmission(); //stop transmitting
lcd.setCursor(0,0); // sets the cursor position to the beginning of the first row of the LCD display
lcd.print(">> The Master << "); //prints the string ">> The Master <<" on the first row
lcd.setCursor(0,1); //sets the cursor position to the beginning of the second row
lcd.print("Slave Value: "); //prints the string Slave Value:
lcd.print(MasterReceive); //pritns MasterReceive in LCD received from Slave
Serial.print("Master received from slave :"); //prints in serial monitor
Serial.print(MasterReceive);
Serial.print("\n");
delay(500); // delay of 500 milliseconds
lcd.clear(); // clears the LCD display
}
Slave Code
//slave
#include<Wire.h> //Libraray for I2C Communication function
#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; //declares a variable SlaveReceived to hold the byte of data received from the master
void setup()
{
lcd.begin(16,2); //sets up the LCD display by initializing it with 16 columns and 2 rows
lcd.setCursor(0,0); //sets the cursor to the first column of the first row.
lcd.print("Skill lync"); //prints the string "skill lync" on the LCD display
lcd.setCursor(0,1); //sets the cursor to the first column of the second row
lcd.print("I2C ARDUINO"); //prints the string "I2C ARDUINO" on the LCD display
delay(5000); //Delay for 5 seconds
lcd.clear(); //Clear LCD display
Serial.begin(9600); //initializes serial communication at a baud rate of 9600.
Wire.begin(8); //initializes the 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 Cursor at line one of LCD
lcd.print(">> The Slave <<"); //prints >> Slave << at LCD
lcd.setCursor(0,1); //sets Cursor at line two of LCD
lcd.print("Master value: "); //Prints Master value: in LCD
lcd.print(SlaveReceived); //prints SlaveReceived value in LCD received from Master
Serial.print("Slave Received From Master : "); //prints in Serial Monitor
Serial.print(SlaveReceived);
Serial.print("\n");
delay(500);
lcd.clear(); //clears the LCD display
}
void receiveEvent(int howMany) //function which is called when the master sends data to the slave.
{
SlaveReceived = Wire.read(); //function reads the received byte from the master and stores it in the SlaveReceived variable
}
void requestEvent() // function which is called when the master requests data from the slave
{
int potvalue = analogRead(A0); //reads the analog value from the potentiometer connected to analog pin A0
byte SlaveSend = map(potvalue,0,1023,0,127); // maps the range of values (0 to 1023) to the range of bytes (0 to 127)
Wire.write(SlaveSend); //The resulting byte is then sent to the master using the Wire.write() function
}
Tinker link
https://www.tinkercad.com/things/6JqfRQTtSIy




Circuit

OUTPUT




