Challenge

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

COde for master 

#include//library for I2C FUNCTION

#include//library for lcd display 

LiquidCrystal lcd(2,7,8,9,10,11);//initialize lcd display

void setup()

{

  lcd.begin(16,2);

  lcd.setCursor(0,0);//set cursor to first line of display 

  lcd.print("Nitendra");

  lcd.setCursor(0,1);//set cursor to second  line of display 

  lcd.print("I2C ARDUINO TO ARDUINO");

  for(int i=0;i<16>

  {

    lcd.scrollDisplayLeft();

    delay(100);

  }

lcd.clear();

  Serial.begin(9600);//begin serial communication at 9600 baud rate

  Wire.begin();//begins i2c at A4,A5

}

void loop()

{

  Wire.requestFrom(8,1);//request one byte from slave

  byte MasterReceive=Wire.read();

  int value=analogRead(A0);//read analog value

  byte Mastersend=map( value,0,1023,0,127);

  Wire.beginTransmission(8);

  Wire.write(Mastersend);

  Wire.endTransmission();

  lcd.setCursor(0,0);//set cursor to first line of display 

  lcd.print(">>The king<<");

  lcd.setCursor(0,1);//set cursor to second  line of display 

  lcd.print("INDIA");

  lcd.print(MasterReceive);

  Serial.println("Master recieved from slave");

  Serial.println(MasterReceive);

  delay(500);

}

Code for slave

#include//library for I2C FUNCTION

#include//library for lcd display 

LiquidCrystal lcd(2,7,8,9,10,11);//define lcd module pins

byte SlaveReceived =0;

void setup()

{

  lcd.begin(16,2);//initializelcd display

  lcd.setCursor(0,0);//set cursor to first line of display 

  lcd.print("Nitendra");

  lcd.setCursor(0,1);//set cursor to second line of display 

  lcd.print("I2C ARDUINO TO ARDUINO");

    delay(5000);

  lcd.clear();

  Serial.begin(9600);

  Wire.begin(8);

  Wire.onReceive(receiveEvent);//function call for ISR

  Wire.onRequest(requestEvent);//function call for ISR

  }

void loop()

{

  lcd.setCursor(0,0);//set cursor to first line of display 

  lcd.print(">>The queen<<");

  lcd.setCursor(0,1);//set cursor to second line of display 

  lcd.print("BHARAT");

  lcd.print(SlaveReceived);

  Serial.println("Slave recieved from master");

  Serial.println(SlaveReceived);//prints in serial monitor

  delay(500);

  lcd.clear();

}

void receiveEvent(int howmany)

{

  SlaveReceived=Wire.read();//read value received from master

}

void requestEvent()//master wants value from slave

{

  int potvalue=analogRead(A0);//read analog value

  byte Slavesend=map(potvalue,0,1023,0,127);//reads analog value from pot

  Wire.write(Slavesend);// one byte converted pot 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.