Skip to content

13. Networking and communications

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

Hardware Serial

From the start of this week I had a pretty good idea of how I was going to have my boards communicate to one another. I am going to use serial communication to communicate between my sensor board from week 10 and my servo board from week 12. Just as a proof of concept that I could get those 2 board to talk, I decided to calculate a value based off the distance from the sensor board and set that as the delay between each step on the servo board. This means that as an object gets closer to the sensor, the delay between each step will decrease making the servo rotate faster. As the object goes farther away from the sensor, the delay will increase the servo will start slowing down.

Redesigning boards

On the boards that I designed in weeks 10 and 12, I had at most 4 pins (ground, power, UPDI, and Tx for the sensor board). Since I wanted the boards to communicate to each other via the Rx and Tx pins, I decided to add the entire FTDI output on the fab_new library. Since I wasn’t using the DTR pin on the end of the FTDI chip, I decided to make that the UPDI pin. I also had to change the ATtiny412 to an ATtiny1614 since the 412 didn’t have enough storage for the program I was attempting to run. The black wire is ground, the red is power, and the yellow is UPDI. These are the new time of flight sensors and servo board, respectively:

Setup

The setup I used for this week’s assignment is sending the distance values from my input board to my output board and doing some computations with those values. The original idea was to have that new, calculated value be the delay between each step of the servo, but for some reason that didn’t work as I explain further below.

This is what my setup looked like between the 2 boards and my computer (to display the values). The arrows represent the flow of the data going from the input board to the output board to an FTDI chip and then finally to my computer. I used a breadboard to have a common voltage and common ground between the boards.

Reading values from one board to another

The very fist thing I wanted to accomplish was just have the 2 boards talk to each other. I decided to do this by printing the values from the input board on the output board.

This is the same exact code as the one used in my Input devices week assignment except that this is just printing the sensor value without ” mm” at the end. That was added for that assignment for readability, but would hinder the other board from understanding the incoming values. Click here to download this code for yourself.

#include <Wire.h>
#include <VL53L1X.h>

VL53L1X sensor;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  Wire.setClock(400000);

  sensor.setTimeout(500);
  if (!sensor.init())
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }

  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(50000);
  sensor.startContinuous(50);

}

void loop()
{
  Serial.println(sensor.read());
  delay(100);
}

This is the code for the output board that receives the data. It just reads whatever is being detected in the serial port and then adds ” mm” at the end for readability. Click here to download the code for yourself.

#include <Wire.h>

int inputdata = 0; // for incoming serial data
int outputdelay = 0; // value that gets printed that will later be used for the servo delay
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    inputdata = Serial.parseInt();
    Serial.print("Distance: ");
    Serial.print(inputdata);
    Serial.println(" mm");
  }
  else {
    Serial.println("ERROR");
  }
  delay(100);
}

Making calculations based off that value

This is the code for the output board that receives the values from the input board and then calculates a value based off of that. Just like before, we are reading from serial using the function “Serial.parseInt()”. In the step before, we could have used “Serial.read()” because that is just reading values. For Serial.read(), whatever is being sent in is read and interpreted as a string. When we use the command Serial.parseInt(), the software expects a number. This means that we can perform computations such as adding, subtracting, multiplying, and dividing with the value. Click here to download this code for yourself. The code for the input board with the distance sensor does not change.

#include <Wire.h>;

int inputdata = 0;   // for incoming serial data
int outputdelay = 0; // value that gets printed that will later be used for the servo delay
void setup() {
  Serial.begin(9600);
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    inputdata = Serial.parseInt();
    outputdelay = inputdata / 40;
    Serial.print("Distance: ");
    Serial.print(inputdata);
    Serial.print(" mm --> servo delay: ");
    Serial.print(outputdelay);
    Serial.println(" ms");
  }
  else {
    Serial.println("ERROR");
  }
  delay(100);
}

This is the working communication with calculations between the 2 boards:

Attempting to change servo speed

I honestly have no idea why I was unable to change the servo speed. When I first used the servo in my output devices assignment, I was able to make the servo spin faster and slower depending on the delay between each step. When I attempted, to set that delay as the computed variable from the values coming in, it didn’t seem to work. I have troubleshooted in every way I can think but nothing seemed to work. I am sure it is not the board since I was able to upload and perform a simple servo test. I know it isn’t the servo itself because the servo worked during that test. I also know that the variable I am setting as the delay is a valid integer. I honestly have no idea what the issue here is.

Software Serial

The next thing I wanted to try was software serial. Hardware serial only has 2 pins total (Rx and Tx). Since I want to have multiple boards communicating to each other in my final project, I will need more than this 1 set of pins. I’d like to have a masterboard with a bunch of student boards. The student boards will be the distance sensors and the servo board.

Designing and making a master board

As I mentioned above, all I need on the board is a power pin (which I will turn into a common power), and ground pin (which will also be turned into common ground), UPDI (for uploading programs), and an LED (for testing). Since an ATtiny1614 has 14 pins in total, that leaves me with 10 pins that are available for serial communication. The only thing I had to keep in mind is that I obviously want the 2 pins for each of the devices to be side-by-side. That being said, this board was very simple to design and mill. Download the schematic here and the board file here

Software serial

Since I had the HC-05 bluetooth module connected to the built-in Rx and Tx pins, I had to use the software serial pins to receive values from one of my input boards. The input board I am using is the time of flight board I designed during Input Devices week.

Software serial

Since I had an FTDI chip connected to the Rx and Tx pins of the master board, I had to use the software serial pins to receive values from one of my input boards. The input board I am using is the time of flight board I designed during Input Devices week.

The code is relatively simple. I just have to start by including the software serial library and then assigning certain pins as the software serial pins. I did this by writing the line “SoftwareSerial input1(9, 10)”. This makes sets pin 9 as Rx and pin 10 as Tx. Then I have to start the hardware serial pins at a baud rate of 9600 and the software serial pins also at 9600 baud rate. In the “void loop()”, I print a counter to show which value is being printed, then spacing for readability, then the value from the distance sensor, ” mm” for units, and then I increase the counter by 1 for the next cycle.

#include <SoftwareSerial.h>

SoftwareSerial input1(9, 10);                       // sets pin 9 as Rx and pin 10 as Tx

int i = 0;

void setup() {
  Serial.begin(9600);                               // starts normal serial monitor at 9600 baud

  input1.begin(9600);                               // starts communication with input1 at 9600 baud
  Serial.println("Input1 has been initialized");
  Serial.println("");                               // adds a line of spacing for readability

}

void loop() {

  if (input1.available() > 0) {
    Serial.print(i);                                // prints counter
    Serial.print("      ");                         // space between counter and distance
    Serial.print(input1.parseInt());                // prints distance value from sensor
    Serial.println(" mm");                          // prints "mm"
    Serial.println("");                             // adds a line of spacing for readability
    i = i + 1;                                      // increases the counter by 1
  }
}

Files

Time of flight code

Receiving data

Delay calculation code

Group work

This week’s group assignment was to communicate between 2 distinct projects. The 2 projects that were used were the ones from Teddy Warner and Drew Griggs. The protocols that was used to communicate between the projects were hardware serial and software serial. To learn more about this week’s group work, go to this page