Skip to content

13. Networking and communications

For this week we had to:

  • Group assignment:

    • Send a message between two projects

    • Document your work to the group work page and reflect on your individual page what you learned

  • Individual assignment:

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

Group assignment

Details of our group work can be found here.

The task in the group project was for Maxim’s printed circuit board (PCB), which contains two microcontrollers, to act as the MASTER. Maxim’s PCB has two microcontrollers that communicate with each other using the I2C protocol (SCL and SDA lines). On Anoush’s PCB, there is only one microcontroller.

Connection

The goal of the group project was to set one of Maxim’s microcontrollers as the MASTER, the other as the Secondary, and Anoush’s microcontroller also as a Secondary. Anoush’s microcontroller would receive values from analog pin 4 within the range of (0, 1023). Then, using the map() function, the range would be narrowed down to (0, 255).

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

The MASTER would request and receive values from Anoush’s microcontroller within the range of (0, 255), and then using the map() function, narrow down these values to the range of (0, 63).

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

Afterwards, the MASTER would transmit this information to the microcontroller on the same PCB. Using the received information, the microcontroller would control the blinking of six LEDs.

Let’s look at the result of the work:

Individual assignment

Data exchange between two microcontrollers will be carried out using the I2C protocol.

The I2C (Inter-Integrated Circuit) protocol is a two-wire communication protocol used to exchange data between devices. It allows the master and Secondary devices to communicate sequentially using the SDA and SCL lines. The I2C protocol provides addressing, synchronization, and the ability to connect multiple Secondary devices to a single bus.

First connection

This week I decided to create a connection between my PCB and the Arduino UNO board.

There is a good article on the Arduino website .

This article also contains code that sends the word hello from one board to another, and the other reads this information and outputs it to Serial Monitor.

Let’s connect pins SCL and SDA to each other. If you look in the datasheet of the ATSAMD11D14A-SS microcontroller that is on my PCB, you can see that the pins PA23 and PA22 correspond to the pins.

Net 1

And then we will upload the code to the appropriate boards. Peripheral sender sketch uploaded to my PCB and Controller reader sketch uploaded to Arduino UNO.

Peripheral Sender Sketch

// Wire Peripheral Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI peripheral device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

Controller Reader Sketch

// Wire Controller Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI peripheral device
// Refer to the "Wire Peripheral Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from peripheral device #8

  while (Wire.available()) { // peripheral may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

The operation of communication between the two boards is demonstrated in the following video:

Operation of three boards

I am very interested in the moment at which several boards are involved to ensure the system’s performance.

To do this, I want to make an experiment where 3 boards will take part at once. One board will be the Master, which will request and receive data from another board (Secondary-1) to which the Hall sensor is connected. Secondary-1 will receive values from the Hall sensor, then use the map() function to narrow the values and send it to Master. And Master will send these values to another board (Secondary-2) to which the servomotor is connected. And since the data received by Secondary-2 is in the range (0, 180), the servomotor will turn to a certain angle.

Let’s make a diagram. Since I am going to connect three boards together at once, I need to use a breadboards to connect the SCL and SDA pins of the three boards.

Let’s connect the SCL and SDA pins of the three microcontrollers on the breadboards. The Master will be my PCB made during the Electronics Production week. And as Secondarys there will be 2 Arduino UNO boards. Also, let’s connect the Hall sensor made by me during the Input Devices week to one Arduino UNO board, via A0 PIN. And to the second Arduino UNO we will connect the servomotor through PIN 9.

Networking 3

Programming will be done in the Arduino IDE. Let’s write codes.

#include <Wire.h>

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

  Wire.beginTransmission(9); // transmit to device #9
  Wire.write(received);              // sends
  Wire.endTransmission();    // stop transmitting

  delay(1000);
}

Let’s explain the code step by step:

  1. Turn on I2C communication and configure Serial to output data to the port monitor.

  2. In an endless loop, the master requests values from the Secondary microcontrollers.

  3. The response from Secondary-1 contains the value of the Hall sensor.

  4. The master sends the values received from Secondary-1 to Secondary-2

  5. The cycle repeats with a delay of 1 second.

#include <Wire.h>

int AnalogPin = A0;
void setup() {
  pinMode(AnalogPin, INPUT);
  Wire.begin(5);                
  Wire.onRequest(requestEvent); 

}

void loop() {
  delay(1000);
}

void requestEvent() {
  int AnalogValue = analogRead(AnalogPin);
  int mappedNumber = map(AnalogValue, 0, 1023, 0, 180);
  Wire.write(mappedNumber);
}

Let’s explain the code step by step:

  1. I2C communication with Secondary-1 is enabled and the request handler function from the Master is set.

  2. In an endless loop, Secondary-1 reads the value of the Hall sensor.

  3. Sends the value of the Hall sensor to the Master.

  4. The cycle repeats with a delay of 1 second.

#include <Wire.h>
#include <Servo.h>    

const int servoPin = 9;    

Servo servo;               
int x = 0;
void setup() {
  Wire.begin(9);  
  servo.attach(servoPin);  

  pinMode(servoPin, OUTPUT);
}
int receiveCounter = 0;
void receiveEvent(int bytes) {
  x = Wire.read();    
}

void loop() {
  Wire.requestFrom(5, 1);
  if (Wire.available()) {
    int hallValue = Wire.read();  
    servo.write(hallValue);
  }
}

Let’s explain the code step by step:

  1. I2C communication with Secondary-2 is turned on and the function of the handler for receiving data from the Master is set.

  2. In an endless loop, Secondary-2 is waiting for a command from the Master.

  3. Depending on the received value, the servomotor will move to the appropriate location.

Let’s look at the output of the code:

So the whole process will look like this:

  1. Master board requests data from Secondary-1.

  2. Secondary-1 reads values from the Hall sensor in the range (0, 1023) and narrows them using map() to the range (0, 180).

  3. Secondary-1 sends the narrowed values back to the master.

  4. Master board transmits the received values to Secondary-2.

  5. Secondary-2 receives the values and controls the servo by turning it to the appropriate angle.

Thus, as a result of the experiment, the operability of the system will be achieved, where the data from the Hall sensor will control the position of the servomotor on the second Secondary board.

Conclusion

I really enjoyed this week. The knowledge gained this week allows me to integrate external devices and expand the functionality of the system by connecting additional devices that operate using the I2C protocol.

Files

First connection - RAR archive

Three boards - RAR archive


Last update: June 23, 2023