15. Networking and communications

Individual assignment:

  • Design, build, and connect wired or wireless node(s) with network or bus addresses

Group assignment:

  • Send a message between two projects

15.1 Individual assignment

I am going to test the I2C connection to communicate 2 boards with Attiny44.

15.1.1. I2C Information

First I look for information about this protocol. On this page of Instructables.com I find information.

I2C (Inter-Integrated Circuit Bus), also commonly known as TWI (Two Wire Interface), is a two-wire synchronous serial bus cable.

  • Use two cables. SDA (serial data) and SCL (serial clock). Each line has a pull-up resistance between the line and the positive rail. A good pull-up resistor is 1-10K ohms.

  • It is a synchronous communication: this means that the data transfer is synchronized through a clock signal, generated by the master, which is present in all the connected devices.

  • Serial: Serial transferred data means that a single bit is transferred at a time over a single cable.

  • Bus: A bus is a system that allows many devices to communicate with each other through a single set of cables. They use an addressing system, in which each device has a unique address.

A master can request to send or receive data from a slave. During a send, the master writes data to the bus and the slave reads the data from the bus and stores it in its memory. During a reception, the master reads the bus for the data sent by the slave. In both situations, the master provides the clock signal in SCK.

15.1.2. Libraries for I2C

I only know the Wire.h library to program I2C communication between devices, since I had only tried it with arduinos.

If I try to compile the code example of the Wire.h library in Arduino IDE with the Attiny44 I get many errors.

I think these errors are caused because Wire.h works with TWI hardware, however ATtiny44 has USI hardware for communication.

After searching the internet, I find these libraries for I2C communication with the attinys:

These two are the ones I have used for testing. Later I found others libraries:

  • USIWire. Library that using it together with the Wire.h library makes it compatible with the ATtiny. Library for the Master / Slave device.

  • Wire.h from the SpencerKonde ATTinyCore package. It is a version of Wire.h adapted to ATtiny. Library for the Master / Slave device.

Turns out I already had it installed since I downloaded ATtinyCore in order to use the Arduino IDE with ATtiny. But since it is called the same as the Wire.h library for arduino that I already had installed previously, it gives me errors. Anyway I have realized this after using the other libraries so I have not tried it.

So I have run the tests using TinyWireM.h and TinyWireS.h.

15.1.3. Wiring for I2C

I2C communication uses the SDA and SCL pins. According to the ATtiny44 datasheet these pins match the ones used for MOSI and SCK of the SPI that I use to program the boards. So I can use those connectors.

Also I have to add a pull-up resistor on the SDA and SCL lines. For this I use a breadboard and 1K ohm resistors.

I am going to use my Hello44 board that has a button and a led, and the Motor board that has an A4959 driver and a water pump. The following image shows the schematic of the I2C connection.

15.1.4. Programming

15.1.4.1 Test 1

In this first test, I will make each time you press the master button to turn on its led, send a “1” to the slave, and turn off its led. And the slave every time it receives a “1” from the master change the state of the motor (on / off).

I have explained the code with comments and I have highlighted the lines related to i2c communication

MASTER

SLAVE

This video shows the operation.

You can download scripts here.

15.1.4.2 Test 2

In the second test, when I press the Master button it sends a value (7 to 3), each time I press it is one less unit. The slave receives this value and uses it to change the motor speed.

MASTER

SLAVE

This video shows the operation.

You can download scripts here.

15.2 Group assignment

The Group Assignment page is at the following link.

In the individual assigment I have already sent messages between two projects.