Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
PROJECT 1 -
MASTER CODE
#include <Wire.h>// this header file is used for using I2C protocol
#define SLAVE_ADDRESS 4 //it is macro and We can Change this to the Slave Arduino's address
void setup() //this function is only called once when the arduino is getting started.
{
Wire.begin(); // this initialize the all wire library i.e. in I2C and this function always be called first before using any wire library.
Serial.begin(9600); // assigning the serial communication with baudrate of 9600
}
void loop() //in arduino coding whatever is written inside this function will always going run again and again.
{
Wire.beginTransmission(SLAVE_ADDRESS); //this function willl going to start, send the data to the slave address
Wire.write("Hello, slave\t"); //in write function we can write any Message to be sent
Wire.endTransmission(); // this function is used for ending the trannsmission or when all the data has been transfered this function will terminate the transmission
delay(1000); // here giving 1000ms delay
}
SLAVE CODE
#include <Wire.h>
#include <LiquidCrystal.h> //this header file is used for using the lcd display 16*2 , hd44780
//here i am assigning the macros for all the lcd pins with the connnected pin no.
#define LCD_RS 12
#define LCD_EN 11
#define LCD_D4 5
#define LCD_D5 4
#define LCD_D6 3
#define LCD_D7 2
#define LCD_COLUMNS 16 // these macros are going to used for telling the program lcd description
#define LCD_ROWS 2
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); // this creates and instance for LiquidCrystal class named as lcd and initialized it with specific pin no.
void setup() {
Wire.begin(4); // this is Slave address This means the Arduino will respond to I2C communication requests sent to address 4.
Wire.onReceive(receiveEvent); // Register a function to be called when a peripheral receives a transmission Register a function to be called when a peripheral receives a transmission
Serial.begin(9600);
lcd.begin(LCD_COLUMNS, LCD_ROWS); //here initialize the lcd total no. of rows and columns
}
void loop() {
lcd.setCursor(0,0); // by setting the cursor at 0 row and 0 column position , our data will going to print from that position on the lcd
delay(100);
}
void receiveEvent(int howMany) // when arduino receives the data via I2C then this function will going to execute and inside the statement of this function we have to assign the int variable for getting the output to the lcd screen because that receiveEvent is getting the data from master and here we are going to see howmay bytes it received in the integer form then it will going to print on lcd screen.
{
while (Wire.available()) //this whille loop will going to run continues until there's any data available in I2C buffer
{
char c = Wire.read(); // this function will going to read all the character type data and stores it in variable c.
Serial.print(c); // this function is used for printing the data on the serial monitor screen
lcd.print(c); // this function is used for printing the data on the lcd screen.
}
}