Networking and communication

This week’s group assignment goals were the following:


In this week, we attempted to connect several boards together via I2C connection. Our goal was to create a single sender multiple receiver communication line, where the master would send a letter to each of the receivers and each would respond accordingly.

The plan was for each student to use either an Atmega 328 board, or an Attiny 44 Board of what we designed and manufactured in the lab. However, after several failed trials to connect the Attiny, we discovered that the library is not working properly and contains bugs as Maha previously mentioned in her documentation.

Thus, we opted to use a combination of manufactured Atmega 328 boards, and some commercial Arduino boards. We assigned a task to each of the receivers.

The tasks were the following:

Maha: Master

For my board, I was using an Arduino UNO, and I was set to send the signals to the boards connected. I sent 5 signals in total, each time establishing the connection with each device.

#include <Wire.h>
void setup()
{
  Wire.begin(); // join i2c bus
}

void loop()
{
  Wire.beginTransmission(1); // transmit to device #1
  Wire.write("a");// sends a to write a message on the serial monitor
  Wire.endTransmission(1); // stop transmitting
  delay(100);
  Wire.beginTransmission(2); // transmit to device #2
  Wire.write("b");// sends b to turn on LED at 1000 ms intervals
  Wire.endTransmission(2); // stop transmitting
  delay(100);
  Wire.beginTransmission(3); // transmit to device #3
  Wire.write("c");// sends c to turn on LED at 500 ms intervals
  Wire.endTransmission(3); // stop transmitting
  delay(100);
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write("d");// sends d to emit sound from the buzzer
  Wire.endTransmission(4); // stop transmitting
  delay(100);
  Wire.beginTransmission(5); // transmit to device #5
  Wire.write("e");// sends e to turn the servo motor.
  Wire.endTransmission(5); // stop transmitting
  delay(100);
}

Shamma: Slave #1

#include <Servo.h>
#include <Wire.h>
void setup() {
  Wire.begin(1);                // stablish an I2C connection on slave #1
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  pinMode(6,OUTPUT);
}
void loop() {
  delay(10);
  }
  void receiveEvent()
{
  while(Wire.available()) // while the connection is availabe
  {
    char c = Wire.read(); // recieves the button state
    //Serial.println(c); // prints the button state on the serial monitor
  if (c == 'a'){
    digitalWrite(6,HIGH);
    delay(1000);
    digitalWrite(6,LOW);
    delay(1000);
  }
  }
}

Shaikha: Slave #2

For my part, I used my Atmega 328 (Arduino) PCB that I created for my final product, as Slave number 2, my role was to receive character from the Master, the letter “b”, and to display it on the Serial Monitor through the FTDI cable. The code is shown Below:

#include <Wire.h>
void setup()
{
  Wire.begin(2);                // stablish an I2C connection on slave #2
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}
void loop()
{
  delay(10);
}
void receiveEvent()
{
  while(Wire.available()) // while the connection is available
  {
    char c = Wire.read(); // stores whatever is received

if (c == "b"){
 Serial.println(c); // prints the letter sent from Master on serial monitor

  }
  else {
    Serial.println("Nothing Received");
  }
  }
}

And the serial monitor screen showed the following, taking into consideration the delay between each output :

Fatima: Slave #3

First I selected to use my ATtiny 44 board as slave, but unfortunately, I faced problems with WireTiny, I need to use this library for I2C communication on ATtiny boards, the library no more active or available in Arduino IDE, we tried to download and try different libraries fo WireTiny. Thus I had to change from ATtiny board to Arduino board. I was slave number 3, I will receive character “ c “ from the master board, then the LED connected in pin 13 will Blink with 1 sec delay, The different between my code the slave#4 is the delay as shown below the code:

#include <Wire.h>
void setup()
{
  Wire.begin(3);                // stablish an I2C connection on slave #1
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  pinMode(13,OUTPUT);// sets LED as output
}
void loop()
{
  delay(10);
}
void receiveEvent()
{
  while(Wire.available()) // while the connection is availabe
  {
    char c = Wire.read(); // recieves the button state
    //Serial.println(c); // prints the button state on the serial monitor
  if (c =='c'){ // if the button is released
    digitalWrite(13,HIGH);// turn the LED off
    delay(1000);
    digitalWrite(13,LOW);// turn the LED off
  }
}
}

Ali: Slave #4

I am using an Arduino UNO, and I will be receiving the fourth message from the master. I wrote my code so that when the master sends the letter “d”, on the transmission, the LED on the Arduino board will blink every 500 ms. Fatima is also doing the blinking, and we are comparing the speed of blinking with each other’s boards.

void setup()
{
  Wire.begin(4);                // stablish an I2C connection on slave #1
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  pinMode(13,OUTPUT);// sets LED as output
}
void loop()
{
  delay(10);
}
void receiveEvent()
{
  while(Wire.available()) // while the connection is availabe
  {
    char f = Wire.read(); // recieves the button state
    //Serial.println(f); // prints the button state on the serial monitor
  if (f =='d'){ // if the button is released
    digitalWrite(13,HIGH);// turn the LED off
    delay(500);
    digitalWrite(13,LOW);// turn the LED off
  }
}
}

Waleed: Slave #5

I am using an Arduino UNO, and I will be receiving the 5th message from the master. whenever I receive the letter e the buzzer will start working for one second which is attached to pin 8.

#include <Wire.h>
void setup()
{
  Wire.begin(5);   // join i2c bus with address 8
  Wire.onReceive(receiveEvent);  //register event
  Serial.begin(9600);   //start serial communication
  pinMode(8,OUTPUT);
}
byte x = 0;
void loop ()
{
 delay(100);
}
//function that executes whenver data is received from master
//this function is registered as an event
void receiveEvent(int howMany){
  while(Wire.available()){
    char c = Wire.read();
   if (c =='e'){
  digitalWrite (8,HIGH);
  delay(1000);}
  else{
  digitalWrite (8,LOW);
  delay(1000);}
}
}

Results

We connected all the boards together, using four wires each [SDA, SCL, GND, VCC]. And other wires for the output devices connected, and we gave the power through the FTDI cable connected to Shaikah's device, as she needed to read the serial monitor.

The slaves received the signals and generated an output accordingly.