Arduino Tutorial: Using Serial to Debug and Communicate(part B)

So, you have read part A of this tutorial(if you haven’t, you know you want to) where I explain how to debug with Arduino, in part B, I am going to be explaining how to do 2 way communication between your Arduino and PC which will allow you to not only debug your projects faster and more easily, but do a lot more interesting stuff. Let’s get started!

What is going to happen?

We are going to be using the same setup from the previous part of this tutorial, except we are going to be sending messages from your PC to the Arduino and then rving replies. I hope it sounds easy, because it is.

In this project, you can type either the digit 1 or 2 and the Arduino will send back the digit in one, i.e. one or two. If you send anything else, it will tell you that the input is invalid and that you need to send the appropriate digits.

Step 1: Preparation

I am going to assume that you have read the first part of this tutorial because the setup used here is going to be exactly the same. You need an Arduino which will be connected to your PC. Select the appropriate COM port and the correct type of Arduino and you will be ready to go. If you don’t know what any of this means, read my blog on getting started with Arduino. Purchase links for any of the parts you need will also be over there.

Step 2: Coding and Uploading

Now that you have gathered the required parts, connected them and prepared everything, we can move on to my favorite part, writing and uploading the code. Don’t get overwhelmed by it, I will explain it step by step.

You should already know the first piece of code. Start by adding this to your setup-

It tells the Arduino to begin Serial communication with your PC at 115200 bits or 14400 bytes per second. This is like telling the PC and Arduino that they need to start talking to each other.

Next, write the following code in the loop.

We first make a variable called c, to store the characters being sent by the PC to the Arduino. The first if statement(if(Serial.available > 0)) checks if there is any incoming data. If you want to visualize it like in the previous part of this blog, think of it like the Arduino peeking in the pipe to see if the PC has sent any letters.

Then we read the the incoming data, convert it to a char and put it in c. If the character in c is 1 or 2, it is printed in words, otherwise the user is prompted to send an appropriate character. This sketch may seem fine, but there is a problem. Compile and upload this to your Arduino and try it out before reading on. SPOILERS AHEAD!

Did you figure it out? The sketch prints the digit in words but prints the prompt after it anyways. If you tried to send an invalid character, it prints the prompt twice and if you simply press enter, it prints the prompt again, but only once this time.

After Sending 1
After sending 4

You might be asking what causes this. It is caused by something called carriage return. This character is not printable on the screen so you have to use something called escape sequences to use it. An escape sequence is just a special code which starts with a backslash. The escape sequence for carriage return is ‘\r’. Carriage return is passed whenever you hit send or press enter in the Serial monitor. When you send a ‘1’ or ‘2’, you are actually sending a digit and carriage return. This is why the prompt appears after the digit is printed in words, because after checking for the digit, it also checks the carriage return and detects that it is an invalid input. When you send an invalid character, the prompt is printed twice, once for the character and then for the carriage return. If you don’t type anything in the text box but still click send/hit enter, then only carriage return is sent which is not a digit, so the prompt is printed once more.

Step 3: Playing by Tweaking

After you have got the code to work, it is time to play around and make some changes of your own. You can start by trying to send multiple digits at a time instead of just a single digit and see what happens. Maybe try to make the code work for all the digits between 0 and 9. Perhaps try to stop the prompt from being printed for the carriage return. You can do whatever you want. Now that you have learnt how to use Serial, you can do all sorts of things like sensors, advanced actuators and A LOT MORE!

Additional Resources

  1. Information about Carriage return and newline
  2. Previous part (Part A)

1 Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s