Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Interfacing a 16*2 LCD with Arduino using I2C protocol
COde:
Master Code:
#include<Wire.h>//getting the wire I2C communication protocol
#include<LiquidCrystal.h>//getting the liquidcrystal library files
LiquidCrystal lcd(2,7,8,9,10,11);//(RS,EN,8,9,10,11)
void setup()
{
lcd.begin(16,2);
// lcd.setCursor(0,0);
//lcd.print("skill lync");
// lcd.setCursor(0,1);
// delay(2000);
// lcd.clear();
Serial.begin(9600);//baud rate for transmission
Wire.begin();//begin communication for i2c in a4 and a5
}
void loop()
{
Wire.requestFrom(8,1);//getting the slave address
int potvalue = analogRead(A0);//getting the analog value from tempearture sensor
byte Mastersend = map(potvalue,0,1023,0,127);//converting to digial value
Wire.beginTransmission(8);
Wire.write(Mastersend);//sending the value to slave
Wire.endTransmission();
lcd.setCursor(0,0);
lcd.print("Master value");
lcd.setCursor(14,2);
lcd.print(Mastersend);//printing the value in LCD
Serial.println("Master value");
Serial.println(Mastersend);
}
Slave Code:
#include<Wire.h>//getting the wire I2C communication protocol
#include<LiquidCrystal.h>//getting the liquidcrystal library files
LiquidCrystal lcd(2,7,8,9,10,11);(RS,EN,8,9,10,11)
byte slaveValue =0;
void setup()
{
lcd.begin(16,2);//getting the LCD begin
Serial.begin(9600);
Wire.begin(8);//starting the I2C Transmissiom
Wire.onReceive(receiveEvent);//when ever master send the request
}
void loop()
{
//lcd.setCursor(0,1);
// lcd.print("Slave Received");
}
void receiveEvent(int howMany)
{
slaveValue = Wire.read();//getting the value from master
lcd.setCursor(0,0);
lcd.print("Slave Received");
lcd.setCursor(0,1);
lcd.print(slaveValue);
Serial.println("slave");
Serial.println(slaveValue);
delay(1000);
}
Logic:
It is also the same as the week12 challenge. In the master, we are inserting the communication protocol library are inserted and Wire. begin is also given to start the communication. we are getting the data from the analog pin and the data converted to the digital value. And it is transmitted to the slaves use the command wire.transmission.
Output:
