Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
1. master card code:
#include // lib for I2C
void setup(){
Wire.begin(); // initializing I2c
Serial.begin(9600); // init Serial monitor
}
void loop(){
Wire.beginTransmission(4); //4 is the I2C address of the target device
Wire.write((byte)(analogRead(A0) & 0xFF)); // Send the low byte
Wire.write((byte)((analogRead(A0) >> 8) & 0xFF)); // Send the high byte
Wire.endTransmission(); // end transmission
Serial.print("sending data from master"); // printing data on serial monitor
Serial.print(analogRead(A0)); // printing data on serial monitor
Serial.print("\n");// to creat new line in serial monitor
delay(500);}
2. Slave card code:
#include // lib for I2c Communication
#include // lib for LCD display
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2); // LCD pin configuration
void setup() {
lcd_1.begin(16, 2); // cursor
lcd_1.print("Data recvd from master"); // Printing
Wire.begin(4); // Set Arduino as an I2C slave with address 4
Wire.onReceive(receiveEvent); //recieved data on I2c pins
Serial.begin(9600);
}
void loop() {
delay(1000);
}
void receiveEvent(int howMany) {
if (howMany >= 2) { // Check if there are at least 2 bytes available (for an integer)
byte lowByte = Wire.read();
byte highByte = Wire.read();
int receivedInteger = (highByte << 8>
Serial.print("From master: ");
Serial.println(receivedInteger);
lcd_1.setCursor(0, 1);
lcd_1.print(receivedInteger);//printing recieved data from master in LCD
}
}