Challenge

Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol

I2C is a synchronous communication protocol meaning, both the devices that are sharing the information must share a common clock signal. It has only two wires to share information out of which one is used for the cock signal and the other is used for sending and receiving data.

As said earlier it has two wires, these two wires will be connected across two devices. Here one device is called a master and the other device is called as slave. Communication should and will always occur between two a Master and a Slave. The advantage of I2C communication is that more than one slave can be connected to a Master.

The complete communication takes place through these two wires namely, Serial Clock (SCL) and Serial Data (SDA).Initialization of transmission begins with a falling edge of SDA, which is defined as ‘START’ condition and rising edge of SDA stops the transmission which is shown as ‘STOP’ condition The library <Wire.h> is included in the program for using the following functions for I2C communication.

program:

//I2C MASTER CODE

//I2C Communication between Two Arduino

#include<Wire.h> //Library for I2C Communication functions

#include<LiquidCrystal.h> //Library for LCD display function

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //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("Project"); //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(); //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(500);

lcd.clear();

}

//I2C SLAVE CODE

//I2C Communication between Two Arduino

#include<Wire.h> //Library for I2C Communication functions

#include<LiquidCrystal.h> //Library for LCD display function

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //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("Project"); //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(500);

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

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.