Challenge

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


/*************************** MASTER ARDUINO *****************************************/


#include<Wire.h>


void setup()
{
pinMode(A1, INPUT);
Serial.begin(9600);
Wire.begin();
}

void loop()
{
Wire.beginTransmission(9); // Begin I2C communication with Slave Arduino
Wire.write("FrmMasterArduino"); // Send a message
//Wire.write(" "); // Send a message
Wire.endTransmission(); // End the transmission
delay(2000); // Wait for a second
}

/*************************** SLAVE ARDUINO *****************************************/


#include<Wire.h>
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
char str[100];
int i = 0;
void setup()
{
Serial.begin(9600);
Wire.begin(9); // Start the I2C communication as a slave with address 9
Wire.onReceive(receiveEvent); // Set the receive event
lcd.begin(16,2);
}
void loop()
{
delay(1000);
}

void receiveEvent(int bytes) {
while (Wire.available()) { // Read all the bytes received
char c = Wire.read(); // Read a byte
str[i] = c;
i++;
}
str[i] = '\0';

Serial.println(str);
// fflush();
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0,0); // Set cursor to the first column, first row
lcd.print("Slave Arduino"); // Print some text on the LCD
lcd.setCursor(0,1); // Set cursor to the first column, second row
lcd.print(str); // Print the received message on the LCD
// lcd.clear(); // Clear the LCD screen
Wire.flush();
}

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.