Challenge

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

Master 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); // defining LCD modult pins(RS,EN,D4,D5,D6,D7)

void setup()

{

lcd.begin(16,2);// Initializing LCD display

lcd.setCursor(0,0);// Sets the cursor position at first line

lcd.print("Skill Lync");// printing in the first line

lcd.setCursor(0,1);// sets the cursor position at second lin

lcd.print("Arduino to Arduino I2C");//printing in the second line

for(int i = 0; i<16; i++)

{ // for loop initialising

lcd.scrollDisplayLeft(); // scrolling the screen to right once

delay(100); // delay for animation

}

delay(2000);

lcd.clear(); // clears the LCD display

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

Wire.begin(); // Begins I2C communication at pin (A4,A5)

}

void loop()

{

Wire.requestFrom(8,1); // Request 1 byte from slave arduino(8)

byte MasterReceive = Wire.read(); /*receive a byte from the slave arduino and store in MasterReceive */

int potvalue = analogRead(A0); // reads analog value form potentiometer

byte MasterSend = map(potvalue,0,1023,0,127); //convert digital value to (0 to 1023) to (0 to 127)

Wire.beginTransmission(8); // start transmit to slave arduino

Wire.write(MasterSend); // sends one byte converted potentiometer value to slave arduino

Wire.endTransmission(); // end trasmitting

lcd.setCursor(0,0); // setting the cursor at first line of lcd

lcd.print(" THE MASTER "); // printing the given message in the first line

lcd.setCursor(0,1); // setting the cursor at second line of lcd

lcd.print(" Slave value :"); // printing the given message in the second line

lcd.print(MasterReceive); //prints the MasterReceive in lcd received from slave

Serial.println("Master Received from Slave:"); // prints in serial Monitor

Serial.println(MasterReceive);

delay(600);

}

Slave code :

#include <Wire.h> // library for I2C communication functions

#include <LiquidCrystal.h> // library for LCD display functions

LiquidCrystal lcd(2,7,8,9,10,11); // Defining LCD module pins as same as master

byte SlaveReceived = 0;

void setup()

{

lcd.begin(16,2); // initialize LCD display

lcd.setCursor(0,0); // setting the cursor at first line of display

lcd.print("Skill Lync"); // printing the message in the first line

lcd.setCursor(0,1); // setting the cursor at second line of display

lcd.print("I2C to Arduino"); // printing the message in the second line

delay(5000); // delay for 5 seconds

lcd.clear(); // clears LCD display

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

Wire.begin(8); // begins I2C communication with slave address at pin (A4,A5)

Wire.onReceive(receiveEvent); // Function call when slave receives from master

Wire.onRequest(requestEvent); // Function call when master receives from slave

}

void loop(void)

{

lcd.setCursor(0,0); // setting the cursor position at first line

lcd.print(" THE SLAVE "); // printing the given message in the first line

lcd.setCursor(0,1); // setting the cursor position at second line

lcd.print("Master value :"); // printing the given message in the second line

lcd.print(SlaveReceived); // prints SlaveReceived value in LCD 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 SlaveReceived */

}

void requestEvent () // this function is called when master receives value from slave

{

int potvalue = analogRead(A0); // reads analog value from potentiometer

byte SlaveSend = map(potvalue,0,1023,0,127); /* convert potentiometer digital value (0 to 1023) to (0 to 127) */

Wire.write(SlaveSend); // sends one byte converted potentiometer value 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.