All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
//I2C MASTER CODE #include //Library for I2C Communication functions#include //Library for I2C Communication functionsLiquidCrystal lcd(2,7,8,9,10,11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7) void setup(){lcd.begin(16,2); //Initialize LCD displaylcd.setCursor(0,0); //Sets Cursor at first line of displaylcd.print("Circuit…
Priyal Choudhary
updated on 29 Jan 2023
//I2C MASTER CODE
#include //Library for I2C Communication functions
#include //Library for I2C Communication functions
LiquidCrystal lcd(2,7,8,9,10,11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
void setup()
{
lcd.begin(16,2); //Initialize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of display
lcd.print("Circuit Digest"); //Print Circuit Digest in LCD
lcd.setCursor(0,1); ////Sets Cursor at second line of Display
lcd.print("I2C MASTER ARDUINO"); //Prints I2C ARDUINO in LCD
delay(5000); //delay for 5 sec
lcd.clear(); //Clears LCD display
Serial.begin(9600); ////Begins Serial Communication at 9600 baud rate
Wire.begin(); //Begins I2C communication at pin (A4,A5)
}
void loop()
{
Wire.requestFrom(8,1); // request 1 byte from slave arduino (8)
byte MasterRecieve = Wire.read(); //Recieved a byte from the slave arduino and store in MasterRecieves
int potvalue = analogRead(A0); //Reads analog value from POT (0-5V)
byte MasterSend = map(potvalue,0,1023,0,127); //Convert digital value (0 to 1023) to (0 to 127)
Wire.beginTransmission(8); // start transmit to slave arduino (8)
Wire.write(MasterSend); //sends one byte converted POT value to slave
Wire.endTransmission(); //Stop transmitting
lcd.setCursor(0,0); //Set Cursor at line one of LCD
lcd.print(">> Master <<"); //Prints >>Master<< at LCD
lcd.setCursor(0,1); //Sets cursor at line two of LCD
lcd.print("SlaveVal"); //Prints SlaveVal: in LCD
lcd.print("MasterRecieves"); //Prints MasterRecieve in LCD recieved from slave
Serial.println("Master Recieved From Slave"); //Prints in Serial Monitor
Serial.println("MasterRecieves");
delay(500);
lcd.clear();
}
//I2C SLAVE CODE
#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
byte SlaveReceived = 0;
void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print("Circuit Digest"); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("I2C SLAVE ARDUINO"); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(8); //Begins I2C communication with Slave Address as 8 at pin (A4,A5)
Wire.onReceive(receiveEvent); //Function call when Slave receives value from master
Wire.onRequest(requestEvent); //Function call when Master request value from Slave
}
void loop(void)
{
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print(">> Slave <<"); //Prints >> Slave << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print("MasterVal:"); //Prints MasterVal: in LCD
lcd.print(SlaveReceived); //Prints SlaveReceived value in LCD received from Master
Serial.println("Slave Received From Master:"); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent (int howMany) //This Function is called when Slave receives value from master
{
SlaveReceived = Wire.read(); //Used to read value received from master and store in variable SlaveReceived
}
void requestEvent() //This Function is called when Master wants value from slave
{
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte SlaveSend = map(potvalue,0,1023,0,127); // Convert potvalue digital value (0 to 1023) to (0 to 127)
Wire.write(SlaveSend); // sends one byte converted POT value to master
}
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
//I2C MASTER CODE #include //Library for I2C Communication functions#include //Library for I2C Communication functionsLiquidCrystal lcd(2,7,8,9,10,11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7) void setup(){lcd.begin(16,2); //Initialize LCD displaylcd.setCursor(0,0); //Sets Cursor at first line of displaylcd.print("Circuit…
29 Jan 2023 04:45 AM IST
Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
int inches = 0;int cm = 0;long readUltrasonicDistance(int triggerPin, int echoPin){pinMode(triggerPin, OUTPUT); // Clear the trigger digitalWrite(triggerPin, LOW); delayMicroseconds(2); // Sets the trigger pin to HIGH state for 10 microsecondsdigitalWrite(triggerPin, HIGH);delayMicroseconds(10);digitalWrite(triggerPin,…
27 Jan 2023 09:43 AM IST
Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Master code: #include //Library for LCD display function LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7) int val1=A4; int val2=A5; void setup() { lccd.begin(16,2); } void loop() { lcd.setCursor(0,0); //Sets Cursor at line one of LCD…
24 Jan 2023 10:40 AM IST
Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
int inches = 0;int cm = 0;long readUltrasonicDistance(int triggerPin, int echoPin){pinMode(triggerPin, OUTPUT); // Clear the trigger digitalWrite(triggerPin, LOW); delayMicroseconds(2); // Sets the trigger pin to HIGH state for 10 microsecondsdigitalWrite(triggerPin, HIGH);delayMicroseconds(10);digitalWrite(triggerPin,…
24 Jan 2023 09:47 AM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.