How to (almost) Use I2C Bridge and Node with Arduino sketch¶
- class page Networking and Communications
- hello.I2C.45.bridge board
-
Note: MOSI_SDA line should be connected to PB0, not PB1
-
hello.I2C.45.node board
- Note: MOSI_SDA line should be connected to PB0, not PB1
Arduino lib Download¶
I2C Master¶
- TinyWireM.zip
- index.zip will download
I2C Slave¶
- TinyWireS.zip
- index(1).zip will download
Arduino Sketch¶
Ref. graduate page
I2C Bridge (Master)¶
#include (TinyWireM.h) #define slave1 (1) #define slave2 (2) void setup() { TinyWireM.begin(); } void loop() { TinyWireM.beginTransmission(slave1); TinyWireM.send(1); TinyWireM.endTransmission(); delay(2000); TinyWireM.beginTransmission(slave1); TinyWireM.send(0); TinyWireM.endTransmission(); delay(2000); TinyWireM.beginTransmission(slave2); TinyWireM.send(1); TinyWireM.endTransmission(); delay(2000); TinyWireM.beginTransmission(slave2); TinyWireM.send(0); TinyWireM.endTransmission(); delay(2000); }
I2C Node (Slave 1)¶
#include (TinyWireS.h) int output=PB4; #define I2C_SLAVE_ADDR (1) //#define I2C_SLAVE_ADDR (2) void setup() { // put your setup code here, to run once: TinyWireS.begin(I2C_SLAVE_ADDR); pinMode(output, OUTPUT); } volatile byte msg = 0; void loop() { if (TinyWireS.available()) msg = TinyWireS.receive(); if (msg == 1) digitalWrite(output, HIGH); else if (msg == 0) digitalWrite(output, LOW); else msg = 0; }
I2C Node (Slave 2)¶
#include (TinyWireS.h) int output=PB4; //#define I2C_SLAVE_ADDR (1) #define I2C_SLAVE_ADDR (2) void setup() { // put your setup code here, to run once: TinyWireS.begin(I2C_SLAVE_ADDR); pinMode(output, OUTPUT); } volatile byte msg = 0; void loop() { if (TinyWireS.available()) msg = TinyWireS.receive(); if (msg == 1) digitalWrite(output, HIGH); else if (msg == 0) digitalWrite(output, LOW); else msg = 0; }
- the master is communicating with the node (slave 1), then send a 1 and 0 to the node.
- The node (slave 1) will then respond by turning ON and OFF the LED which correspond with the condition set with the message receive.
- the master communicate with node (slave 2) and then the process is repeated.