Serial – The basics

Arduino, Home Automation, Robotics1 Comment

You Are Here:, Home Automation, RoboticsSerial – The basics

The Serial is the default communication protocol of Arduino. This means its via Serial that data transmits.

For example, when you upload your code to your Arduino, it is via Serial (with a USB cable).

You can check data transmission in real-time with TX and RX LEDs.

  • TX blinking => Arduino is transmitting data.
  • RX blinking => Arduino is receiving data.

You can try to upload your code to the Arduino and watch for the RX led to blink.

In this tutorial you will learn how to use the serial monitor that will allow us to communicate with the Arduino in realtime.

What is a Serial monitor ?

Open the Arduino IDE and click on the magnifying glass on the top right corner. (An Arduino must be plugged)

A window like this appears :

In the red box : That’s an entry, this means that you can type whatever you want and send it to the Arduino. But if you did not programmed how your Arduino will react, nothing will happen.

In the blue box : It’s an output, you can code your Arduino so that it can prints data as output, pretty great for debugging.

In the yellow box : it’s a drop down menu where you can the transmission rate in bits per seconds. 9600 bauds is a norm which is great speed.

Programmation side

Actually pretty easy to do, all you have to do is to add a line of code in the function void setup() to indicate that you want to use serial. You have to specify the data speed.

Just like that :

void setup() {
Serial.begin(9600);
}

void loop() {

}

Serial.begin(9600) indicates that we are initiating serial communication.

Serial in “output mode”

Just below this line, write Serial.println("Hello World!");

Upload it to the Arduino and open the serial monitor, you should see this :

Arduino is writing in the serial what you wanted it to write.

Like told above, you can use this to do debugging, for example :

You would like to retrieve the temperature of a room every 5 secs and write it in an Excel file. Just before coding something that will write in your Excel file, you need to ensure that the Arduino is getting the right data. To do so, you just have to write Serial.println(yourValue); when you have your data ready.

NB : I use Serial.println(); but I could also use Serial.print(); the difference is that Serial.println(); skip a line.

Serial in “input” mode

We can communicate with the Arduino in real time thanks to the serial. Here is how to proceed :

Take the code we’ve been writing before and add this :

void setup() {
  Serial.begin(9600);

}

void loop() {

  while (Serial.available() > 0) {
    
    if (Serial.read()-'0' == 1) {
      Serial.print("You write 1 !");
      
    }
    
  }

}

Upload this and open the serial monitor. Type 1 in the monitor and send it. You should receive the message “You write 1 !”.

How it’s working ?

while (Serial.available() > 0) is a loop you go through only if Arduino is receiving serial data (0 = nothing, so if it’s > 0 there is data, obviously).

if(Serial.read()-'0' == 1) is a condition which is true if the data received in the serial is 1. (We subtract ‘0’ from Serial.read() to have data readable for human).

The result is not awesome but now, you know how to interact with the Arduino. This can be really important to make your project easier to develop. Moreover you can program events for your Arduino to react with.

 

We are using here the serial monitor to send data to the Arduino, but like said earlier, Arduino can communicate with other devices and can control (or being controlled) by those devices ! For example, Raspberry Pi and Arduino connexions are frequent and really useful in home automation, robotics… Moreover, the thumbail of this tutorial is a LCD screen, we can display informations on it through serial !

About the author:

One thought on “Serial – The basics

Leave a Reply to communication tips Cancel reply

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

Top