Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
The program is as follows:
#include<wire.h> //Library for I2C communication functions//
#include<LiquidCrystals.h> //Library for displayfunction//
LiquidCrystal lcd(7,8,9,10,11); //Define LCD module pins //
void setup()
{
lcd.begin(16,2); //Intialize LCD display//
lcd.Setcursor(0,0); //SetCursor at first line of Display//
lcd.print("SPEED"); //pirnt circuit digestion in LCD//
lcd.SetCursor(0,1); //Set cursor at second line of display//
lcd.print("Mas***); //print on LCD//
delay(5000); //Delay for 5 seconds//
lcd.clear(); //Begins Serial communication at 9600 baud rate//
Serial.begin(9600); //Begins I2C coomunication 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 & store in MasterReceive
int potvalue=analogRead(A0);//Reads analog value from pot(0-5v)
byte Master send =map(potvalue 0,1023,0,127);
wire.begintrasnmission(8); //start transmission
wire.write(Master Send); //sends one byte of value to slave
wire.endTransmission();
lcd.Setcursor(0,0); // setcursor at first line of LCD
lcd.print("">> Master<<"); //Master prints on LCD
lcd.Setcursor(0,1); //setcursor at second line of LCD
lcd.print("slaveval1:");//prints slave value
lcd.print(Master Receive);
Serial.println("Master received from slave:");
Serial.println(MasterReceive);
delay(600);
lcd.clear();
}
The program code for slave
#include<wire.h> //Library for I2C communication functions//
#include<LiquidCrystals.h> // Library for LCD display function//
LiquidCrystal lcd(7,8,9,10,11);
byte slave Received=0;
void setup()
{
lcd.begin(16,2); //Intialize LCD display//
lcd.setcursor(0,0);//Set cursor at first line of LCD//
lcd.print("SPEED"); //prints circuit digest in LCD//
lcd.Setcursor(0,1); //Set cursor at second line of LCD//
lcd.print("I2C 2 ARDUINO");
delay(5000);
lcd.clear();
Serial.begin(9600);
Wire.begin(8);
Wire.onReceive(receive Event); //function call when slave receives value from Master//
}
void loop()
{
lcd.Setcursor(0,0); //Set cursor at first line of LCD//
lcd.print(">>Slave<<");
lcd.Setcursor(0,1);
lcd.print("Masterval1:");
lcd.print(Slave received);
Serial.println("slave Received from Master");
Serial.println(Slave Received);
delay(600);
lcd.clear();
}
void receive Event(int howmany)
{
SlaveReceived = wire.read();
}
void requestEvent()
{
int potvalue=analogRead(A0);//Reads analog value from POT(0-5V)
byte Slave send = map(potvalue,0,1023,0,127);
Wire.write(Slave send); //send one byte converted POT value to Master//
}