Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
CODE:
MASTER:
//MASTER
//SLAVE address-4
///MASTER CODE
#include<Wire.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
Wire.begin();
}
void loop()
{
Wire.requestFrom(4,1);
float MasterReceive2 = Wire.read();
MasterReceive2 = (MasterReceive2*3.928)-47.857;
lcd.setCursor(0,0);
lcd.print(">>MASTER<<");
lcd.print("S1: ");
lcd.setCursor(0,1);
lcd.print(MasterReceive2);
lcd.print(" Cel");
//lcd.
delay(500);
int temp = analogRead(A0);
byte MasterSend = map(temp,0,1023,0,127);
Wire.beginTransmission(4);
Wire.write(MasterSend);
Wire.endTransmission(4);
Serial.print("Master Sent value ");
Serial.println((MasterSend*3.928)-47.857);
}
SLAVE:
//SLAVE 2 address -4
#include <Wire.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte SlaveSend2 = 0;
byte SlaveReceived = 0;
void setup()
{
lcd.begin(16, 2);
lcd.clear();
Serial.begin(9600);
Wire.begin(4);
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
}
void loop()
{
float SlaveReceived1 = (SlaveReceived*3.928)-47.857;
lcd.setCursor(0,0);
lcd.print(">>SLAVE<< M: ");
lcd.setCursor(0,1);
lcd.print(SlaveReceived1);
lcd.print(" Cel");
delay(500);
}
void requestEvent ()
{
int potvalue = analogRead(A0);
byte SlaveSend2 = map(potvalue,0,1023,0,127);
Wire.write(SlaveSend2);
Serial.print("Value sent by Slave: ");
Serial.println((SlaveSend2*3.928)-47.857);
}
void receiveEvent(int num)
{
SlaveReceived = Wire.read();
}
OUTPUT:




