This week we are to make something that has Network & Communication. Thus I've decided to make I2C as my Final project will have an LCD which I decided to have and LCD indicator, and to also make a better understanding of how it work.
"It is a serial Protocol for 2-wire interface to connect low-speed devices like microcontrollers, EEPROMS, A/D and D/A converters, I/O interfaces and other similar peripherals" source
In short, it is using a Master board to control the slave board via 2 lines, SDA and SCL.
But before I jump in into the LCD module for my final project, I designed the I2C Bridge and Node done by Neil and to create it, I'll use Eagle again to redesign the board.
In order for ATtiny MCU to communicate using I2C, new libraries need to be install into Arduino IDE. As a Master ->TinyWireM, as a Slave ->TinyWireS.
To include library in Arduino IDE. TinyWireM.h as master and TinyWireS.h as slave
#include "TinyWireM.h" #include "TinyWireS.h"
In I2C, to initialize as Master address is not require. But as a Slave user need to predefine an address for the MCU
Using the command "begin" for initializing the MCU
TinyWireM.begin(); ------------------------------- #define I2C_SLAVE_ADDRESS 0x6 TinyWireS.begin(I2C_SLAVE_ADDRESS); // join i2c network as slave
Example are included when both the library are install, or more example can be found on the web site (TinyWireM, TinyWireS).
#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); }
First, the library TinyWireM is included, next is to define the 2 slaves that I would be using.
Next, we will begin the TinyWireM library, and then it will parse into the Loop
In the loop, it will 'transmit' a signal, 1, to the 'slave1' and it will end the transmission and delay for 2 seconds. Then it will 'transmit' a signal, 0, to the 'slave1' again and it will end the transmission and delay for 2 seconds.
After "slave1" is done, "slave2" will be called in action, and back to "slave1".
#include <TinyWireS.h> #define output (PB0) #define I2C_SLAVE_ADDR (1) 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; }
#include <TinyWireS.h> #define output (PB0) #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; }
Each slave has it's own address, so each Slave's address is defined as I2C_SLAVE_ADDR(1) & I2C_SLAVE_ADDR(2). As each slave there is an LED, therefore we need to declare the pinMode as an OUTPUT.
In the loop, it is receving messages from the Master and based on the condition, if the message is 1, it will turn on the LED Light and when it is 0, it will turn off the light. Simple as that.
I2C master Schemetic:Here
I2C master Board:Here
I2C node Schemetic:Here
I2C node Board:Here
Arduino: Master, Slave1, Slave2