Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
//master arduino
//I2C Communication between two arduino
//library for I2C communication function
#include
#include
//Define LCD module pins
LiquidCrystal lcd1(2,7,8,9,10,11);
byte MasterSend;
void setup()
{
Wire.begin();//Begin I2C communication at A4,A5
Serial.begin(9600);//Begin Serial communication at 9600 band rate
}
void loop()//using loop function
{
//
Wire.requestFrom(11,1);//request 1 byte from slave arduino(2)
byte MasterReceive = Wire.read();//receive a byte from the slave arduino and store in masterReceived
float value = analogRead(A0); //read analog value from value
byte MasterSend = map(value,0,1023,0,127);//convert digital value (0 -1023) to (0 -127)
Wire.beginTransmission(11); //start transmit to slave arduino
Wire.write(MasterSend);//send one byte converted value to slave
Wire.endTransmission();
lcd1.begin(16,2);//intialize LCD display size 16x2
lcd1.setCursor(0,0);
lcd1.print("
lcd1.setCursor(0,1);
lcd1.print("slave value :");
lcd1.print(MasterReceive);
//print in serial monitor
Serial.print("\n master from slave");
Serial.println(MasterReceive);
delay(1000);
}
//I2C SLAVE CODE
//I2C Communication between Two Arduino
//CircuitDigest
//Pramoth.T
#include
#include
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
byte SlaveReceived = 0;
void setup()
{
lcd.begin(16,2); //Initilize LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(8); //Begins 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 Currsor at line one of LCD
lcd.print(">> Slave <<"); //Prints >> Slave << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print("MasterVal:"); //Prints MasterVal: in LCD
lcd.print(SlaveReceived); //Prints SlaveReceived value in LCD received from Master
Serial.println("Slave Received From Master:"); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent (int howMany) //This Function is called when Slave receives value from master
{
SlaveReceived = Wire.read(); //Used to read value received from master and store in variable SlaveReceived
}
void requestEvent() //This Function is called when Master wants value from slave
{
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte SlaveSend = map(potvalue,0,1023,0,127); // Convert potvalue digital value (0 to 1023) to (0 to 127)
Wire.write(SlaveSend); // sends one byte converted POT value to master
}