Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
//master
#include <Wire.h>//allows you to communicate with I2C/TWI devices or with other devices
void setup()
{pinMode(A0, INPUT);//input pin
Serial.begin(9600);
Wire.begin(1);//to send and receive data between two lines
}
void loop()
{
int x=analogRead(A0);
signed int voltage=(x*5)/1024;//AD convertor
Wire.beginTransmission(1);//begin transmission between two lines
Wire.write(voltage);//writing voltage to slave
Wire.endTransmission();//ending transmission between two lines
delay(50);
}
//slave
#include <Wire.h>//to communicate between two lines
#include <LiquidCrystal.h>//allows an arduino to control LCD display
LiquidCrystal lcd(12,11,10,9,8,7);//pins to display the data
void setup()
{ Serial.begin(9600);
Wire.begin(1);
Wire.onReceive(receiveEvent);//I've got a stream of mesasges coming into an I2C slave from its master
lcd.begin(16,2);//no of columns and rows
lcd.clear();
}
void loop()
{
delay(50);
}
void receiveEvent(int howMany)
{
signed int voltage=Wire.read();//reading voltage sent by master
lcd.setCursor(0,0);//setting the cursor
lcd.print( "WELCOME");
lcd.setCursor(0,1);//setting the cursor
lcd.print("voltage is ");
lcd.print( voltage);//printing voltage
delay(50);
}