Skip to content

14. Embedded Networking and Communications

This week I worked on Networking and communications between two microcontrollers using i2c

Group assignment:

Send a message between two projects

For more detail check the Group Page

Individual assignment:

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

Group assignment

We did I2C communication between ATtiny44 board and Atmega board.

Master Board (Mohammad Alshamsi)

We used the Atmega board that Mohammad designed and we uploaded the following Master code to the board.

#include <Wire.h>

byte on = 100;
byte off = 200;

void setup() {

//Wire.begin();
pinMode(3,OUTPUT);
Wire.begin();
Wire.setClock(10000) ;

}

void loop() {

Wire.beginTransmission(55);
Wire.write(on);
Wire.endTransmission();

delay(500);

Wire.beginTransmission(55);
Wire.write(off);
Wire.endTransmission();

delay(500);

}

Slave Board (Sara Alhadhrami)

We used the ATtiny44 board that Sara Alhadhrami designed and we uploaded the following Slave code to the board.

#include <TinyWire.h>

#define led_pin 1

byte own_address = 55;
byte received;

void setup() {
  // config led_pin as Output for driving an LED
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, HIGH);

  // config TinyWire library for I2C slave functionality
  TinyWire.begin( own_address );
  // sets callback for the event of a slave receive
  TinyWire.onReceive( onI2CReceive );
}

void loop() {

}

/*
I2C Slave Receive Callback:
Note that this function is called from an interrupt routine and shouldn't take long to execute
*/
void onI2CReceive(int howMany){

  received = TinyWire.read();

  if(received==100){
  digitalWrite(led_pin, LOW);
  }else
  if(received==200){
  digitalWrite(led_pin, HIGH);
  }
}

The following video shows I2C communication between Atmega (Master) and ATtiny44 (Slave) PCBs.

Individual assignment:

For this Assignment I’ll use two of my PCBs that I have made in Electronics design week and Final project. Here is very nice tutorial in how to do I2C Connection.

I2C Protocol

I2C combines the best features of SPI and UARTs. With I2C, you can connect multiple slaves to a single master and you can have multiple masters controlling single, or multiple slaves. This is really useful when you want to have more than one microcontroller logging data to a single memory card or displaying text to a single LCD. circuitbasics.com

circuitbasics.com

I2C only uses two wires to transmit data between 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. circuitbasics.com

  • Here is the Master board

  • Here is the Slave Board

  • The problem is that this board doesn’t have SDA and SCL port, so have to fix the board and SDA and SCL header

  • I used dremel tool make new traces

  • Here is the new traces

  • Here is the board after soldering a new header for i2c communication

  • Now the board is ready for communication and I connect SDA and SCL pins of the both boards together. Also for power I have connect Ground and VCC.

  • Here is the master Code which will send values from 0 to 6
// 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(2); // transmit to device #2
  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);
}

I2C Connection

  • Here is the Slave Code which will blink LED for 200ms when receive “0” and will blink LED for 400ms when receive “3”
//Include the required Wire library for I2C

#include<Wire.h>

int LED = 10;
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(2); 
  // 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);
  }
}

I2C Connection

I have uploaded the code to the both boards using FTDI cable

Here is the video of the two boards working with i2c communication


Last update: October 28, 2021