Challenge

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

Solution:

Master Code://Header Files

#include

#include

LiquidCrystal lcd(13,12,5,4,3,2); //Create a LiquidCrystal object for the LCD with the specified I2C address 

void setup()

{

  lcd.begin(16, 2); //Initialize the LCD

  Wire.begin(); //Initialize the I2C communication

}

void loop()

{

  int analogValue = analogRead(A0); //Read an analog value from pin A0

  byte MasterSend = map(analogValue, 0, 1023, 0, 127); //Map the analog value to a byte range (0-127)

  Wire.beginTransmission(8);

  Wire.write(MasterSend); //Send the mapped value to the slave

  Wire.endTransmission(); //End the I2C transmission

  Wire.requestFrom(8, 1);

  if (Wire.available())

  {

    byte SlaveReceived = Wire.read();

    lcd.clear(); //Clear the LCD screen

    lcd.setCursor(0, 0); //Set the cursor position to the first row, first column

    lcd.print(">>  Slave  <<"); //Display a message indicating communication with the slave

    lcd.setCursor(0, 1); //Set the cursor position to the second row, first column

    lcd.print("MasterVal:"); //Display a label

    lcd.print(SlaveReceived); //Display the value received from the slave

  }

  delay(1000); //Delay for 1 second

}

Slave Code:

#include

void setup()

{

  Wire.begin(8); //Begins I2C communication with Slave Address as 8 at pins A4 and A5

  Wire.onRequest(requestEvent);

}

void loop()

{

}

void requestEvent()

{

  byte SlaveSend = map(analogRead(A0), 0, 1023, 0, 127);

  Wire.write(SlaveSend);

}

 Project Link:

https://www.tinkercad.com/things/07hepKLH9t6-project-01/editel?sharecode=rhHhz09oPJ3xx8gy2VXVsSXSf1mU-Px-jSy427tGDHg

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.