Skip to content

14. Networking and communications

This week I worked on defining my final project idea and started to getting used to the documentation process.

Group assignment

Individual assignment

Individual assignment this week was design, build, and connect wired or wireless node(s) with network or bus addresses

For this assignment, I decided to try I2C connection.

From the above picture, one of the advantages of I2C connection is that it only needs two single connections (pins) to microcontrollers to multiple devices.

The overview of I2C connection is

  1. The master device sends read/write requirement and addresses assigned to each slave to the slaves.
  2. All slaves receive the address data based on the SCL clock.Only when the address is the same as the one registered in the slave, the slave will send data to the master device.

Designing board

I only designed the master board because I would use boards created previously.

The designed CAD is shown below.



Programming

I used Arduino IDE to program I2C communication codes. In my case, the master device was connected to one slave device,the board for a LED and a button created peviously.

Master device

#include <TinyWireM.h>
#define slave1 (1)

void setup() {
  TinyWireM.begin();
}
  byte x=0;
void loop() {
  TinyWireM.beginTransmission(slave1);
  TinyWireM.send(1);
  TinyWireM.endTransmission();
  delay(1000);

  TinyWireM.beginTransmission(slave1);
  TinyWireM.send(0);
  TinyWireM.endTransmission();
  delay(1000);
}
#define slave1 (1)  

This line defines the address of the slave device(ATtiny44 on the slave device).

TinyWireM.beginTransmission(slave1);

Set up slaver’s address.

TinyWireM.send(1);   
TinyWireM.endTransmission();

Send bytes to the slaves.

Slave device

The slave device will blink the LED only when the slave device received the same address as the one registered beforehand.

#include <Wire.h>

#define led_pin 8
void setup() {
  pinMode(led_pin,OUTPUT);
  Wire.begin(1);
}

void loop() {
  volatile byte msg = 0;
      msg = Wire.read();
      if(msg==1){
          digitalWrite(led_pin,HIGH);
          delay(100);
        }
       else{
        digitalWrite(led_pin,LOW);
        delay(100);
        }

}
Wire.begin(1);

Start I2C communication. Registered address of this slave is 1.

Wire.read();

Read data from the master device.

Setting environment

In my case, an additional environment setting was required. When first I tried to use “Wire.h” and “TinywireS.h” for the slave board using ATtiny44, both library could not be used, although the master using ATtiny45 could use TinywireM.h.

After I googled the solution for this issue, I found the past student’s page that shows how to execute Wire.h in Attiny44.

From this web page, additional board manager library file was required. All files and tutorial are shown here.

After I completed to install new board manager library file, I could compile the code for the slave device.

Wiring

Actual wiring is shown below.

Pull-up registers were already included in Master board, so just wiring power, GND, SDA, and SCL was enough to prepare I2C communication.

From the above video, durations of the LED to blink was not 100ms(0.1s), it looked like about 1000ms. This was because the master code defined the duration of sending as 1000ms, so the duration to blink LED was not 100ms. Therefore, this result can be an evidence that I could implement I2C communication.

Eagle_schematic
Eagle_board

Master_code
Slave_code