Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
we will use I2C communication between two arduino boards and send (0 to 127) values to each other by using potentiometer. Values will be displayed on the LCD16*2 connected to each of the Arduino. Here one Arduino will act as Master and another one will act as Slave.
what is SCL and SDA
Serial Clock (SCL): Shares the clock signal generated by the master with the slave
Serial Data (SDA): Sends the data to and from between the Master and slave.
- Wire.begin(address):
Use: This library is used for making communication with I2C devices. This Initiate the Wire library and join the I2C bus as a master or slave.
Address: The 7-bit slave address is optional and if the address is not specified, it joins the bus as a master like this [Wire.begin()].
- Wire.read():
Use: This function is used to read a byte that was received from master or slave device, either that was transmitted from a slave device to a master device after a call to requestFrom() or was transmitted from a master to a slave.
- Wire.write():
Use: This function is used to write data to a slave or master device.
Slave to Master: Slave writes data to a master when Wire.RequestFrom() is used in master.
Master to Slave: For transmission from a master to slave device Wire.write() is used in-between calls to Wire.beginTransmission() and Wire.endTransmission().
Wire.write() can be written as:
- Wire.write(value)
value: a value to send as a single byte.
- Wire.write(string) :
string: a string to send as a series of bytes.
- Wire.write(data, length):
data: an array of data to send as bytes
length: the number of bytes to transmit.
- Wire.beginTransmission(address):
Use: This function is used to begin a transmission to the I2C device with the given slave address. Subsequently, build queue of bytes for transmission with the write() function and then transmit them by calling endTransmission() function. 7-bit address of the device is transmitted.
- Wire.endTransmission();
Use: This function is used to end a transmission to a slave device that was begun by beginTransmission() and transmits the bytes that were queued by Wire.write().
- Wire.onRequest();
Use: This function gets called when a master requests data using Wire.requestFrom() from the slave device. Here we can include Wire.write() function to send data to the master.
- Wire.onReceive();
Use: This function gets called when a slave device receives a data from a master. Here we can include Wire.read(); function to read the data sent from master.
- Wire.requestFrom(address,quantity);
Use: This function is used in the master to request bytes from a slave device. The function Wire.read() is used to read the data sent from the slave device.
address: the 7-bit address of the device to request bytes from
Master program code;
//I2C MASTER CODE
//I2C Communication between Two Arduino
//Circuit Digest
//Pramoth.T
#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print("SPEED"); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("Mas***"); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(); //Begins I2C communication at pin (A4,A5)
}
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.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print(">> Master <<"); //Prints >> Master << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print("SlaveVal:"); //Prints SlaveVal: in LCD
lcd.print(MasterReceive); //Prints MasterReceive in LCD received from Slave
Serial.println("Master Received From Slave"); //Prints in Serial Monitor
Serial.println(MasterReceive);
delay(600);
lcd.clear();
}
Slave program code
//I2C SLAVE CODE
//I2C Communication between Two Arduino
//CircuitDigest
//Pramoth.T
#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
byte SlaveReceived = 0;
void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print("SPEED"); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print("I2C 2 ARDUINO"); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd.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(receiveEvent); //Function call when Slave receives value from master
Wire.onRequest(requestEvent); //Function call when Master request value from Slave
}
void loop(void)
{
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print(">> Slave <<"); //Prints >> Slave << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print("MasterVal:"); //Prints MasterVal: in LCD
lcd.print(SlaveReceived); //Prints SlaveReceived value in LCD received from Master
Serial.println("Slave Received From Master:"); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(600);
lcd.clear();
}
void receiveEvent (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 requestEvent() //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
}