Making projects with Arduino can be fun, but sometimes they do not behave the way you wanted them to. In this 2 part tutorial, you will learn how to make finding problems(debugging) WAY easier for yourself using Serial. It will also allow your Arduinos to communicate with each other in a very easy manner with just 2 pins. Lets get started!
What is Serial?
Serial is something called a protocol, which can be understood as a technique used by the Arduino to talk to a PC or another Arduino. It allows for very simple and convenient two way communication, meaning both your Arduino and PC can send and receive data.
You can visualize it by thinking of two pipes parallel to each other, with each pipe only allowing items to flow in one direction and only allowing one item to flow at a time. The Arduino and your PC are on opposite ends of the 2 pipes. Here is how it will look-
Let’s say that that Arduino wants to say “hi” to the PC. It takes 2 papers and writes the letters h and i on them. It then folds the papers and then throws them towards the PC on the right pipe.
The PC then checks the pipe and finds the two pieces of paper. It grabs them(one by one) and unfolds them to read the message and finds a “hi”. It can then send a reply to the Arduino in the same way(but through the left pipe).
Note-If a series of characters are sent, they must be read sequentially. It is not possible to read them in random order. Taking the above example, the PC can not read the letter “i” before reading the letter “h”. If it wants to get to the “i”, it must read the letter “h”(slower) or completely discard it(faster).
Now that you have understood the basics of Serial, let us try it out. In this tutorial, we are going to make an Arduino say hi to your PC and then print it on the screen and in the next tutorial, we are going to make your PC send a reply to the Arduino.
Step 1: Preparation
I am assuming that you own an Arduino. If you don’t, then read my tutorial on getting started started with Arduino(it will really help). Apart from an Arduino, you will not need anything special for this tutorial(except for the USB cable of course!)
You must also have the Arduino IDE downloaded and installed on your PC/MAC and be familiar with Arduino programming. These are all the things you need and you are now ready to move on to the next step!
Step 2: Coding and uploading
In the first part of the tutorial, we are going to make the Arduino print something on your screen. In the second part, we are going to be kicking it up a notch by sending variable messages from a PC to the Arduino and then recieving messages accordingly. Let’s begin!
You need to start by connecting the Arduino to your PC and making sure that the COM port and board variant are correctly selected. Start by entering the following code in the setup.
This tells the Arduino to begin Serial communication with your PC. The number is the rate at which the individual “packets” or information are sent. In this case, 9600 tells the Arduino to send 1200 bytes or 1.2 KB of information per second. You can change this number, but for this test, this speed is WAY more than enough.
Next, enter the following code in the loop.
This tells the Arduino to print “hello” on the screen, wait for a second and then repeat the process again. The Arduino sends all the letters one by one. Your code should look like this-
Compile and upload your code.
You would have realized that there is nothing being printed on the screen. This is because to read the serial messages. You need to use the open the serial monitor. To do this you can either use the keyboard shortcut(ctrl+shift+m) or you can go to tools and select Serial monitor.
This will restart the Arduino and now you should be able to see your program running.
Step 3: Playing by Tweaking
Now that you have got the code to work, you can try to play around with the code and see how it changes the output. Try changing the Serial.println() to Serial.print(). This will result in everything getting printed on the same line.
You can also add escape sequences and see how they change the output.
Important things to remember with Serial
Serial makes it easy for you to debug your sketches because it gives you all information in a very convenient manner and does not leave you guessing, but their are still a few important things to remember while using it. The first thing you need to remember is that while using Serial you will not be able to use 2 pins on your Arduino. As for which ones these are, just search for pins which have RX and TX written next to them. These pins are used to send and receive data and if you are using Serial, you can not use these. If you do, then either the messages you are sending and receiving will get corrupted, or the components you are trying to control will not work.
You would have also realized that 2 LEDs keep on blinking when your Arduino sends a message to the PC. These are directly connected to the RX and TX lines and show the live flow of signals between your Arduino and PC.