Networking & Communications

Introduction

In this week we will design, build, and connect wired or wireless node(s) with network or bus addresses.


I2C Communication

For this week, we chose to work on the I²C, is a multi-master, multi-slave, serial computer bus . It supports several slaves (eg 100+) allowing one transmission per time between the master and one slave. For I2C to work, the system requires one device to be set as a master to control the flow of information. The rest of the devices are set as slave devices. The slave devices receive commands from the master and send information at the request of the master.


I2C communication on Arduino/PCB

To start the tast, i used my Atmega 328p PCB that i made, as a Master and used an Arduino as a slave. The code was simple, basically to send 2 characters to the slave and then based on the condition it will start turning the LED ON and OFF. I2C devices communicate with 2 signals, SDA and SCL.The connection is as follows:
  • 5V on arduino connected to Vcc pin on the PCB
  • GND pin on Arduino to GND pin on PCB
  • SDA: A4 pin on arduino to PC4 on PCB
  • SCL:A5 pin on arduino to PC5 on PCB
The library that will be used for the code is the Wire.h library which is installed by default in the Arduino IDE, this library allows us to communicate with I2C / TWI devices. The commands used are explained in the code itself. I uploaded the codes, and monitored the slave through the serial monitor to check the received data, which was successful and in return the led went on and off.
Downloads

Slave Code
 
#include <Wire.h>
void setup()
{
  Wire.begin(1);
  // Begin using Wire in slave mode, where it will respond at "1" 
  when other I2C masters chips initiate communication.
  Wire.onReceive(receiveEvent);
  // Causes "receiveEvent" to be called when a master
  device sends data. This only works in slave mode.
  Serial.begin(9600);
  //start serial communication
  pinMode(13,OUTPUT);
  //LED pin on arduino }
byte x = 0;
void loop ()
{
 delay(100);
}
//function that executes whenver data is received from master
void receiveEvent(int howMany){ 
//howMany contains the amount of bytes of the received data
while(Wire.available()){
char c = Wire.read();
Serial.println(c);
if (c == 's')
  {digitalWrite (13,HIGH);}
  //Turn on LED
  else if (c == 'x')
  {digitalWrite (13,LOW);}
  //Turn off LED
}} 
Master Code
            
            
#include <Wire.h>
void setup()
{
  Wire.begin();
  // Begin using Wire in master mode, it will initiate and 
  control data transfers.

}

void loop ()
{
  Wire.beginTransmission(1);  
  //Start a new transmission to a device at "address number 1"
  Wire.write('s');            
  //send data: letter s
  Wire.endTransmission();   
  //ends the transmission and causes all buffered data to be sent.

  delay (500);

 Wire.beginTransmission(1);  
 //Start a new transmission to a device at "address number 1"
 Wire.write('x');           
 //send data: letter x
 Wire.endTransmission();   
 //ends the transmission and causes all buffered data to be sent.
 delay (500);
}   


  


Group Assignment

For this task, we need to send a message between two projects.
The link to the group assignment page is found here.


Experience

A very important week that explains how to share data between different nodes/devices and the different transmission mediums available. This will also help us decide choose the applicable method to use for the final project.