Exercise, Week 14 - Networking and Communications

 

Group Assignment - Send a message between two projects.

 

This is a group assignment done with Ting Kok Eng and  Noel Kristian.
Our collective work is documented on the 
SP Fablab Website Assignment 10 and hence only my learning and reflections are documented here

 

Individual Assignment

 

This is week topic is on Networking and Communications. I had designed a network and established I2C communications using a computer with an Arduino UNO as a Master, an ATtiny44 as Slave 1 and ATtiny45 as Slave 2, and connected wires to SDA and SCL on these boards (1).

 

 

 

INTRODUCTION TO I2C COMMUNICATION

I2C combines the best features of SPI and UARTs. With I2C, I can connect multiple slaves to a single master or can have multiple masters controlling single, or multiple slaves, and this only uses two wires to transmit data between devices. Is useful when there is more than one microcontroller logging data to a single memory card or displaying text to a single LCD (1).

 

I2C uses two wires to connect SDA and SCL on these devices.

·      SDA (Serial Data) – The line for the master and slave to send and receive data.

·      SCL (Serial Clock) – The line that carries the clock signal.

 

I2C is a serial communication protocol, so data is transferred bit by bit along a single wire (the SDA line). Like SPI, I2C is synchronous, so the output of bits is synchronized to the sampling of bits by a clock signal (SCL line) shared between the master and the slave. The clock signal is always controlled by the master (1). I2C bus need two pull-up resistor 4.7 kohm, one at the SDA line and one at the SCL line. If the resistor is missing, the SCL and SDA lines will always be low and the I2C bus will not work.

 

HOW I2C WORKS (1)

Data in I2C is transferred in messages, and it contains of frames data.   

This message has…

·      address frame that contains the binary address of the slave,

·      one or more data frames that contain the data being transmitted,

·      start and stop conditions, read/write bits, and ACK/NACK bits between each data frame.

 

ADDRESSING

I2C does not have slave select lines like SPI, so it needs another way to let the slave know that data is being sent to it, and not another slave. It does this by addressing, the address frame is always the first frame after the start bit in a new message. The master sends the address of the slave it wants to communicate with to every slave connected to it. Each slave then compares the address sent from the master to its own address. If the address matches, it sends a low voltage ACK bit back to the master. If the address does not match, the slave does not thing and the SDA line remains high (1).

 

Video of Networking and Communications using I2C

 

TinyWire library

I am using the built-in 'Wire.h' Arduino library for my Master Device (Arduino UNO). And, using the TinyWireS.h library for my Slave Devices an ATtiny44 and ATtiny45. The TinyWire library only work on ATtiny MCU as it is built upon 'TWI' which is a sub-set of the implementation of I2C on the ATtiny MCUs family.

 

Arduino Code for Master Device (an Arduino UNO)

 

Description of Program Code for Master Device (an Arduino UNO)

First, join I2C bus "busWire.begin()" and a serial communication "Serial.begin(9600)" is established between my computer and an Arduino UNO. Then serial monitor is executed, ready to receive user command "process_incoming_command()" from host PC via Software Serial, user can than enter ‘0’ for Arduino UNO as Master , ‘1’ for an ATtiny44 as Slave 1, and ‘2’ an ATtiny45 as Slave 2…

·      When user entered ‘0’, a LED will blink 3 times

·      When user entered ‘1’, a I2C will begin transmission to Slave 1 address, instruct it to blink a LED 3 times, then end the transmission

·      When user entered ‘2’, a I2C will begin transmission to Slave 2 address, instruct it to blink a LED 3 times, then end the transmission

Serial communication will also read user input "cmd = Serial.read()" and display it "Serial.println(cmd)" on the screen.

 

Arduino Code for Slave Device 1 (ATtiny44) and Slave Device 2 (ATtiny45)

 

Description of Program Code for Slave Device 1 (ATtiny44) and Slave Device 2 (ATtiny45)

After serial communication is established, Slave Device 1 and Slave Device 2 can join I2C bus as slave using it assigned address "TinyWireS.begin(I2C_SLAVE_ADDRESS)". When the Master Device called the Slave Device based on user input, and the Slave Device is available "if (TinyWireS.available())" the blink function "blink_led()" will execute a LED to blink 3 time.

 

 

Source Codes for Arduino UNO

1. Arduino Code for Master Device (Arduino UNO), right mouse click and save link: wk14unoi2cmaster.ino

2. Arduino Code for Slave Device 1 (ATtiny44), right mouse click and save link: wk14slave44.ino

3. Arduino Code for Slave Device 2 (ATtiny45), right mouse click and save link: wk14slave45.ino

 

 

Design Files for Slave Boards (ATtiny44 Board and Attiny45 Board)

1. Design file for ATtiny44 board, please refer to Input Devices.

2. Design file for ATtiny45 board, please refer to Electronics Design.

 

 

What I have learned: 

 

1. There are different methods to send a message between two projects, e.g. I2C (inter integrated circuit), SPI (serial peripheral interface), UART (universal asynchronous receiver transmitter), Bluetooth and etc.

2. Bit-bang I2C is an alternative Networking and Communication using I2C protocol on any IO pins, rather than just the SDA/SCL pins.

3. I2C bus need two pull-up resistor 4.7 kohm, one at the SDA line and one at the SCL line. If the resistor is missing, the SCL and SDA lines will always be low and the I2C bus will not work.

 

Citation

1. Reference from URL on 13/05/2020: https://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/