Challenge

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

SLAVE CODE::


#include<Wire.h>//header file for communication
#include<LiquidCrystal.h>//lcd header file
LiquidCrystal lcd_1(12,11,5,4,3,2);//enabling pinto display lcd
byte A;
void setup()//setup function
{
  Wire.begin(1);//wire denotes the address of a arduino
  lcd_1.begin(16,2); 
  Wire.onRequest(Request);//request from master
  Wire.onReceive(Receive);//sending from slave
  Serial.begin(9600);//begin the serial monitor
}
  
  
void loop()//infinite loop 
{
   Serial.println("VALUE GOT FROM MASTER::");
   Serial.println(A);// print in serial monitor
   lcd_1.setCursor(0,0);//setting the cursor in lcd
   lcd_1.print("VALUE FROM MASTER");
   lcd_1.setCursor(0,1);
   lcd_1.print(A);//printing in lcd
   delay(1000);
   lcd_1.clear();//lcd screen clear
}
  
 void Request()
  {
  float c=analogRead(A0);//getting the value from A0 pin
  byte z=map(c,0,1024,0,127);//mapping the value
  Serial.println("VALUE TRANSMITTED");
  Serial.println(z);
  Wire.write(z);//writing from the slave
  
  }
void Receive(int y)
  {
      A=Wire.read();//reading from the master
  }


MASTER CODE ::


#include<Wire.h>//header file for communication
#include <LiquidCrystal.h>//lcd header file
LiquidCrystal lcd_1(12,11,5,4,3,2);

void setup()//setup function
{
  lcd_1.begin(16, 2);
  Wire.begin();//wire denotes the address of a arduino
  Serial.begin(9600);//begin the serial monitor
}

void loop()//infinite loop 
{
  int h=analogRead(A0);//getting the value from A0 pin
  byte y=map(h,0,1023,0,127);//mapping the value
  Wire.requestFrom(1,1);//request from slave
  byte x=Wire.read();//storing in x variable
  Serial.print("VALUE RECEIVED FROM SLAVE::");
  Serial.println(x);// print in serial monitor
  lcd_1.setCursor(0,0);//setting cursor
  lcd_1.print("VALUE FROM SLAVE");
  lcd_1.setCursor(0,1);
  lcd_1.print(x);//printing in lcd
  delay(1000);
  lcd_1.clear();//to clear the screen
  Wire.beginTransmission(1);//to begin  the transmission
  Wire.write(y);//writing to slave 
  Wire.endTransmission(); //to end the transmission
}
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.