Challenge

Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol

//MASTER
#include//libary for I2C communication functions
#include//libary for LCD display function
LiquidCrystal lcd(2,7,8,9,10,11);//Define LCD Module pins(RS,EN,D4,D5,D6,D7)
void setup()
{
lcd.begin(16,2);//Initilize LCDdisplay
lcd.setCursor(0,0);//sets cursor at first line of display
lcd.print("JAYA");//prints jaya in LCD
lcd.setCursor(0,1);//setcursor at second line of display
lcd.print("SURIYA");//prints I2C SURIYA IN LCD
for(int i=0;i<16;i++)//loop repeat 16 times
{
lcd.scrollDisplayLeft();//scroll whole to the rigtht once
delay(100);//slight delay for animation
}
delay(2000);//delay for 5 seconds
lcd.clear();//clear LCD display
Serial.begin(9600);//begin serial communication at 9600
Wire.begin();//begin I2C communication at pin (A4,A5)
}
void loop()
{
Wire.requestFrom(8,1);//request 1 byte from slave arudino (8)
byte MasterReceive = Wire.read();//receive a byte from slave
int potvalue =analogRead(A0);//read analog value from pot(0-5v)
byte MasterSend = map(potvalue,0,1023,0,127);//convert digital value one bit to another bit
Wire.beginTransmission(8);//start transmit to slave arudino
Wire.write(MasterSend);//sends one byte convert pot
Wire.endTransmission();//stop transmitting
lcd.setCursor(0,0);//sets cursor at line one of LCD
lcd.print(">> the Master <<");//prints the >>master>>
lcd.setCursor(0,1);//sets cursor at line two LCD
lcd.print(">> SlaveVal <<");//prints slave in LCD
lcd.print(MasterReceive);//prints master receive in LCD received
Serial.println("Master Recevied From Slave");//prints in serial monitor
Serial.println(MasterReceive);
delay(500);
}

//SLAVE
#include//libary for I2C communication functions
#include//libary for LCD display function
LiquidCrystal lcd(2,7,8,9,10,11);//Define LCD Module pins(RS,EN,D4,D5,D6,D7)
byte SlaveReceived =0;
void setup()
{
lcd.begin(16,2);//Initilize LCDdisplay
lcd.setCursor(0,0);//sets cursor at first line of display
lcd.print("JAYA");//prints jaya in LCD
lcd.setCursor(0,1);//setcursor at second line of display
lcd.print("SURIYA");//prints I2C SURIYA IN LCD
delay(5000);//delay for 5 seconds
lcd.clear();//clear LCD display
Serial.begin(9600);//begin serial communication at 9600
Wire.begin(8);//begin I2C communication at pin (A4,A5)
Wire.onReceive(receiveEvent);//function calls when slave receive from master
Wire.onRequest(requestEvent);//function call when master request from slave
}
void loop()
{
lcd.setCursor(0,0);//sets cursor at line one of lcd
lcd.print(">>the Slave<<");//prints >>slave<< at lcd
lcd.setCursor(0,1);//sets cursor at line two lcd
lcd.print(">>MasterVal<<");//prints masterval in lcd
lcd.print(SlaveReceived);//prints slave received value in lcd received
Serial.println("Master Recevied From Master");//prints in serial monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent(int howMany)//this function is called when slave received value from master
{
SlaveReceived =Wire.read();// used to read value from master
}
void requestEvent() //this function is called when master wnts value from slave
{
int potvalue =analogRead(A0);//read analog value from pot(0-5v)
byte SlaveSend = map(potvalue,0,1023,0,127);//convert pot value digital value (0 to 1023)to(0 to 127)
Wire.write(SlaveSend);//sends one bytes converted pot value to master
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.