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 <Wire.h>
//library for LCD display
#include <LiquidCrystal.h>
//Define LCD module pins
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);
void setup()
{
//Begin I2C communication at A4,A5
Wire.begin();
//Begin Serial communication at 9600 band rate
Serial.begin(9600);
}
void loop()
{
//request 1 byte from slave arduino(2)
Wire.requestFrom(2,2);
//receive a byte from the slave arduino and store in masterReceived
byte CMReceive = Wire.read();
//read analog value from value
lcd_1.begin(16,2);
//set cursor at line one in LCD
lcd_1.setCursor(0, 0);
//print master ate LCD
lcd_1.print("DIS IN CM");
lcd_1.setCursor(6,1);
//print master received in LCD received from slave
lcd_1.print(CMReceive);
// Wait for 1000 millisecond(s)
delay(1000);
//receive a byte from the slave arduino and store in masterReceived
byte INCHReceive = Wire.read();
//read analog value from value
lcd_1.begin(16,2);
//set cursor at line one in LCD
lcd_1.setCursor(0,0);
//print in serial monitor
lcd_1.println("DIS IN INCH");
lcd_1.setCursor(6,1);
//print master received in LCD received from slave
lcd_1.print(INCHReceive);
//print the changing vlaue of temperature sensor
Serial.println(CMReceive);
//print the changing vlaue of temperature sensor
Serial.println(INCHReceive);
// Wait for 1000 millisecond(s)
delay(1000);
}
//I2C Communication between two arduino
//library for I2C communication function
#include<Wire.h>
int Master =0 ;
int Slave = 0;
int duration =0;
void setup()
{
pinMode(11,INPUT);
pinMode(12,OUTPUT);
//Begin Serial communication at 9600 band rate
Serial.begin(9600);
//Begin I2C communication at A4,A5
Wire.begin(2);
//Function call when master request value from slave
Wire.onRequest(requestEvent);
}
void loop()
{
}
//This function is called when master wants value from slave
void requestEvent ()
{
digitalWrite(12, LOW);
delayMicroseconds(2);
digitalWrite(12, HIGH);
delayMicroseconds(10);
digitalWrite(12, LOW);
duration = pulseIn(11, HIGH); // it will find the duration of ultrosonic wave
Master = 0.01723 * duration;// formula to convert the cm
Wire.write(Master);//write the value in wire
Slave = (Master / 2.54); //formula for inch conversion
Wire.write(Slave);//write the value in wire
}