Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Interfacing a 16*2 LCD with Arduino using I2C protocol :
I2C PROTOCOL:
The term I2C stands for “Inter Integrated Circuits”. It is normally denoted as IIC or I squared C or even as 2-wire interface protocol (TWI) at some places but it all means the same. I2C is a synchronous communication protocol, means both the devices that are sharing the information must share a common clock signal. It has only two wires, SDA and SCL to share information, out of which SCL is used for the clock signal and SDA is used for sending and receiving data.
Material Required :
* ESP32
*16*2 LCD display
* I2C LCD controller
* Wires
* Breadboard
CODE
Master code:
#include
#include
LiquidCrystal lcd (2, 7, 8, 9, 10, 11);
void setup()
{
lcd.begin(16,2);
lcd.setCursor (0,0);
lcd.print("Welcome");
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();
}
void loop()
{
Wire.requestFrom(8,1);
byte MasterReceive = Wire.read();
int potvalue = analogRead(A0);
byte MasterSend = map(potvalue, 0, 1023, 0, 127);
Wire.beginTransmission (8);
Wire.write (MasterSend);
Wire.endTransmission();
lcd.clear();
lcd.setCursor(0,0);
lcd.print (">> the Master <<<");
lcd.setCursor (0,1);
lcd.print ("SlaveVal:");
lcd.print (MasterReceive);
Serial.println("Master Received From Slave");
Serial.println(MasterReceive);
delay(500);
}
Slave Code:
#include
#include
LiquidCrystal lcd (2, 7, 8, 9, 10, 11);
void setup()
{
lcd.begin(16,2);
lcd.setCursor (0,0);
lcd.print("Welcome");
lcd.setCursor(0,1);
lcd.print ("I2C 2 ARDUINO");
delay(5000);
lcd.clear();
Serial.begin(9600);
Wire.begin(8);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}
void loop(void)
{
lcd.setCursor(0,0);
lcd.print (">> the Slave <<<");
lcd.setCursor (0,1);
lcd.print ("MasterVal:");
Serial.println("Slave Received From Master");
delay(500);
lcd.clear();
}
void receiveEvent (int howMany)
{
int SlaveReceived=Wire.read();
lcd.print (SlaveReceived);
Serial.println(SlaveReceived);
}
void requestEvent()
{
int potvalue = analogRead(A0);
byte SlaveSend = map(potvalue, 0, 1023, 0, 127);
//Wire.beginTransmission (8);
Wire.write (SlaveSend);
}