Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
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.
To interface a 16x2 LCD with two Arduinos, one acting as the master and the other as the slave, and using the I2C communication protocol, you can follow these steps:
**Master Arduino:**
1. Connect the I2C bus on the master Arduino. On most Arduino boards, you'll use the A4 (SDA) and A5 (SCL) pins for this purpose.
2. Install the "LiquidCrystal_I2C" library for controlling the LCD using I2C communication. You can install this library using the Arduino Library Manager.
3. Upload the following code to the master Arduino:
```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // Set the LCD address (use I2C scanner to find the address)
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Master Arduino");
lcd.setCursor(0, 1);
lcd.print("Sending data...");
Wire.begin(); // Initialize I2C communication
}
void loop() {
// Send a message to the slave Arduino
Wire.beginTransmission(9); // 9 is the I2C address of the slave Arduino
Wire.write("Hello from Master");
Wire.endTransmission();
delay(1000); // Delay for 1 second
lcd.clear();
}
```
**Slave Arduino:**
1. Connect the I2C bus on the slave Arduino in the same way as on the master Arduino.
2. Install the "LiquidCrystal_I2C" library on the slave Arduino as well.
3. Upload the following code to the slave Arduino:
```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address (use I2C scanner to find the address)
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
Wire.begin(9); // 9 is the I2C address of the slave Arduino
Wire.onReceive(receiveData); // Register the receiveData function to handle incoming data
}
void loop() {
// The loop can be left empty, as data reception is handled by the receiveData function
}
void receiveData(int byteCount) {
// This function is called when data is received from the master Arduino
String message = "";
while (Wire.available()) {
char c = Wire.read();
message += c;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Slave Arduino");
lcd.setCursor(0, 1);
lcd.print(message);
}
```
In this setup, the master Arduino sends a message to the slave Arduino using the I2C communication protocol. The slave Arduino receives the message and displays it on the LCD. Make sure to set the correct I2C addresses (0x3F and 0x27 in this example) for your specific LCD modules and use the I2C scanner to determine the correct addresses if needed.
- Communication between Arduino and 16*2 LCD using I2C protocol.
- Displaying the message sent by Master and received by slave i.e. LCD display
You must correctly set up the communication channel via I2C between the Arduinos and the LCD display in order to establish communication between two Arduinos using the communication protocol known as I2C and show the message sent by the master Arduino on a 16x2 LCD attached to the slave Arduino. The following steps outline how to achieve this:
**Master Arduino:**
```arduino
#include <Wire.h>
void setup() {
Wire.begin(); // Initialize I2C communication as master
}
void loop() {
Wire.beginTransmission(9); // Begin transmission to slave with address 9
Wire.write("Hello from Master"); // Send the message
Wire.endTransmission(); // End transmission
delay(1000); // Delay for 1 second
}
```
**Slave Arduino:**
```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address
void setup() {
lcd.init();
lcd.backlight();
Wire.begin(9); // Initialize I2C communication as slave with address 9
Wire.onReceive(receiveEvent); // Register the receiveEvent function
}
void loop() {
// The loop can be left empty as data reception is handled by the receiveEvent function
}
void receiveEvent(int howMany) {
while (Wire.available()) {
char receivedChar = Wire.read();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Message Received:");
lcd.setCursor(0, 1);
lcd.print(receivedChar);
}
}
```
**Connections:**
For the Slave Arduino, connect the SDA and SCL pins to the corresponding SDA and SCL pins of the LCD display. Additionally, connect the VCC and GND of the LCD display to the power supply of the Arduino.
For the Master Arduino, there's no need for the LCD display as it only sends the message to the slave.
**Important Note:**
Ensure that both Arduinos have a common ground and the necessary pull-up resistors are connected for the I2C communication.
This setup will have the master Arduino sending the message "Hello from Master" to the slave Arduino via the I2C protocol. The slave Arduino, upon receiving this message, will display it on the 16x2 LCD connected to it. Adjust the I2C address (0x27 in this example) in the slave code to match your specific LCD module, and ensure the I2C communication is correctly set up for both Arduinos.
- Students will get the Knowledge of I2C communication Protocol and the Arduino.
- The Hardware connection of all components used in this project.
- Displaying the message sent by master (Arduino) and received by the slave (16*2 LCD display).
To create a project that teaches students about the I2C communication protocol, Arduino, and displays a message sent by the master Arduino on a 16x2 LCD display connected to the slave Arduino, follow these steps, including the hardware connections:
**Project Overview:**
In this project, students will learn how to use the I2C communication protocol to send data between two Arduinos. The master Arduino will send a message to the slave Arduino, which will display the message on a 16x2 LCD.
**Components Needed:**
- 2 Arduino boards (e.g., Arduino Uno)
- 16x2 LCD display with I2C backpack
- Jumper wires
- Breadboard (optional)
**Hardware Connections:**
1. **Master Arduino:**
- Connect the SDA (A4) and SCL (A5) pins on the master Arduino to the corresponding pins on the I2C backpack of the LCD display.
- There's no need to connect the LCD display to the master Arduino as it's only responsible for sending data.
2. **Slave Arduino:**
- Connect the SDA and SCL pins on the slave Arduino to the corresponding pins on the I2C backpack of the LCD display.
- Connect the VCC and GND pins on the I2C backpack to the 5V and GND pins of the slave Arduino.
- Ensure that both Arduinos share a common ground for proper communication.
**Arduino Code:**
**Master Arduino:**
```arduino
#include <Wire.h>
void setup() {
Wire.begin(); // Initialize I2C communication as master
}
void loop() {
Wire.beginTransmission(9); // Begin transmission to the slave with address 9
Wire.write("Hello from Master"); // Send the message
Wire.endTransmission(); // End transmission
delay(1000); // Delay for 1 second
}
```
**Slave Arduino:**
```arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address
void setup() {
lcd.init();
lcd.backlight();
Wire.begin(9); // Initialize I2C communication as a slave with address 9
Wire.onReceive(receiveEvent); // Register the receiveEvent function
}
void loop() {
// The loop can be left empty, as data reception is handled by the receiveEvent function
}
void receiveEvent(int howMany) {
while (Wire.available()) {
char receivedChar = Wire.read();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Message Received:");
lcd.setCursor(0, 1);
lcd.print(receivedChar);
}
}
```
**Project Demonstration:**
1. Upload the master Arduino code to the master Arduino board.
2. Upload the slave Arduino code to the slave Arduino board.
3. Power up both Arduinos.
4. The master Arduino will send the message "Hello from Master" to the slave Arduino via the I2C protocol.
5. The slave Arduino will receive and display the message on the 16x2 LCD.
This project allows students to learn about the I2C communication protocol, the use of the Arduino platform, and how to display data received from one Arduino on a peripheral device like a 16x2 LCD.