Challenge

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

Project - 1 :

In this project implementing 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.

Goals :

1: Communication between Arduino and 16*2 LCD using I2C protocol.

2: Displaying the message sent by Master and received by slave i.e. LCD display.

Key Highlights of the Project : 

1: Using the inter-integrated circuit communication protocol, transferring the data i.e. the message we want to display is sent to the LCD.

2: Two Arduino’s connected to the LCD display and the communication is executed using I2C protocol between Arduino and the LDC display.

Deliverables of this project : 

1: Communication between the Arduino’s and LCD using I2C.

2:The message passed from master to slave and displayed on the 16*2 LCD.

Ans :

To interface a 16x2 LCD with two Arduino boards (one as the Master and one as the Slave) using the I2C communication protocol, we need to use an I2C LCD module (also known as an I2C backpack) that simplifies the connection as,

Components Required :

1: 2 Arduino Uno 

2: 2 16X2 LCD display module

3: 2 Potentiometer 220 ohm

4: 1 Potentiometer 250 kohm

5: 1 Potentiometer 330 kohm

6: 2 Resistor 220 ohm

7: 2 Breadboard

8: Connecting Wires

Wiring Connections :

(1): Master Arduino :

  1. Connect the 5V pin of the Master Arduino to the red rail on one of the breadboards.
  2. Connect the Ground (GND) pin of the Master Arduino to the blue rail on the same breadboard.
  3. Connect the SDA (Serial Data) pin of the Master Arduino to A4.
  4. Connect the SCL (Serial Clock) pin of the Master Arduino to A5.
  5. Connect one end of a 220 ohm resistor to 5V on the breadboard.
  6. Connect the other end of the 220 ohm resistor to the anode (longer lead) of one of the 16x2 LCD modules.
  7. Connect the cathode (shorter lead) of the LCD module to the blue rail on the breadboard.
  8. Connect the potentiometer (220 ohm) to the LCD module's contrast control pin (usually labeled VO).
  9. Connect one end of the potentiometer to 5V on the breadboard.
  10. Connect the other end of the potentiometer to GND on the breadboard.
  11. Connect the wiper (middle pin) of the potentiometer to the VO pin on the LCD module.
  12. Connect the red rail on the breadboard to VCC on the LCD module.
  13. Connect the blue rail on the breadboard to GND on the LCD module.

(2): Slave Arduino :

  1. Connect the 5V pin of the Slave Arduino to the red rail on the other breadboard.
  2. Connect the Ground (GND) pin of the Slave Arduino to the blue rail on the same breadboard.
  3. Connect the SDA (Serial Data) pin of the Slave Arduino to A4.
  4. Connect the SCL (Serial Clock) pin of the Slave Arduino to A5.
  5. Connect one end of a 220 ohm resistor to 5V on the breadboard.
  6. Connect the other end of the 220 ohm resistor to the anode (longer lead) of the second 16x2 LCD module.
  7. Connect the cathode (shorter lead) of the second LCD module to the blue rail on the breadboard.
  8. Connect the potentiometer (220 ohm) to the second LCD module's contrast control pin (usually labeled VO).
  9. Connect one end of the potentiometer to 5V on the breadboard.
  10. Connect the other end of the potentiometer to GND on the breadboard.
  11. Connect the wiper (middle pin) of the potentiometer to the VO pin on the second LCD module.
  12. Connect the red rail on the breadboard to VCC on the second LCD module.
  13. Connect the blue rail on the breadboard to GND on the second LCD module.

(3): LCD Modules :

Both LCD modules should have their SDA and SCL pins connected to the corresponding pins on their respective Arduino boards.

(4): Potentiometers :

Adjust the potentiometers connected to the LCD modules to control the contrast of the displays.

(5): Connecting Wires :

  1. Connect the red rail on the first breadboard to the red rail on the second breadboard (5V rail).
  2. Connect the blue rail on the first breadboard to the blue rail on the second breadboard (GND rail).

Testing :

1: Power Test :

  • Power on both your Master and Slave Arduinos. We should see the onboard LED (usually labeled "L") on each Arduino board light up, indicating that they are powered.
  • Check if the LCD backlight on both 16x2 LCD modules is on. The backlight should be illuminated.

2: I2C Communication Test :

  • Load the appropriate code onto the Master and Slave Arduino boards as per the previous instructions.
  • Open the Serial Monitor for both Arduino IDE instances (one for Master and one for Slave) by clicking on the magnifying glass icon in the top right corner of the Arduino IDE window.
  • Ensure that the Serial Monitor for the Master Arduino is set to a baud rate of 9600 (or the rate specified in your code).
  • Send a message from the Master Arduino to the Slave Arduino by entering a value in the Serial Monitor for the Master Arduino and pressing "Send." We should see this message displayed on the LCD of the Slave Arduino and also printed in the Slave Arduino's Serial Monitor.

3: Contrast Adjustment Test :

Adjust the potentiometers connected to the LCD modules to verify that they control the contrast of the displays. Turning the potentiometers should make the characters on the LCDs either more visible or less visible, allowing us to find the optimal contrast level.

4: Data Display Test :

If our code is designed to display sensor data or messages on the LCDs, verify that the information is being displayed correctly. For example, if we have a temperature sensor connected to one of the Arduinos, ensure that the temperature reading is displayed on the LCD.

5: Backlight Control Test (Optional) :

