Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Overview of the project:
As mentioned in the overview of this project two Arduino's are selected and interfaced with one LCD screen using I2C communication protocol. Out of these two arduino's one is selected as master and another is selected as slave. The address to slave (arduino) is given as 8,and serial commination is established using pin A4 and A5.Here potentiometer measurement is selected as message to be sent to LCD display
Result:
Communication between Arduino and 16*2 LCD using I2C protocol is established and the message sent by Master and received by slave is executed i.e. LCD display
Master Code:
//I2C master code
//I2c communication between two arduino
#include<Wire.h>//libraray for I2C communication function
void setup()
{
Serial.begin(9600);//begine serail communication at 9600 boundrate
Wire.begin();//begine serail communication at pin(A4,A5)
}
void loop() {
float potvalue1 = analogRead(A0);//reads analog value from POT (0-8v)
byte MasterSend = potvalue1;
Wire.beginTransmission(8);//start transmit to slave arduino(8)
Wire.write(MasterSend);//send one byte converted POT value to slave
Wire.endTransmission();//stop transmitting
Serial.println("master sent to slave");//prints in serial monitor
Serial.println(MasterSend);
delay(500);
}
slave code:
.//I2C slave code
//I2C communication between two arduino
#include<Wire.h>//libraray for I2C communication function
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);//define LCD module pin(RS,EN,D4,D5,D6,D7)
float SlaveReceived = 0;
void setup()
{
lcd.begin(16,2);//initilize LCD display
lcd.setCursor(0,0);//sets cursor at first line of display
lcd.print("skill lync");//prints skill lync in LCD
lcd.setCursor(0,1);//sets cursor at secound line of display
lcd.print("I2C arduino 2 arduino");//prints I2C arduino 2 arduino in LCD
delay(5000);
lcd.clear();
Serial.begin(9600);//begins serial communciation at 9600 baudrate
Wire.begin(8);//begine I2C communication with slave address as 8 at pin(A4,A5)
Wire.onReceive(receiveEvent);//function call when slave receive value from master
}
void loop()
{
float potvalue = analogRead(A0);//reads analog value form POT(0-5V)
potvalue = potvalue*4.88;
potvalue = potvalue/1000;
lcd.setCursor(0,0);//sets currsor at line one of LCD
lcd.print("The Slave:");
lcd.print(potvalue);
lcd.setCursor(14,0);
lcd.print("V");
lcd.setCursor(0,1);//sets cursor at line two of LCD
lcd.print("The Master:");//prints master value in LCD
lcd.print(SlaveReceived);//prints slave received value in LCD recieved from master
lcd.setCursor(15,1);
lcd.print("V");
Serial.println("slave received from msater");
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent(int howMany)//this function is called when slave received from master
{
SlaveReceived = Wire.read();//used to read value received from master and store in varaibale slavereceived
SlaveReceived = SlaveReceived*0.0197;
}