Challenge

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

Objective -

To implement the Code for “Interfacing a 16*2 LCD with two Arduino’s (one for Master and one for Slave) using Inter-Integrated Circuit (I2C) communication protocol” by sending the message to LCD using I2C communication protocol and display the message on LCD screen.

Model -

Code -

  • Master Arduino Code
// I2C Master Code

#include <LiquidCrystal.h>

#include <Wire.h>

// Define LCD module Pins(RS,EN,D4,D5,D6,D7)
LiquidCrystal lcd(2,7,8,9,10,11);

void setup()
{
  // Initialize LCD display
  lcd.begin(16,2);
  // Set Cursor at first line of display
  lcd.setCursor(0,0);
  // Prints I2C Communication in LCD 
  lcd.print("I2C Communication");
  // Set Cursor at second line of display
  lcd.setCursor(0,1);
  // Prints I2C Arduino to Arduino in LCD
  lcd.print("I2C Arduino to Arduino");
  // Delay of 2 seconds
  delay(2000);
  // For Loop
  for(int i = 0; i<16; i++)
  {
    // Scroll whole screen to right once
    lcd.scrollDisplayLeft();
    // Delay of 100 ms
    delay(100);
  }
  // delay of 2 seconds
  delay(2000);
  // Clear lcd display
  lcd.clear();
  // Begins Serial Communiacation at 9600 baud rate
  Serial.begin(9600);
  // BeginsI2CCommunication at pin (A4,A5)
  Wire.begin();
}

void loop()
{
  // Request 1 byte from slave arduino (8)
  Wire.requestFrom(8,1);
  // Receive a byte from slave arduino
  byte MasterReceive = Wire.read();
  // Reads analog value from POT (0-5V)
  int potvalue = analogRead(A0);
  // Convert digital value(0 to 1023) to (0 to 127)
  byte MasterSend = map(potvalue, 0 ,1023, 0,127);
  // start transmit to slave arduino(8)
  Wire.beginTransmission(8);
  // sends one byte converted pot value to slave
  Wire.write(MasterSend);
  // stop transmitting
  Wire.endTransmission();
  // Set Cursor at line one of LCD
  lcd.setCursor(0,0);
  // Prints Master at LCD
  lcd.print("Master ");
  // Set Cursor at line two of LCD
  lcd.setCursor(0,1);
  // Prints Slaveval: in LCD
  lcd.print("SlaveVal:");
  // Prints MasterReceive in LCD received from Slave
  lcd.print(MasterReceive);
  // Prints in Serial Monitor
  Serial.println("Master Received from Slave");
  Serial.println(MasterReceive);
  delay(500);
  lcd.clear();
}
  • Slave Arduino Code
// I2C Slave Code
#include <Wire.h>

#include <LiquidCrystal.h>

// Define LCD module Pins(RS,EN,D4,D5,D6,D7)
LiquidCrystal lcd(2,7,8,9,10,11);

byte SlaveReceived = 0;

void setup()
{
  // Initialize LCD Display
  lcd.begin(16,2);
  // Sets Cursor at first line of Display
  lcd.setCursor(0,0);
  // Prints I2C Communication in LCD
  lcd.print("I2C Communication");
  // Sets Cursor at second line of display
  lcd.setCursor(0,1);
  // Prints I2C Arduino to Arduino in LCD
  lcd.print("I2C Arduino to Arduino");
  // Delay of 2 seconds
  delay(2000);
  // For Loop
  for(int i = 0; i<16; i++)
  {
    // Scroll whole screen to right once
    lcd.scrollDisplayLeft();
    // Delay of 100 ms
    delay(100);
  }
  // Delay of 2 seconds
  delay(2000);
  // clears LCD display
  lcd.clear();
  // Begins Serial Communication at 9600 baud rate
  Serial.begin(9600);
  // Begins I2C Communication with Slave Address as 8 at pin  (A4,A5)
  Wire.begin(8);
  // Function Call when Slave receives value from Master
  Wire.onReceive(receiveEvent);
  // Function call when MAster request value from Slave
  Wire.onRequest(requestEvent);
}

void loop(void)
{
  // sets Cursor at line one of LCD
  lcd.setCursor(0,0);
  // Prints Slave at LCD
  lcd.print("Slave ");
  // Set cursor at line two of LCD
  lcd.setCursor(0,1);
  // Prints MasterVal: in LCD
  lcd.print("MasterVal:");
  // Prints Slave Received value in LCD received from Master
  lcd.print(SlaveReceived);
  // Prints in Serial Monitor
  Serial.println("Slave Received from Master");
  Serial.println(SlaveReceived);
  delay(500);
  lcd.clear();
}

// This Function is called when slave recieves value from Master
void receiveEvent (int howMany)
{
  // used to reasd value received from master and store in variable SlaveReceived
  SlaveReceived = Wire.read();
}

// This function is called when master and store in variable Slavereceived
void requestEvent()
{
  // Reads analog value from POT (0-5V)
  int potvalue = analogRead(A0);
  // Convert potvalue digital value (0 to 1023) to (0 to 127)
  byte SlaveSend = map(potvalue, 0,1023,0,127);
  // Sends one byte converted POT value to master
  Wire.write(SlaveSend);
}

Output -

TinkerCAD Link -> https://www.tinkercad.com/things/6KJ01lMhV2z-master-slave-i2c-communication/editel?sharecode=gcpa58qCu3NJKSTVQUP3aSs8wGqSda3jsQL5ZghNx7s

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.