Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
//Master code:-
#include <Wire.h> // Include the Wire library for I2C communication
#define slaveAdr 8 // Define the address of the slave device
void setup()
{
Wire.begin(); // Initialize the I2C communication as Master
Serial.begin(9600); // Start the serial communication with the baud rate of 9600
}
void loop()
{
Wire.beginTransmission(8); // Start I2C communication with the slave device
Wire.write("pinaki sarkar"); // Send the string "pinaki sarkar" to the slave device
Wire.endTransmission(); // End the I2C communication
delay(1000); // Wait for a second
}
//slave code:-
#include <LiquidCrystal.h>
#include <Wire.h> // Include the Wire library for I2C communication
#define slaveAdr 8 // Define the address of the slave device
LiquidCrystal lcd_1(7,13,12,11,10,8); // Initialize the LCD
void setup(){
lcd_1.begin(16,2); // Set up the LCD's number of columns and rows
lcd_1.setCursor(0,1); // Position the LCD cursor
Serial.begin(9600); // Start the serial communication with the baud rate of 9600
while(!Serial) // Wait for the serial port to connect
{
;}
Wire.begin(slaveAdr); // Initialize the I2C communication as Slave
Wire.onReceive(getData); // Attach a function to trigger when data is received
}
void loop()
{
delay(100); // Wait for 100 milliseconds
}
void getData(int noByte) // Function to be triggered when data is received
{
char buffer[16]; // Buffer to store incoming data
int i=0; // Initialize the index variable
Serial.println(noByte); // Print the number of bytes received
while(Wire.available()) // While there is data available to read
{
buffer[i] = Wire.read(); // Read the data and store it in the buffer
i++; // Increment the index variable
}
buffer[i]='\0'; // Add a null character at the end of the buffer
Serial.print(buffer); // Print the buffer
lcd_1.print(buffer); // Display the buffer on the LCD
delay(10000); // Wait for 10 seconds
lcd_1.clear(); // Clear the LCD
}