14. Networking and communications

Goal(s):

  • individual assignment: design, build, and connect wired or wireless node(s) with network or bus addresses

  • group assignment: send a message between two projects

Introduction

The goal of this week is to communicate between 2 microcontrollers. This week I learned the basics of different communication protocols and decided to build and control mcu’s using I2C protocol. This was the toughest week for me as the logic of this exercise is still not full understood by me. I decided to replicate an earlier project and then do a deep dive after the course to understand this better.

Embedded Communication

Embedded communication means transport of information from one circuit to another circuit. Where the circuit which transmits the data is called Transmitter and the circuit who receive the data is called Receiver. Atleast one Transmitter and a Receiver is required to establish the communication. Communication may be in one direction or in both directions, there are different classifications according to this, basic classification is Synchronous and Asynchronous communication.

  • Synchronous means: sender and receiver use the same clock signal

  • Asynchronous Means: sender provides a synchronization signal to the receiver before starting the transfer of each message.

Communication Protocols

A communication protocol is a system of rules that allow two or more entities of the communications system to transmit information via any kind of variation of a physical quantity. Communications protocols cover authentication, error detection and correction, and signaling. They can also describe the syntax, semantics, and synchronization of analog and digital communications. Communications protocols are implemented in hardware and software. There are thousands of communications protocols that are used everywhere in analog and digital communications.

Communications devices have to agree on many physical aspects of the data to be exchanged before successful transmission can take place. Rules defining transmissions are called protocols. There are two main categories in this.

  1. Inter System Protocol

This is used to communicate between two different devices like a communication between a computer and microcontroller. It uses a dedicated bus to communicate.

Different Inter system protocol:

UART Protocol USART Protocol USB Protocol

  1. Intra System Protocol

This is used to communicate two devices within the circuit. The circuit complexity, power consumption will be less in this.

Different Intra system protocol:

  • I2C Protocol
  • SPI Protocol
  • CAN Protocol

I shared the above information here for getting and idea about the communication protocols which I got from here.

Inter integrated Circuit (I2C) Protocol

I2C is a serial protocol for two-wire interface to connect low-speed devices like microcontrollers, EEPROMs, A/D and D/A converters, I/O interfaces and other similar peripherals in embedded systems. It was invented by Philips and now it is used by almost all major IC manufacturers. There are a lot of documentations and tutorials are available in internet regarding this topic. One of the useful documentation I referred is I2C BUS. The bus configuration of I2C communication is shown below.

Since my LCD Screen uses I2C protocol for receiving information from the temperature sensor, I decided to go with I2C protocol only.

Research

To get started with this assignment, I went through Jim’s documentation for the Networking Assignment to understand how to go about with this project as I was very comfortable with ATTiny85 programming and boards, where he has used ATTiny45.

I went through his schematics and documentation for understanding how to implement I2C protocol with my boards.

I downloaded his files and opened the schematic in EasyEDA software to understand the wiring and logic used, since it was based on the the Fab Archive documentation shared by Neil, I went to the source file to redraw the circuit.

Schematics and Boards

Master Schematic

Master Board

Slave Schematic

Slave Board

Final Prepared Boards

Master Board

Slave Board

Coding the Boards

Note: For having I2C on attiny85 we need to have two libraries TinyWireM and TinyWireS respectively. In the above code the LED pin and I2C is initialized as slave with address 0x4 in setup. And in the loop, transmission is received from master (alternating 0 and 1) and LED is turned ON when 1 is received and off when 0 is received from the master.

Using Arduino as Master and Attiny85 as Slave

Using the Arduino IDE and Arduino as ISP, I tried programming the master and slave boards, but unfortunately,…

Sketch uses 804 bytes (9%) of program storage space. Maximum is 8192 bytes.
Global variables use 30 bytes (5%) of dynamic memory, leaving 482 bytes for local variables. Maximum is 512 bytes.
avrdude: Yikes!  Invalid device signature.
         Double check connections and try again, or use -F to override
         this check.

An error occurred while uploading the sketch

As I was not able to debug the issue and this error was the same across all the combinations, I thought that ther may be some other error that I was overlooking.

Master Code

//I2C Code for Attiny45 as Master

#include < TinyWireM.h>

void setup()
{
  TinyWireM.begin(); // join i2c bus (address optional for master) //I2C is initialized in setup
}

byte x = 0;
                                                                   //And in the loop, transmission is started with address 0x4 (the slave attiny85) with an alternate 0 and 1
void loop() {                                                    
  TinyWireM.beginTransmission(0x4);
 TinyWireM.write(++x % 2);
 TinyWireM.endTransmission();
  delay(1000);
}

Slave Code

//I2C Code for Attiny45 as Slave

#define LED_PIN 3

#define I2C_SLAVE_ADDRESS 0x4 // Address of the slave

#include < TinyWireS.h>
#include < avr/io.h>
#include < util/delay.h>

void setup()
{
   pinMode(LED_PIN, OUTPUT);

  TinyWireS.begin(I2C_SLAVE_ADDRESS); // join i2c network
}

void loop()
{
  byte recd = 1;
  if(TinyWireS.available()) {
     recd = TinyWireS.receive();
     if(recd == 1) {
         digitalWrite(LED_PIN, HIGH);
         delay(1000);
     } else {
     digitalWrite(LED_PIN, LOW);
         delay(1000);
     }
 }
}

Alternative

In order to verify if the board was the issue or the code, I decided to implement a simple Arduino I2C protocol using this link

Master and Slave I2C

Master Code

                                                                          // Include the required Wire library for I2C<br>
#include<Wire.h>
int x = 0;
void setup() {
                                                                          // Start the I2C Bus as Master
  Wire.begin(); 
}
void loop() {
  Wire.beginTransmission(9);                                              // transmit to device #9
  Wire.write(x);                                                          // sends x 
  Wire.endTransmission();                                                 // stop transmitting
  x++; // Increment x
  if (x > 5) x = 0;                                                       // `reset x once it gets 6
  delay(500);
}
                                                                          // Include the required Wire library for I2C
#include <Wire.h>
int LED = 13;
int x = 0;
void setup() {
                                                                          // Define the LED pin as Output
  pinMode (LED, OUTPUT);
                                                                          // Start the I2C Bus as Slave on address 9
  Wire.begin(9); 
                                                                          // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
  x = Wire.read();                                                        // read one character from the I2C
}
void loop() {
                                                                          //If value received is 0 blink LED for 200 ms
  if (x == 0) {
    digitalWrite(LED, HIGH);
    delay(200);
    digitalWrite(LED, LOW);
    delay(200);
  }
                                                                          //If value received is 3 blink LED for 400 ms
  if (x == 3) {
    digitalWrite(LED, HIGH);
    delay(400);
    digitalWrite(LED, LOW);
    delay(400);
  }
}

Master code verified successfully

Master code uploaded successfully to Master Board on the right

Master code verified successfully

Master code uploaded successfully to Master Board on the right

Successfully working

Doing this exercise helped me fail at the networking exercise, but I was able to narrow down the possible error in my protocol to either the board preparation or the code used to upload through the header pins.

Learning

Altough the other exercises in Electronics from Design to Input and output worked, the main issue with the Networking assignment was in understaning the logic used for the Communication and whether there was any other change in programming protocols to the board. I have to explore this topic much more deeply with regards to the course and ensure that I get error free operations in the future.

Download

Codes Boards