Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Offline Embedded Batch - 2
//I2C Master Code Communication between two Arduino
#include
#include
LiquidCrystal lcd(10,11,5,4,3,2);
void setup()
{
lcd.begin(16,2); //initilize the LCD display
lcd.setCursor(0,0); //set cursor at first line
lcd.print("skill lync"); //print the data in LCD
lcd.setCursor(0,1); //set cursor at second line
lcd.print("I2C ARDUINO TO SLAVE");
for(int i=0;i<16;i++)
{
lcd.scrollDisplayLeft();
delay(100); //delay for 3 seconds
}
delay(1000);
lcd.clear(); //clear the data in LCD display
Serial.begin(9600); //begin serial communicaton
Wire.begin();
}
void loop()
{
Wire.requestFrom(8,1); //request 1 byte from the slave arduino(8)
byte MasterReceive = Wire.read(); //recieve a byte from the slave arduino and store in Masterreceiver
int portvalue=analogRead(A0); //reads analog value from port
byte MasterSend = map(portvalue,0,1023,0,127); //convert digital value
Wire.beginTransmission(8); //start transmit to slave arduino(8)
Wire.write(MasterSend); //sends one byte converted port value to slave
Wire.endTransmission(); //stop transmitting
lcd.setCursor(0,0); //set the cursor at first line
lcd.print("The Master"); //print the given line
lcd.setCursor(0,1); //set the cursor at second line
lcd.print("Slavevalue:");
lcd.print(MasterReceive); //print master received value in LCD received from slave
Serial.println("Master Recieved From Slave"); //prints in serial monitor
Serial.println(MasterReceive);
delay(600);
}
//Slave code
#include
#include
LiquidCrystal lcd(10,11,5,4,3,2); //declare the LCD pins
byte SlaveReceived =0;
void setup()
{
lcd.begin(16,2); //initilize the LCD display
lcd.setCursor(0,0); //set cursor at first line
lcd.print("skill lync"); //print the data in LCD
lcd.setCursor(0,1); //set cursor at second line
lcd.print("I2C 2 Arduino");
delay(3000); //delay for 3 seconds
lcd.clear(); //clear the data in LCD display
Serial.begin(9600); //begin serial communicaton
Wire.begin(8); //begin the I2C slve address
Wire.onReceive(receiveEvent); //function call when the slave receive value from master
Wire.onRequest(requestEvent); //function call when the master request value from slave
}
void loop(void)
{
lcd.setCursor(0,0); //set the cursor at first line
lcd.print("The Slave"); //print the data in LCD
lcd.setCursor(0,1); //set the cursor at second line
lcd.print("Mastervalue:");
lcd.print(SlaveReceived); //print slave received value in LCD
Serial.println("Slave Received From Master:"); //prints in serial monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent(int howmany) //the function is callled when slave recieves value from master
{
SlaveReceived = Wire.read(); //to read value received from master and store in variable slave reaceived
}
void requestEvent() //function is called when master wants value from slave
{
int portvalue=analogRead(A0); //reads analog value from port(0-5V)
byte SlaveSend = map(portvalue,0,1023,0,127); //convert port value in digital value 10 bit to 4 bit
Wire.write(SlaveSend); //slave send to write the value in master
}