Home Berytech Fab Lab Fab Academy

Networking and Communication

Serial Communication

To achieve a serial communication between two microcontrollers, we need to take care of the following:

  1. the wired connection between them
  2. the code used

In order to achieve serial communication between the boards, SDA and SCL pins have to be connected with each other while the GND is shared between the two boards. This is important to ensure serial communication!

With respect to the code, different codes will be used for each of the microcontrollers. One of the codes will make the first microcontroller act as a Sender of the data. So the board can be gathering data from a certain sensor and sending them to the other microcontroller using the serial terminal. On the other hand, the other board will carry the code that Recieves the data from the serial. In that case, the receiver could be using this data to control a certain output like an LED or a motor. In our case, we were simply reading these data to the serial monitor.

For the group assignment the boards of Wael and Ghassan used for the input and output week were used. The boards were connected using in our case digital pins that were set as software serial. So the test was gathering data from the ultrasonic sensor on the first board (sender), and sending this data to the other board(receiver), were we read the data from the serial monitor.

The following video represents how the distance was collected from the sender board, and sent to the receiver board, where we can see all details on the serial monitor.

Below, the Arduino codes of the sender and the receiver boards are illustrated:

//Sender Code #include <SoftwareSerial.h> SoftwareSerial mySerial(8, 9); int trigPin = 7; //Trig - green Jumper int echoPin = 6; //Echo - yellow Jumper long duration, cm, inches; void setup() { //Serial Port begin Serial.begin (9600); mySerial.begin(9600); //Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // The sensor is triggered by a HIGH pulse of 10 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the signal from the sensor: a HIGH pulse whose // duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); // convert the time into a distance cm = (duration/2) / 29.1; inches = (duration/2) / 74; Serial.println(cm); mySerial.println(cm); mySerial.println(" cm"); delay(250); } //Receiver Code //necessary library needed for the communication #include <SoftwareSerial.h> //pins used for the communication, wired respectively SoftwareSerial mySerial(8,9); void setup(){ mySerial.begin(9600); Serial.begin(9600); } void loop(){ //reading the data sent if (mySerial.available()>0){ Serial.write(mySerial.read()); } }

Downloads

Sender.ino | Receiver.ino