Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Project:
/* Program for "Interfacing a 16*2 LCD with two Arduino's (one for Master & one for
Slave) using Inter-Integrated Circuit (12C) communication protocol" by sending the
message to LCD using I2C communication protocol and display the message on LCD
screen*/
//12C MASTERCODE
1/12C communication between two arduino
#include«Wire.h> //Library for 12C
Communication functions
#include«LiquidCrystal.h›// Library for LCD
display function
LiquidCrystal Icd(2, 7, 8, 9, 10, 11); //Define LCD
Module Pins (RS,EN,D4,D5,D6D7)
void setup()
Icd.begin (16,2); // Initilize LCD display
Icd.setCursor(0,0);/Sets Cursor at first line
of Display
Icd.print("CYIENT"); //Prints CIRCUIT DIGEST in
LCD
Id.setCursor(0,1);/Sets Cursor at second
line of Display
Icd.print("|2C MASTER ARDUINO"); //Prints 12C
ARDUINO in LCD
delay (5000); //Delay for 5 seconds
Icd.clear@; //Clears LCD display
Serial.begin (9600); //Begins Serial
Communication at 9600 baud rate
Wire.begin); //Begins I2C communication
at pin (A4,A5)
7
void loop(
{
Wire.requestFrom (8,1); // request 1 byte from
slave arduino (8)
byte MasterReceive=Wire.read0);// 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
Icd.setCursor(0,0);/Sets Currsor at line one
of LCD
lcd.print("›> Master «<"); //Prints >> Master «<
at LCD
Icd.setCursor(0,]);/Sets Cursor at line two
of LCD
Icd.print("SlaveVal:"); //Prints SlaveVal: in LCD
Icd.print(MasterReceive);// Prints
MasterReceive in LCD received from Slave
Serial.printin ("Master Received From Slave);
Prints in Serial Monitor
Serial.printin(MasterReceive);
delay (500);
Icd.clear;
for slave
//12C SLAVE CODE
1/12C Communication between Two Arduino
#include«Wire.h> //Library for 12C
Communication functions
#include«LiquidCrystal.h>// Library for LCD
display function
LiquidCrystal Icd(2, 7, 8, 9, 10, 11); / /Define LCD
Module Pins (RS,EN,D4,D5,D6,D7)
byte SlaveReceived=0;
void setup()
{
Icd.begin (16,2); //Initilize LCD display
Icd.setCursor(0,0);/Sets Cursor at first line
of Display
lcd.print ("CYIENT"); //Prints CIRCUIT DIGEST in
LCD
Icd.setCursor(0,1); //Sets Cursor at second
line of Display
lcd.print("I2C SLAVE ARDUINO"); //Prints 12C
ARDUINO in LCD
delay (5000); //Delay for 5 seconds
Icd.clear@;/Clears LCD display
Serial.begin (9600); //Begins Serial
Communication at 9600 baud rate
Wire.begin(8); //Begins 12C communication
with Slave Address as 8 at pin (A4,A5)
Wire.onReceive(receiveEvent);| /Function
call when Slave receives value from master
void loop (void)
{
Icd.setCursor(0,0);/Sets Currsor at line one
of LCD
Icd.print("»> Slave «<"); //Prints »> Slave «< at
LCD
Icd.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.printin("Slave Received From Master'");
Prints in Serial Monitor
Serial.printin(SlaveReceived);
delay (500);
Icd.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
}
