Challenge

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

// Master Code

//
#include //Library - I2C Communication functions
#include //Library - LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Defining LCD Pins

void setup()

{
lcd.begin(16,2); //Initilizing the LCD display
lcd.setCursor(0,0);
lcd.print("Hello user !"); // Printing in LCD
delay(5000);
lcd.clear(); //Clearing the LCD
Serial.begin(9600); //Initialising Serial Communication
Wire.begin(); //Initialising I2C communication
}

void loop()

{
Wire.requestFrom(8,1); // request 2 bytes from slave (10)
byte MasterReceive = Wire.read(); // receive a byte from the slave arduino and store in MasterReceive
int val = analogRead(A0); // Reads analog value from POT (0-5V)
byte MasterSend = map(val,0,1023,0,127); //Scaling down digital value.
Wire.beginTransmission(8); // starting transmit to slave arduino (10)
Wire.write(MasterSend); // sending one byte converted value to slave
Wire.endTransmission(); // stoppin transmission
lcd.setCursor(0,0);
lcd.print(" Master ");
lcd.setCursor(0,1);
lcd.print("ValofSlave:");
lcd.print(MasterReceive);
Serial.println("Master Received"); //Printing in Serial Monitor
Serial.println(MasterReceive);
delay(500);
lcd.clear();

}

//Slave Code
//
#include //Library for I2C Communication functions
#include //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Defining LCD Module

byte SlaveReceived = 0;

void setup()

{
lcd.begin(16,2); //Initilizing LCD display
lcd.setCursor(0,0);
lcd.print("Hello !!");
delay(5000);
lcd.clear();
Serial.begin(9600); //Initilizing Serial Communication
Wire.begin(8); //Initilizing I2C communication
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
}

void loop(void)
{
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print(" Slave ");
lcd.setCursor(0,1);
lcd.print("ValofMaster:");
lcd.print(SlaveReceived); //Printing the value from master in LCD
Serial.println("Slave Received"); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();

}

void receiveEvent(int num)
{
SlaveReceived = Wire.read();
}

void requestEvent()
{
int val = analogRead(A0);
byte SlaveSend = map(val,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.