Arduino Lesson #9 – serial communication

Via the serial interface of an Arduino, you can send and receive data. In this tutorial, I want to explain how to send & evaluate data via the serial communication pins RX & TX to another Arduino.

Arduino Lesson #9 – serial communication
Arduino Lesson #9 – serial communication

Parts list

For this tutorial, I use the following components:

Structure & Circuit

The two Arduino UNOs are cross-connected via the RX & TX pins respectively, i.e. RX goes to TX and TX goes to RX.

circuit two Arduino UNO connected via serial pins
circuit two Arduino UNO connected via serial pins

But please note if you want to upload some code to these devices you have to remove the wires between these devices. The pins RXD & TXD are used by USB-Serial converter and can also be used to program any other device like an Attiny85 microchip.

Programming

In the following, I want to show how to send data from one Arduino to another. For this purpose, the “Funduino” is the receiving unit and the “China clone” is the sending unit.

Arduino UNO and Fundiono UNO connected via serial pins
Arduino UNO and Fundiono UNO connected via serial pins

Program – Transmitter

The Transmitter would send data if the device will be restart with the reset button in the upper right corner of this device.

void setup() {
  //begin of serial communication with 9600 baud
  Serial.begin(9600);
  //send text "Hello World!" via serial port
  Serial.println("Hello World!");
}
void loop() {
  //empty
}

Program – Receiver

The Receiver will only listen at the serial interface to wait for data. If data available so the complete data will be stored into a variable and print out to the serial interface of the receiver.

void setup() {
  //begin of serial communication with 9600 baud
  Serial.begin(9600);
}
void loop() {
  //if serial data available, then...
  if (Serial.available() > 0) {
    //read the complete data into a variable
    String data = Serial.readString();
    //print data to serial interface
    Serial.print(data);
  }
}

Video

Leave a Reply

Your email address will not be published. Required fields are marked *