Challenge

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

//Master code

#include<Wire.h> //library for i2c communication functions

#include<LiquidCrystal.h> //library 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); //initialize LCD display
lcd.setCursor(0,0); // sets cursor at first line of display
lcd.print("SKILL LYNC"); //print skill lync in LCD
lcd.setCursor(0,1); // sets cursors at second line of display
lcd.print("I2c Arduino to Arduino"); //prints I2c Arduino to Arduino
for (int i=0;i<16;i++) //for loop repeating 16 times
{
lcd.scrollDisplayLeft(); //scroll whole screen tothe right once
delay(100); //delay
}
delay(2000); //delay for 5 seconds
lcd.clear(); //clears lcd display
Serial.begin(9600); //begins serial communication at 9600 baud rate
Wire.begin(); //begins I2c communication at pin (A4 ,A5)
}
void loop()
{
Wire.requestFrom(8,1); //request 1 byte fromm slave arduino(8)
byte MasterReceive = Wire.read();

//receive a byte from the slave arduino and store in masterreceived


int potvalue = analogRead(A0); //reads the value from POT (0-5V)
byte MasterSend = map(potvalue,0,1023,0,127);

//convert potvalue digital value (0 to 1023) to (0-127
Wire.beginTransmission(8); //start transmission to slave arduino(8)
Wire.write(MasterSend); //sends one byte converted pot value to slave
Wire.endTransmission(); //stop transmitting
lcd.setCursor(0,0); //sets cursors at line two of LCD
lcd.print(">>The Master<<"); //prints >>Master<< at LCD
lcd.setCursor(0,1); //sets cursor at line one of lcd
lcd.print("slave_val:"); //prints slave_val in lcd
lcd.print(MasterReceive); //prints masterReceived in lcd receivedfrom slave
Serial.println("Master received from Slave"); //prints in serial monitor
Serial.println(MasterReceive);
delay(100);
lcd.clear();
}

//slave code


#include<Wire.h> //library for i2c communication functions

#include<LiquidCrystal.h> //library 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); //initialize LCD display

lcd.setCursor(0,0); // sets cursor at first line of display
lcd.print("SKILL lync"); //print skill lync in LCD
lcd.setCursor(0,1); // sets cursors at second line of display
lcd.print("I2c 2 Arduino"); //prints I2c 2 Arduino
delay(5000); //delay for 5 seconds
lcd.clear(); //clears lcd display
Serial.begin(9600); // serial begins communication at 9600 baud rate
Wire.begin(8); //begins I2c communication slave address as 8 at pin (A4,A5)
Wire.onReceive(receiveEvent); //function call when slave receives value from master
Wire.onRequest(requestEvent); //function call when master receives value from slave
}
void loop(void)
{
lcd.setCursor(0,0); //sets cursor at line one of the lcd display
lcd.print(">>The Slave<<"); //print the slave at lcd

lcd.setCursor(0,1); //sets cursor at second line of the lcd display
lcd.print("Master_val:"); //prints master value in lcd
lcd.print(SlaveReceived); //prints the slave received value at lcd received
Serial.println("Slave received from master:"); //prints in serial monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent(int howmany) // when slave receives value from master this function called
{
SlaveReceived = Wire.read(); //used to read the value received from master and store in variable
}
void requestEvent() // this function called when master wants value from slave
{
int potvalue = analogRead(A0); //reads the value from POT (0-5V)
byte SlaveSend = map(potvalue,0,1023,0,127);//convert potvalue digital value (0 to 1023) to (0-127
Wire.write(SlaveSend); //sends one byte 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.