Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
//code for master:
#include<Wire.h>
int x = 0;
void setup()
{
Wire.begin(); // Start the I2C Bus as Master
}
void loop() {
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
x++; // Increment x
delay(500);
}
//code for slave:
#include<Wire.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2); //interfacing lcd with arduino
int x;
void setup()
{
Wire.begin(9); // Start the I2C Bus as Slave on address 9
Wire.onReceive(receiveEvent);
lcd.begin(16,2); //lcd wil start now
Serial.begin(9600); //serial monitor for displating the output
}
void receiveEvent(int bytes) //function for receiveing event
{
x = Wire.read(); // read one character from the I2C
}
void loop()
{
lcd.clear(); //clearing the lcd display
lcd.setCursor(0, 0); //setting cursor to first row first column
lcd.print("data is:"); //displaying the information on lcd
lcd.setCursor(10, 0); //setting cursor to first row 10th column for displaying the data
lcd.print(x);
Serial.println("data is:");
Serial.println(x); //displaying the output on serial monitor
delay(500);
}
//output screenshot is attached