Project 1 - Interfacing a 16*2 LCD with Arduino using I2C protocol
Master:
#include <Wire.h>
void setup() {
Serial.begin(9600); /* begin serial comm. */
Wire.begin(); /* join i2c bus as master */
Serial.println("I am I2C Master");
}
void loop() {
Wire.beginTransmission(0x26); /* begin with device address 8 */
Wire.write("Hello Slave"); /* sends hello string */
Wire.endTransmission(); /* stop transmitting */
//Wire.requestFrom(8, 9); /* request & read data of size 9 from slave */
while(Wire.available()){
char c = Wire.read();/* read data received from slave */
Serial.print(c);
}
Serial.println();
delay(1000);
}
Slave:
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
#define SLAVE_ADDRESS 9
//#define LCD_COLUMNS 16
//#define LCD_ROWS 2
//LiquidCrystal_I2C lcd(0x27, LCD_COLUMNS, LCD_ROWS); // Address of the LCD may vary
void setup() {
Wire.begin(0x26); /* join i2c bus with address 8 */
Wire.onReceive(receiveEvent); /* register receive event */
Wire.onRequest(requestEvent); /* register request event */
// lcd.init(); // Initialize the LCD
// lcd.backlight();
Serial.begin(9600); /* start serial comm. */
Serial.println("I am I2C Slave");
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
void receiveEvent(int howMany) {
while (0 <Wire.available()) {
char c = Wire.read(); /* receive byte as a character */
Serial.print(c);
// lcd.print(c);/* print the character */
}
Serial.println(); /* to newline */
}
// function that executes whenever data is requested from master
void requestEvent() {
Wire.write("Hi Master"); /*send string on request */
}
NOTE: I am unable to simulate two i2c.