Arduino Serial Monitor
In this tutorial I will be going through on how to setup the Arduino serial monitor so you can debug and interact with a program running on the Arduino. This is pretty simple but it can be a bit confusing at first especially if you’re new to programing and the Arduino.

As part of this tutorial we will build a simple circuit to demonstrate how you can both receive and send commands over the serial monitor.


If you want to see my video on it, then be sure to check it out below. I go through all the steps to setting up the circuit, code and also being able to open and use the monitor window correctly.

Equipment

The equipment that I use in this simple Arduino serial monitor is listed below. You can easily learn everything without using the example circuit but I find doing is often the best way to fully understand how things work.

Arduino Uno

1x 100-ohm resistor

1x Red LED

Breadboard

Breadboard wire

The Circuit for the Example

The circuit that we will be building for this is super simple and will consist of an LED that we will be able to control from the serial monitor. This is purely just there to show both sending and receiving of data through the monitor.

To setup the circuit simply do the following. If you’re having trouble please refer to the circuit diagram.

  1. Simply connect the positive lead of the red LED up to pin 2.
  2. Next hook a 100-ohm resistor from the negative lead to the ground rail on the Arduino

Circuit Diagram

The Arduino Serial Monitor Code

The code we will be using is again very simple with its purpose is to show the basics of how you can use the Arduino Serial monitor to view and send data. I will explain each of the lines we’re using and how you can use it in your next project to debug or interact with the Arduino. If you just want to download the code straight away then you can download it form here.

To begin we initialise an integer variable for our LED, this is the pin number leading to the LED. We also initialize a variable called count, this will store the amount of time the LED is on or off.

int redLedPin = 2;
int count = 0;

In the setup function we first set the red LED to act as an output so we can turn it off and on.

We then call the serial.begin function with a parameter value of 9600. The value we’re setting is known as the bits per second (baud) rate. This can be set to quite a few different rates but we will use 9600 as this seems to be the default and will work with most devices without any issues. It is very important to call this otherwise you won’t be able to communicate with the Arduino.

Next we wait for the serial interface to initialise and connect to the computer/device. We then call a function called Serial.println(). This function allows us to print a line to the Arduino serial monitor. You will notice this will print the text and then start on a new line for the next input/output.

void setup() {
  pinMode(redLedPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Enter Y to turn on the LED:");
}

Lastly we have the loop, this will continually loop through until either a new program is uploaded or the Arduino is switched off.

Inside the loop function we have an if statement checking to see if there is data waiting in the serial buffer(Serial.Available). In simple terms we're checking to see if we have sent a command to it. If there is data, we enter the if statement.

Next we create a variable called ch and call Serial.read() which will get us the data currently waiting in the serial buffer. Keep in mind this function will only get us the first byte of the data that is incoming to the Arduino. If you want to get an entire string you will need to use something like Serial.readString.

If the byte of data is either y or n we then enter the relevant if statement. In here you will notice we change the output of the LED pin high or low. We then print out a series of statements. There are two things you should notice here. Serial.println will print the data and go to a new line whilst Serial.print will print the data but stay on the same line when outputting. You will also notice you can print variables such as the count example in the code below. Doing this will allow you to be able to debug values when it comes to data processing.

Lastly I delay by a second and add 1 to count. This is just showing you an example of adding data to a variable then printing it via the serial output.

void loop(){
  if (Serial.available()){
    char ch = Serial.read();
    if (ch == 'y' ||	ch == 'Y'){
      digitalWrite(redLedPin, HIGH);
      Serial.println("You have turned on the LED!!");
      Serial.print("The LED was off for ");
      Serial.print(count);
      Serial.println(" seconds");
      Serial.println("If you want to switch it off, simply enter N or n!");
      count = 0;
    }
    if (ch == 'n' ||	ch == 'N'){
      digitalWrite(redLedPin, LOW);
      Serial.println("You have turned off the LED!!");
      Serial.print("The LED was on for ");
      Serial.print(count);
      Serial.println(" seconds");
      Serial.println("If you want to switch it on, simply enter Y or y!");
      count = 0;
    }
  }
    delay(1000);
    count += 1;
}

Once you’re done you should be able to deploy the code onto the Arduino. In the next part I will show you how to connect, receive and send data to the Arduino.

Connecting, Receiving and Sending Data to the Arduino

Now that the code has been uploaded to the Arduino we will need to open up the Arduino serial monitor. This is pretty easy and there are only a few options that I will need to explain.

To open up the serial monitor go up to tools and then select the serial monitor. Alternatively, CTRL+SHIFT+M will also bring up the same window. Make sure you are hooked up to the Arduino otherwise the window won’t open up.

serial monitor menu location
You should now have a window open that looks similar to the one below.
At the top of this screen there should be an input area. This is where you can type and send text to the Arduino. To send the text that you have typed simply press the send button.

Below this is the output window, all the data that is sent to us will be displayed here. If you're using the example code above you will be able to see the text that is in the Serial.println functions.

At the bottom we have 3 different options. The first is pretty self-explanatory, auto scroll will disable and enable the automatically scrolling of the output box.

Second we have the line ending option. You can set the monitor to automatically append a line ending after what you enter/send to the Arduino.

Thirdly we have the baud rate that I mentioned above. Make sure this matches to what you have set in the code. If it is different than the output text will appear as gibberish. If you’re receiving gibberish, then this is a likely cause of this.
the monitor screen

This is basically everything you need to know to understand the basics of the serial monitor. There are a few more things you can learn about such as using strings via the serial interface and more but this probably all you need to know for now.

I hope you now understand how to use the Arduino serial monitor correctly and will help you in future Arduino projects. If I have missed something, got something wrong , you have feedback or anything else then feel to leave a comment below.

50440f720f551e2d72035e0049601fb7\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Subscribe & Get 21 Awesome Arduino Projects

Enter your email below to get instant access to 21 amazing Arduino projects.

Please check your inbox or junk mail for a confirmation email!