If we've connected the LCD backlight control pins to digital pins on the Arduinos (to control the backlight programmatically), we can test turning the backlight on and off in the code to verify that it functions as expected.

Explanation of the code used :

(1): Master Arduino Programming :

This code is for the Master Arduino. It initializes an LCD display, sets up I2C communication, and continuously sends and receives data from the slave Arduino.

1: Setup Function :

  1. Initializes the LCD display to show "Circuit Digest" and "I2C 2 ARDUINO."
  2. Initializes serial communication for debugging.
  3. Begins I2C communication.

2: Loop Function :

  1. Requests 1 byte of data from the slave Arduino (address 8) via I2C.
  2. Reads an analog value from a potentiometer connected to A0.
  3. Maps the analog value to a range from 1 to 255 and sends it to the slave Arduino (address 8).
  4. Updates the LCD display with ">> Master <<" and the received data.
  5. Prints received data from the slave Arduino in the serial monitor.
  6. Clears the LCD display and waits for 500 milliseconds before the next iteration.

(2): Slave Arduino Programming :

This code is for the Slave Arduino. It initializes an LCD display, sets up I2C communication, and continuously receives and sends data to the master Arduino.

1: Setup Function :

  1. Initializes the LCD display to show "Circuit Digest" and "I2C 2 ARDUINO."
  2. Initializes serial communication for debugging.
  3. Begins I2C communication with a slave address of 8.
  4. Registers functions for receiving and responding to I2C requests.

2: Loop Function :

  1. Updates the LCD display with ">> Slave <<" and the received data from the master.
  2. Prints received data from the master Arduino in the serial monitor.
  3. Clears the LCD display and waits for 500 milliseconds before the next iteration.

3: receiveEvent Function :

  1. Called when the Slave receives data from the master.
  2. Reads the received data and stores it in the variable SlaveReceived.

4: requestEvent Function :

  1. Called when the Master requests data from the slave.
  2. Reads an analog value from a potentiometer connected to A0.
  3. Maps the analog value to a range from 1 to 255 and sends it to the master Arduino.

Code :

(1): Master Arduino Programming :

#include                                         /* Library for I2C Communication functions */

#include                                    /* Library for LCD display function */

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

 void setup()                                                    /* This allows us to communicate with Arduino board via the Serial Monitor in the Arduino IDE and this function is typically used for one-time initialization tasks */

{ 

  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 2 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();                                                       /* 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 from POT (0-5V) */

    byte MasterSend = map(potvalue,0,1023,1,255);              /* Convert digital value (0 to 1023) to (1 to 255) */

    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);                                                            /* Sets Currsor at line one of LCD */

    lcd.print(">>  Master  <<");                                                /* Prints >> Master << at>

    lcd.setCursor(0,1);                                                           /* Sets Cursor at line two of LCD */

    lcd.print("SlaveVal:");                                                            /* Prints SlaveVal: in LCD */

    lcd.print(MasterReceive);                          /* Prints MasterReceive in LCD received from Slave */

    Serial.println("Master Received From Slave");                             /* Prints in Serial Monitor */

    Serial.println(MasterReceive);

    delay(500);                                     

    lcd.clear();                                                                                     /* Clears LCD display */

 }     

(2): Slave Arduino Programming :

#include                                             /* Library for I2C Communication functions */

#include                                              /* 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 = 1;                  

void setup()                                                           /* This allows us to communicate with Arduino board via the Serial Monitor in the Arduino IDE and this function is typically used for one-time initialization tasks */

{

  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 2 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.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();                                                                                         /* Clears LCD display */

}

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,1,255);                       /* Convert potvalue digital value (0 to 1023) to (1 to 255) */

  Wire.write(SlaveSend);                                 /* sends one byte converted POT value to master */

}

Uses of the application :

1: Educational Projects :

This setup is commonly used in educational environments to teach concepts related to microcontroller programming, I2C communication, and interfacing with external devices like LCDs. It provides hands-on experience for students learning about Arduino and electronics.

2: Remote Monitoring and Display :

In situations where we want to display data or information from a remote sensor or device, we can use the Slave Arduino to collect data and display it on the LCD. The Master Arduino can be located remotely and send commands or data to the Slave, which then displays it on the LCD.

3: Sensor Data Display :

We can connect various sensors (temperature, humidity, light, etc.) to the Slave Arduino and use the LCD to display real-time sensor data. This is useful in applications like weather stations or environmental monitoring.

4: Home Automation :

In a home automation project, we can use this setup to display status information or control options for various smart home devices. For example, we could display the temperature, control lights, or show the status of security cameras.

5: Data Logging and Display :

The Slave Arduino can be used to collect and process data from multiple sources. The data can then be displayed on the LCD for monitoring or saved to an SD card for later analysis.

6: Information Kiosk :

This setup can be used to create information kiosks or displays in public places. The LCDs can provide information, announcements, or directions to visitors.

7: Industrial Automation :

In industrial settings, we can use this configuration to display critical information or control parameters of machines or processes. For example, it can be used to show the status of manufacturing equipment or process variables.

8: IoT Applications :

In Internet of Things (IoT) projects, we can integrate this setup into a larger network of devices. The Master Arduino can act as a gateway, collecting data from multiple sensors or devices and displaying summarized information on the LCD.

9: Prototyping and Development :

Engineers and developers can use this setup for prototyping and testing various ideas before implementing them in more complex systems.

10: Custom User Interfaces :

We can create custom user interfaces for devices or systems, allowing to interact with and control them via the LCD displays.

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.