Networking and Communication

This week's assignment was to set up a liable communication between two microcontrollers. Since I had this assignment in mind when I was designing the PCB´s for the input and output week´s, I only had to program these two microcontrollers properly because I already had the communication connectors available on the PCB´s. Sounds really easy and fun, but sure it took me a long time to figure out what was really going on. Even now, they are things going on that I´m not really sure of. For starters, I had programmed each board separately and set it up to send some information via serial to the computer. From the serial monitor window I was seeing the right measurements from the sensor board and the right steps from the stepper board. All these where working smoothly separately, but when I was connecting everything together and checked for the outcome from the computer I was seeing a bunch of "random" values.

Sure I couldn´t know what to do with all this values and where they came from. These numbers where in the range of 0 and 65000, sometimes another range from 0 to 10, etc. Really weird and I was going step by step to eliminate the possible error. I haven´t really gone nowhere with this since both of the PCB´s where working accordingly separate, but where doing weird things connected. Because of these random values I haven´t connected motor to be moved and first I had to figure out some solution.

The first idea of what was going wrong it came to me when I changed the program to send me back the same information I just sent from the Serial Monitor window. When I was sending one character the device was sending me back 2, even 3 characters. These characters where always numbers, even when I was sending letters. Another thing I have noticed was the number of characters I was sending and receiving. When I was sending one character I was receiving back one line of numbers (2 or 3 characters), but when I was sending 2 or 3 characters I was receiving back a staggering 2 or 3 lines of numbers. Bingo! The communication was misunderstanding the characters on hand and was interpreting it as something else. After a long search, readings and talking with my colleagues I saw what I had wrong! The serial communication command, the command which was sending the information through serial, I had it as serial.print! This command was working well when I was receiving the information to the Serial Monitor window and that window was interpreting the information correctly, but when I was sending the same information to another microcontroller the misinterpretation was taking over. The solution was to use the serial.write command and like this force the interpretation of all the characters at once and not divided by each characters. After this, everything started to move accordingly. Since, from time to time, the sensor measurements where a little bit off I have programmed the stepper to move at a speed of 50 rpm and 10 steps at one time when the measured distance was more than 4cm (5 = 255 * 4 / 200). And stops moving when the measurements where less than 4 cm. Even so, the stepper does not move smoothly and has some kind of discontinuity. This discontinuity can came partly from the 100 milliseconds delay between each readings of the sensor and partly because of the off measurements from time to time.

The communication protocol is a simple Serial communication protocol (serial.write()), the same communication is used to read the sensor values to the Serial Monitor and the bridge board uses it to define the steps and speed of the stepper. The node board is kind of an emitter and both, PC and bridge board, uses that emitted information to either display or move the stepper.

Ultrasound Sensor Node

//include the serial communication library
#include < SoftwareSerial.h >
SoftwareSerial Serial_sw(0, 1); // rx, tx

//define all the needed variables and define the pin numbers
#define echoPin 9 // echo conencted to pb1
#define trigPin 10 // trigger connected to pb0

int duration;
byte distance;

//initializing the serial communication and pins for input and output
void setup()
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);


// the actual program loop
void loop()
digitalWrite(trigPin, LOW);

digitalWrite(trigPin, HIGH);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration / 58;



Stepper Motor Bridge

#include < SoftwareSerial.h >
#include < Stepper.h >

SoftwareSerial serial_sw(10, 9); //rx, tx

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 0, 1, 3, 4);

void setup() {

byte distance;

void loop() {

if (serial_sw.available() > 0) {

distance =;

int motorSpeed = map(distance, 0, 255, 0, 200);

if (motorSpeed > 5) {
} else if (motorSpeed <= 5) {