Challenge

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

Here’s the implementation of interfacing a 16x2 LCD with two Arduinos (Master and Slave) using the I2C communication protocol. The Master Arduino sends a message, and the Slave Arduino displays it on the LCD.

---

### Circuit Setup:

1. **Master Arduino**: Connect to the I2C bus lines (SDA and SCL).

2. **Slave Arduino**: Connect to the same I2C bus lines and to the 16x2 LCD (via I2C backpack or directly to GPIO pins).

3. **I2C Bus**:

- Connect SDA (A4 on Arduino Uno) and SCL (A5 on Arduino Uno) for both Arduinos.

- Use pull-up resistors (4.7kΩ) on SDA and SCL if not built into your module.

4. **LCD**: Use an I2C-compatible module or interface it using GPIO pins.

---

### Master Code: Sending the Message

```cpp

#include <Wire.h>

void setup() {

Wire.begin(); // Initialize I2C as Master

Serial.begin(9600);

delay(1000);

}

void loop() {

String message = "Hello from Master!";

// Send message to Slave

Wire.beginTransmission(8); // Address of the Slave

Wire.write(message.c_str()); // Send message as bytes

Wire.endTransmission();

Serial.println("Message sent to Slave: " + message);

delay(2000); // Delay for readability

}

```

---

### Slave Code: Displaying the Message on LCD

```cpp

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Initialize LCD with I2C address 0x27

LiquidCrystal_I2C lcd(0x27, 16, 2);

String receivedMessage = "";

void setup() {

Wire.begin(8); // Initialize I2C as Slave with address 8

Wire.onReceive(receiveEvent); // Register receive event callback

lcd.begin();

lcd.backlight(); // Turn on LCD backlight

lcd.print("Waiting for Msg");

}

void loop() {

if (receivedMessage.length() > 0) {

lcd.clear();

lcd.print(receivedMessage); // Display the received message

receivedMessage = ""; // Reset message after display

}

delay(100);

}

void receiveEvent(int bytes) {

char buffer[32]; // Temporary buffer to store the received data

int i = 0;

while (Wire.available() && i < sizeof(buffer) - 1) {

buffer[i++] = Wire.read();

}

buffer[i] = '\0'; // Null-terminate the string

receivedMessage = String(buffer); // Store received message

}

```

---

### Explanation:

#### Master:

1. **Wire.begin()**: Initializes the Arduino as an I2C Master.

2. **Wire.beginTransmission()**: Begins communication with the Slave Arduino (address `8` in this case).

3. **Wire.write()**: Sends the message to the Slave.

4. **Wire.endTransmission()**: Ends the I2C communication.

#### Slave:

1. **Wire.begin(8)**: Initializes the Arduino as an I2C Slave with address `8`.

2. **Wire.onReceive(receiveEvent)**: Sets up a function to handle data reception.

3. **LCD Initialization**: The `LiquidCrystal_I2C` library manages the LCD screen.

4. **Display Message**: The received data is stored in a string and displayed on the LCD.

---

### Dependencies:

1. Install the `LiquidCrystal_I2C` library in the Arduino IDE:

- Go to **Tools > Manage Libraries**.

- Search for `LiquidCrystal_I2C` and install.

---

### Testing:

1. Upload the **Master Code** to one Arduino and **Slave Code** to the other.

2. Open the Serial Monitor for the Master to observe the sent messages.

3. The Slave will display the received message on the LCD.

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.