Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Interfacing a 16*2 LCD with Arduino using I2C protocol
Objective:- To send a meaasge from slave to master using Inter-Integrated Circuit (I2C) communication protocol in arduino and display the message on 16*2 LCD screen.
Connections:-
The analog pins A4 and A5 in arduino are used for used for I2C communication. The digital pin 2 is connected to RS of LCD. Digital pin 3 connected to E of LCD. The Digital pins 4, 5, 6, 7 are connected to DB1, DB2, DB3, DB4 of LCD. Input supply 5v and Gnd are connected to the LCD from arduino. The potentiometer is used to turn ON and OFF the LCD. The LED pins of LCD are connected to ground and 5v pins of Arduino.
CODE :
SLAVE CODE
// Header to use LCD functions
#include<LiquidCrystal.h>
// Header to use I2C communication
#include<Wire.h>
//pins connected to LCD
LiquidCrystal lcd(2,3,4,5,6,7);
//String to get the message
char MasterReceive[16];
void setup()
{
Serial.begin(9600);
//To begin I2C communication
Wire.begin();
lcd.begin(16,2);
lcd.clear();
}
void loop()
{
//8 address 16 characters
Wire.requestFrom(8,16);
//To move the cursor to the begining
lcd.setCursor(0,0);
//To print in display
lcd.print("MASTER");
//To move the cursor to the next line
lcd.setCursor(0,1);
int i=0;
//To check the number of bytes available
while(0<Wire.available())
{
//To print character by character
MasterReceive[i]=Wire.read();
i++;
}
//To print the received message
lcd.print(MasterReceive);
//Delay for 1 second
delay(1000);
//To end the I2C transmission
Wire.end();
}
//master code
// Header to use LCD functions
#include<LiquidCrystal.h>
// Header to use I2C communication
#include<Wire.h>
//pins connected to LCD
LiquidCrystal lcd(2,3,4,5,6,7);
void setup()
{
Serial.begin(9600);
//To begin I2C communication
Wire.begin(8);
//To call the send function
Wire.onRequest(send);
//To start the LCD display
lcd.begin(16,2);
//To clear the display
lcd.clear();
}
void loop()
{
//To move the cursor to the begining
lcd.setCursor(0,0);
//To display in LCD
lcd.print("SLAVE");
//To move the cursor to the next line
lcd.setCursor(0,1);
//To display in LCD
lcd.println("Mssage sent");
}
void send()
{
//creating string
char ch[17]="Slave Message ";
//To send message to Master
Wire.write(ch);
}