Challenge

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

Master code:

#include<LiquidCrystal.h>
#include<Wire.h>
LiquidCrystal lcd(3,4,5,6,7,8,9,10,11,12,13);
void setup()
{
lcd.begin(16,2);
Wire.begin();
}
void loop()
{
Wire.requestFrom(1,1); // Requesting one byte of data from slave arduino which is having address number "1"
byte number = Wire.read(); // Reading the received data and storing in variable named "number"
lcd.print("received:");
lcd.print(number);
int potvalue = analogRead(A3);
byte mastersend = map(potvalue, 0, 1023, 0, 127); // Mapping the potvalue in the other range
Wire.beginTransmission(1); // Starting the transmission with slave arduino having address number "1"
Wire.write(mastersend);
Wire.endTransmission(); // Ending the transmission
delay(500);
lcd.clear(); // Clearing lcd display
}

Slave Code:

#include<LiquidCrystal.h>
#include<Wire.h>
byte val = 0;
int potvalue = 0;
byte slavesend = 0;
LiquidCrystal lcd(4,5,6,3,7,8,9,10,11,12,13);
void setup()
{
lcd.begin(16,2);
Wire.begin(1); // Setting this arduino as slave and giving a address number 1
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
}

void loop()
{
}
void receiveEvent(int howMany) // This function is called when master arduino sends data to this slave arduino
{
val = Wire.read();
lcd.clear();
lcd.print("recived:");
lcd.print(val);
}
void requestEvent() // This function is called when master arduino request data from this slave arduino
{
potvalue = analogRead(A3);
slavesend = map(potvalue, 0, 1023, 0, 127);
Wire.write(slavesend);
}

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.