Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
CHALLENGE 9 : PROJECT 1
Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Program:
Program that demonstrates the communication between a Master Arduino and a Slave Arduino using the I2C protocol to display a message on a 16x2 LCD screen.
Master Arduino Code:
#include <Wire.h>
void setup()
{
Wire.begin(); // Initialize I2C communication as Master
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop()
{
Wire.beginTransmission(9); // Start communication with Slave Arduino (address 9)
Wire.write("Hello, Slave!"); // Send message to Slave Arduino
Wire.endTransmission(); // End transmission
delay(2000); // Wait for 2 seconds before sending the next message
}
Slave Arduino Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize LCD object with I2C address 0x27, 16 columns, and 2 rows
void setup()
{
lcd.begin(16, 2); // Initialize the LCD screen
Wire.begin(9); // Initialize I2C communication as Slave with address 9
Wire.onReceive(receiveEvent); // Register the receiveEvent function as the callback for receiving data
}
void loop()
{
// Code in the loop is not necessary for this example
}
void receiveEvent(int numBytes)
{
while (Wire.available())
{
char c = Wire.read(); // Read the received character from the Master Arduino
lcd.print(c); // Print the received character on the LCD screen
}
}
Output:
The LCD screen will show:
```
Hello, Slave!
Hello, Slave!
Hello, Slave!
...
On the Serial Monitor of the Master Arduino, you will see the message "Hello, Slave!" being printed repeatedly every 2 seconds.
On the 16x2 LCD screen connected to the Slave Arduino, you will see the message "Hello, Slave!" being displayed continuously as it receives the message from the Master Arduino.
This pattern will continue as long as the program is running and the communication between the Master and Slave Arduinos is established. Each time the Master Arduino sends the message, the Slave Arduino receives it and displays it on the LCD screen.
Description of the program:
Master Arduino Code:
- The `Wire` library is included to enable I2C communication.
- In the `setup()` function, `Wire.begin()` is called to initialize I2C communication as a Master.
- `Serial.begin(9600)` initializes the serial communication for debugging purposes.
- In the `loop()` function, the program repeatedly executes the following steps:
- `Wire.beginTransmission(9)` starts the communication with the Slave Arduino by specifying its address as 9.
- `Wire.write("Hello, Slave!")` sends the message "Hello, Slave!" to the Slave Arduino.
- `Wire.endTransmission()` ends the transmission.
- `delay(2000)` introduces a 2-second delay before sending the next message.
Slave Arduino Code:
- The `Wire` library and the `LiquidCrystal_I2C` library are included. The latter is used to interface with the 16x2 LCD screen via I2C.
- An `lcd` object is created with the LCD's I2C address (0x27) and the dimensions (16 columns and 2 rows).
- In the `setup()` function, the LCD is initialized using `lcd.begin(16, 2)`, and I2C communication is initialized as a Slave with address 9 using `Wire.begin(9)`.
- The `Wire.onReceive(receiveEvent)` registers the `receiveEvent` function as the callback for receiving data.
- The `loop()` function is left empty as we do not need any code there.
- The `receiveEvent(int numBytes)` function is a callback that gets executed whenever the Slave Arduino receives data from the Master Arduino.
- It uses a `while` loop and `Wire.available()` to read the received character(s) from the Master Arduino.
- The received character is then printed on the LCD screen using `lcd.print(c)`.
Explanation:
- The Master Arduino continuously sends the message "Hello, Slave!" to the Slave Arduino every 2 seconds.
- The Slave Arduino receives the message using the `receiveEvent` callback and prints it on the LCD screen using `lcd.print()`.
- The Master Arduino displays the sent message on the Serial Monitor for debugging purposes.
The I2C communication is established between the Arduinos using the `Wire` library. The Master Arduino acts as the sender, and the Slave Arduino acts as the receiver and display controller for the LCD screen.