Challenge

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

#include // lib for I2C comm
#include // lib for LCD display fun
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); // connected to LCD module pins(RS, EN, D4, D5, D6, D7)

void setup()
{
lcd.begin(16,2); // initializing LCD display
lcd.setCursor(0,0); // sets the cursor at 1st line of display
lcd.print("Embedded Systems"); // prints in lcd
lcd.setCursor(0,1); // sets the cursor in 2nd line of display
lcd.print("I2C Master arduino"); // prints in LCD
for(int i=0; i<16; i++)
{
lcd.scrollDisplayLeft();
delay(100);
}
delay(2000);
lcd.clear(); // clears LCD display
Serial.begin(9600); // begins serial comm at 9600 baud rate
Wire.begin(); // begins I2C comm at pin (A4, A5)
}

void loop()
{
Wire.requestFrom(8,1); // request 1 byte from the 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); // starts transmit to slave arduino (8)
Wire.write(MasterSend); // sends 1 byte converted pot value to slave
Wire.endTransmission(); // stops transmitting
lcd.setCursor(0,0); // sets cursor at line 1 of LCD
lcd.print(">> the Master <<"); // prints in LCD
lcd.setCursor(0,1); // sets cursor at line 2 of LCD
lcd.print("slaveval:"); // prints 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();
}

#include // lib for I2C comm
#include // lib for LCD display fun
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); // connected to LCD module pins(RS, EN, D4, D5, D6, D7)
byte SlaveReceived = 0;

void setup()
{
lcd.begin(16,2); // initializing LCD display
lcd.setCursor(0,0); // sets cursor at 1st line of display
lcd.print("Embedded Systems"); // prints embedded systems
lcd.setCursor(0,1); // sets cursor at 2nd line of LCD display
lcd.print("I2C Slave arduino"); // prints in LCD
delay(5000);
lcd.clear();
Serial.begin(9600); // begins serial comm at 9600 baud rate
Wire.begin(8); // begins I2C comm with slave address at 8 pin
Wire.onReceive(receiveEvent); // fun call when slave receives value from master
Wire.onRequest(requestEvent); // fun call when master request value from slave
}

void loop(void)
{
lcd.setCursor(0,0);
lcd.print(">> the Slave <<");
lcd.setCursor(0,1);
lcd.print("Masterval:");
lcd.print(SlaveReceived);
Serial.println("Slave received from Master:");
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}

void receiveEvent (int howMany) // fun is called when slave receives value from master
{
SlaveReceived = Wire.read(); // used to read value received from master and store in var slavereceived
}

void requestEvent() // fun calls when master wants value from slave
{
int potvalue = analogRead(A0); // read 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 1 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.