Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
AIM: Interfacing a 16*2 LCD with Arduino using I2C protocol
MATERIALS REQUIRED:
1.ARDUINO UNO R3.
2.Potentiometer(250kohms).
3.Bread board.
4.Resistors (1Kohm).
5.LCD (16*2) DISPLAY.
6.Jumping wires.
MASTER CODE:
#include//Library for LCD display functions
#include//Library for I2C Communication
LiquidCrystal lcd(2,3,4,5,6,7);//Define LCD module pins
void setup()
{
lcd.begin(16,2);//Initialize LCD display
lcd.setCursor(0,0);//Set the cursor at the beginning line of display
lcd.print("PAVAN KUMAR");//Prints circuit Digest in the LCD
lcd.setCursor(0,1);//Set the cursor to the second line of display
lcd.print("I2C 1 ARDUINO");//Prints the second digest line
delay(1000);//Delay of 1 second
lcd.clear();//Clear the LCD
Serial.begin(9600);//Intialize the serial monitor
Wire.begin();//Begins the I2C communication at A4,A5 pins
}
void loop()
{
Wire.requestFrom(8,1);//Request 1 byte from the slave arduino (8)
byte MasterReceive=Wire.read();//Receive a byte from slave arduino and store in master receive
int potvalue=analogRead(A0);//Reads analog value from pot(0-5v)
byte MasterSend=map(potvalue,0,1023,0,255);//Convert digital value to 0-255
Wire.beginTransmission(8);//Start transmission to slave arduino 8
Wire.write(MasterSend);//Sends one byte pot value to slave
Wire.endTransmission();//End of transmission
lcd.setCursor(0,0);//Set the cursor to line one of LCD
lcd.print(">>THE MASTER<<");//Prints the given In LCD
lcd.setCursor(0,1);//Set cursor to the next line
lcd.print("Slave val:");//Prints the slave value in LCD
lcd.print(MasterReceive);
Serial.println("Master received from Slave");//Prints in serial monitor
Serial.println(MasterReceive);
delay(5000);//Delay is 5 seconds
}
SLAVE CODE:
#include//Library for LCD display functions
#include//Library for I2C Communication
LiquidCrystal lcd(2,3,4,5,6,7);//Define LCD module pins
byte SlaveReceived=255;
void setup()
{
lcd.begin(16,2);//Initialize LCD display
lcd.setCursor(0,0);//Set the cursor at the beginning line of display
lcd.print("THE SLAVE");//Prints circuit Digest in the LCD
lcd.setCursor(0,1);//Set the cursor to the second line of display
lcd.print("I2C 2 ARDUINO");//Prints the second digest line
delay(1000);//Delay of 1 second
lcd.clear();//Clear the LCD screen
Serial.begin(9600);
Wire.begin(8);//Begins the I2C communication with the slave address of 8
Wire.onReceive(receiveEvent);//Function call when slave receives value from then master
Wire.onRequest(requestEvent);//Function call when master wants a value from slave
}
void loop()
{
lcd.setCursor(0,0);//Set the cursor to first line
lcd.print(">>THE SLAVE<<");//Prints the given on the LCD
lcd.setCursor(0,1);//Sets the cursor to second line
lcd.print("Master val:");//Prints the value master val in LCD
lcd.print(SlaveReceived);//Prints the slave received in LCD
Serial.println("Slave received from Master");//Prints the given in LCD
Serial.println(SlaveReceived);
delay(5000);//delay of 5 seconds
lcd.clear();//Clear the LCD
}
void receiveEvent(int howMany) //Function is called when slave receives value from master
{
SlaveReceived = Wire.read(); //Used to read value received from master
}
void requestEvent() //Function is called when Master wants value from slave
{
int potvalue = analogRead(A0); //Reads analog value from pot (0-8V)
byte SlaveSend = map(potvalue,0,1023,0,255); //Converts pot digital value
Wire.write(SlaveSend); //Sends one byte converted pot value to master
}