Skip to content

13. Networking and communications

We did this group assignment at the end of the week and used the projects we made during the week. We connected Anush’s project and Maxime’s project together using the I2C protocol.

The connection we made was the following:

only

The code on the primary (SAMD11C) requests some data from Anush’s project (device at address 5) and, once received, passes it on to the secondary on the same board (ATTINY1614 at address 9):

//Primary

#include <Wire.h>

void setup() {
  // Start the I2C Bus as Master
  Wire.begin();
}
void loop() {
  Wire.requestFrom(5, 1);    // request from device #5
  uint8_t received = -1;
  while (Wire.available()) {
    received = Wire.read();
  }

  uint8_t toSend = map(received, 0, 255, 0, 63);

  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(toSend);              // sends
  Wire.endTransmission();    // stop transmitting
  Serial.print("toSend : ");
  Serial.println(toSend);
  delay(1000);
}

The secondary on the ATTINY1614 receives some data from the primary and translates this decimal number into a 6 bits number that is then displayed on the 6 LEDs controlled by the microcontroller:

//Secondary 1
const int PIN_GREEN = 0;
const int PIN_RED   = 1;
const int PIN_BLUE  = 2;

const int PIN_GREEN2 = 5;
const int PIN_RED2   = 4;
const int PIN_BLUE2  = 3;

#include <Wire.h>

int x = 0;
void setup() {
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);

  pinMode(PIN_RED2,   OUTPUT);
  pinMode(PIN_GREEN2, OUTPUT);
  pinMode(PIN_BLUE2,  OUTPUT);

  digitalWrite(PIN_BLUE2,   1);
  delay(3000);
  digitalWrite(PIN_BLUE2,   0);


  // Start the I2C Bus as secondary on address 9
  Wire.begin(9);
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
}
int receiveCounter = 0;
void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}

void loop() {
  pinsDown();

  //decimal to 6 bits binary number

  int fiveBits = x % 32;
  int bitSix = x / 32;
  if (bitSix == 1) digitalWrite(PIN_GREEN,   1);

  int fourBits = fiveBits % 16;
  int bitFive = fiveBits / 16;
  if (bitFive == 1) digitalWrite(PIN_RED,   1);

  int threeBits = fourBits % 8;
  int bitFour = fourBits / 8;
  if (bitFour == 1) digitalWrite(PIN_BLUE,   1);

  int twoBits = threeBits % 4;
  int bitThree = threeBits / 4;
  if (bitThree == 1) digitalWrite(PIN_GREEN2,   1);

  int oneBits = twoBits % 2;
  int bitTwo = twoBits / 2;
  if (bitTwo == 1) digitalWrite(PIN_RED2,   1);

  if (oneBits == 1) digitalWrite(PIN_BLUE2,   1);
}

//sets all the pins to DOWN
void pinsDown() {
  digitalWrite(PIN_RED,   0);
  digitalWrite(PIN_GREEN,   0);
  digitalWrite(PIN_BLUE,   0);
  digitalWrite(PIN_RED2,   0);
  digitalWrite(PIN_GREEN2,   0);
  digitalWrite(PIN_BLUE2,   0);
}

The secondary of Anush’s Project gets some request from the primary. Everytime it gets a requests, it reads the analog pin number 4 and communicates the number read to the primary. Since, in our case, no input device is attached to this pin, the reading is more or less random and serves only as demonstration purpose.

//Secondary 2
#include <Wire.h>

int AnalogPin = 4;
void setup() {

  pinMode(AnalogPin, INPUT);

  Wire.begin(5);                // join i2c bus with address #5
  Wire.onRequest(requestEvent); // register event


}

void loop() {
  delay(100);
}

void requestEvent() {

  int AnalogValue = analogRead(AnalogPin);

  uint8_t mappedNumber = map(AnalogValue, 0, 1023, 0, 255);

  Wire.write(mappedNumber);

  Serial.println(mappedNumber);
}

And here is the result:


Last update: June 20, 2023