Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
master side programming
/*
LiquidCrystal Library
D time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
here we sending of data from masters and slave both side is happening
master displays the message of salve analog value dipending upon potentiometer
Slave displays the message of Master value depending upon potentiometer*/
#include
#include
/*LiquidCrystal name(RS,E,D4,D5,D6,D7)*/
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);
void setup()
{
lcd_1.begin(16, 4); // Set up the number of columns and rows on the LCD.
lcd_1.setCursor(0,0); // Print a message to the LCD.
lcd_1.print(" Arduino I2C ");
delay(500);
lcd_1.setCursor(3,1);
lcd_1.print("master");
delay(2000);
lcd_1.clear();
Wire.begin();
}
void loop()
{
Wire.requestFrom(8,1); // request 1 byte from slave arduino (8)
byte MasterReceive = Wire.read(); // receive a byte from the slave arduino and store in MasterReceive
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte MasterSend = map(potvalue,0,1023,0,127); //Convert digital value (0 to 1023) to (0 to 127)
Wire.beginTransmission(8); // start transmit to slave arduino (8)
Wire.write(MasterSend);
;
// sends one byte converted POT value to slave
Wire.endTransmission(); // stop transmitting
lcd_1.setCursor(0,0); //Sets Currsor at line one of LCD
lcd_1.print(">> Master <<"); //Prints >> Master << at LCD
lcd_1.setCursor(0,1); //Sets Cursor at line two of LCD
lcd_1.print("SlaveVal:"); //Prints SlaveVal: in LCD
lcd_1.print(MasterReceive); //Prints MasterReceive in LCD received from Slave
Serial.println("Master Received From Slave"); //Prints in Serial Monitor
Serial.println(MasterReceive);
delay(500);
lcd_1.clear();
}
SLAVE SIDE PROGRAMMING
#include
#include
LiquidCrystal lcd_1(12,11,5,4,3,2); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
byte SlaveReceived = 0;
void setup()
{
lcd_1.begin(16,2); //Initilize LCD display
lcd_1.setCursor(0,0); //Sets Cursor at first line of Display
lcd_1.print("ARDUINO I2C"); //Prints CIRCUIT DIGEST in LCD
lcd_1.setCursor(4,1); //Sets Cursor at second line of Display
lcd_1.print("SLAVE"); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd_1.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(8); //Begins I2C communication with Slave Address as 8 at pin (A4,A5)
Wire.onReceive(receivevalue); //Function call when Slave receives value from master
Wire.onRequest(requestvalue); //Function call when Master request value from Slave
}
void loop(void)
{
lcd_1.setCursor(0,0); //Sets Currsor at line one of LCD
lcd_1.print(">> Slave <<"); //Prints >> Slave << at LCD
lcd_1.setCursor(0,1); //Sets Cursor at line two of LCD
lcd_1.print("MasterVal:"); //Prints MasterVal: in LCD
lcd_1.print(SlaveReceived); //Prints SlaveReceived value in LCD received from Master
Serial.println("Slave Received From Master:"); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(500);
lcd_1.clear();
}
void receivevalue (int howMany) //This Function is called when Slave receives value from master
{
SlaveReceived = Wire.read(); //Used to read value received from master and store in variable SlaveReceived
}
void requestvalue() //This Function is called when Master wants value from slave
{
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte SlaveSend = map(potvalue,0,1023,0,127); // Convert potvalue digital value (0 to 1023) to (0 to 127)
Wire.write(SlaveSend); // sends one byte converted POT value to master
}



