Challenge

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

Code for Master Arduino

#include <LiquidCrystal.h> //library for LCD display
#include <Wire.h> //library for I2C protocol

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //definig the LCD module pins(Rs,En,D4,D5,D6,D7)

char d[1];
void setup()
{
Wire.begin();//begins I2C
Serial.begin(9600);
lcd.begin(16,2); //initialize LCD display
lcd.print("recieving data..."); //prints in lcd
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("Please wait...");
}
void loop()
{
Wire.requestFrom(8,12);//requests 12 bytes from slave Arduino of address 8
lcd.clear(); //clears the displyed data in lcd
lcd.setCursor(0,0);
lcd.print("Slave message");
lcd.setCursor(0,1);
int i=0;//int i is initialized to 0 to display character by character in lcd
while(0<Wire.available()) //while wire.available is valid,reads character by character from Slave
{
d[i]=Wire.read();
i++;
}

lcd.print(d); //displays message in lcd module
Serial.print(d);//prints message in serial monitor
delay(1000);
Wire.end();//ends the i2c
}

Code for Slave Arduino

#include <LiquidCrystal.h>//library for LCD display
#include <Wire.h>//library for I2C protocol

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);//definig the LCD module pins(Rs,En,D4,D5,D6,D7)

int value=0;

void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);//begins serial monitor
Wire.begin(8);//begins I2C as slave of address 8
lcd.print("Sending data..."); //displays in lcd
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("Please wait...");
delay(1000); //1 second delay
lcd.clear();//clears displayed lcd data
lcd.setCursor(0,0);
lcd.print("data sent to ");
lcd.setCursor(4,1);
lcd.print("master");
Wire.onRequest(requestEvent);//when master sends request, function requestEvent gets executed
}

void loop()
{
delay(50);

}
void requestEvent()
{
Wire.write("hello master");//writes message 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.