Printing Switch Values: Serial Monitor Guide

how to print switch value to serial monitor

The Serial Monitor is a powerful tool that allows you to communicate with your Arduino board and print data being sent and received. It is based on the Serial communication protocol and requires you to connect your Arduino to your computer via a USB cable. To open the Serial Monitor, you can press the circular icon in the top right corner of the Arduino IDE. If no Arduino is connected, you will see an alert message, and you will need to connect your Arduino via a USB cable to your computer.

To communicate with the Arduino, you need to set the correct Port by going to Tools -> Port: COM3 -> COM3 and selecting the available Serial Port. If there are multiple options, you can test the communication with a simple program that prints Hello to the Serial Monitor. In the setup() function, we initialize the Serial communication with a baud rate of 9600. In the loop() function, we use the Serial.println() function to print the string Hello to the Serial Monitor and then wait for a second.

The Serial Monitor provides a user interface with various buttons and functions. You can toggle Auto scroll to automatically scroll up and see the latest message, toggle Timestamps to prefix each output with a timestamp, and Clear Output to delete all printed text. You can also specify the Line Ending, which is usually not necessary unless you are using the Message field. It is important to ensure that the baud rate set in the Serial Monitor matches the one specified via Serial.begin() in the setup() function.

When printing data to the Serial Monitor, you can use either Serial.print() or Serial.println(). The only difference is that Serial.println() adds a new line character at the end of each message, while Serial.print() does not. For example, Serial.print(1); Serial.print(2); Serial.print(3); will output 123 on the same line, while Serial.println(1); Serial.println(2); Serial.println(3); will output:

1

2

3

You can print single characters, text with multiple characters, and numbers with or without these functions. For instance, Serial.println('x'); will print the character 'x', while Serial.println(Some text); will print the text Some text. It is important to use double quotes for text with multiple characters, as single quotes will result in garbled characters.

You can also print variables of different data types, and Arduino's Serial Monitor will interpret the data correctly. For example, int sensorValue = 42; Serial.print(Sensor value: ); Serial.println(sensorValue); will print the text Sensor value: followed by the value of sensorValue.

In addition to printing, the Serial Monitor allows you to write and read serial data. The Serial library functions Serial.write() and Serial.read() enable this two-way communication.

shundigital

How to open the Serial Monitor

To open the Serial Monitor, simply click the Serial Monitor icon. The icon is located to the right of the other icons in Arduino 0023 and below. In Arduino 1.0 and beyond, the icon is located to the far right.

Selecting which port to open in the Serial Monitor is the same as selecting a port for uploading Arduino code. Go to Tools -> Serial Port, and select the correct port. Once open, you can alter the baud rate, enter key emulation, and autoscroll settings.

The Serial Monitor is a great quick and easy way to establish a serial connection with your Arduino. If you're already working in the Arduino IDE, there's no need to open up a separate terminal to display data.

shundigital

How to test the communication

To test the communication between an Arduino and a computer, you can use the Arduino IDE's Serial Monitor. This tool allows you to display and send serial data, which can be useful for debugging.

  • Connect your Arduino to your computer: Use a USB cable to connect your Arduino board to your computer.
  • Open the Arduino IDE: Launch the Arduino Integrated Development Environment (IDE) on your computer.
  • Select the correct board and port: In the Arduino IDE, go to "Tools" and select the appropriate board type and serial port that your Arduino is connected to.
  • Upload a test sketch: Open a simple sketch, such as the Blink example sketch, and upload it to your Arduino.
  • Open the Serial Monitor: Click on the Serial Monitor icon in the Arduino IDE to open the Serial Monitor window.
  • Set the baud rate: Ensure that the baud rate in the Serial Monitor matches the baud rate specified in your sketch.
  • Observe the output: If your sketch is running correctly, you should see the output from your sketch displayed in the Serial Monitor. For example, if you uploaded the Blink sketch, you should see messages indicating the state of the LED (on or off).
  • Send data to the Arduino: You can also send data from the Serial Monitor to the Arduino by entering text in the text box and clicking the "Send" button. Try sending different commands or data to your Arduino and observe its response.
  • Analyze the communication: Examine the data being sent between the Arduino and the computer. Ensure that the data is as expected and that there are no errors or unexpected behaviour.
  • Test with different sketches: Repeat the process with different sketches to test various functionalities of your Arduino and its communication with the computer.

By following these steps, you can effectively test the communication between your Arduino and the computer, ensuring that data is being transmitted and received correctly.

shundigital

User Interface of the Serial Monitor

The Serial Monitor is a window split into two sections. The top section displays the monitoring mode, port, baud rate, line ending, and a stop monitoring button. The bottom section is where messages are displayed.

The Serial Monitor allows users to configure, monitor, and communicate with serial ports. Users can monitor and stop monitoring data coming from the port. They can also send data to a serial port by entering text into the text field at the bottom of the view and using the Enter key or the Send Message arrow button. The Serial Monitor also allows users to clear the incoming data text field and send preset control signals.

The Serial Monitor provides a range of configurable settings, including:

  • Ports that are actively connected to a device
  • Serial port-compatible devices connected to the machine
  • Frequency at which the monitor attempts to communicate with the connected device
  • Line ending to use in messages sent to the connected device
  • Whether to add timestamps to the output of data received from the connected port
  • Whether to auto-scroll new content that comes from the connected port
  • Automatic reconnection
  • Type of encoding for messages sent to the serial port
  • Number of data bits, stop bits, and parity used for the serial port connection
  • Ability to log output to a file

The Serial Monitor can be used to get a user's input. The first step is to prompt the user for information, such as a question or menu options. The user can enter int, float, or string data types. The next step is to use the Serial.available() function in an empty while loop to wait for the user's input. Finally, the information entered by the user needs to be parsed and an action needs to be performed based on that input.

shundigital

Serial.print() vs Serial.println()

Serial.print() and Serial.println() are both used to print text and data to the serial port. The difference between the two is that Serial.print() prints whatever is sent in without moving the cursor to a new line, whereas Serial.println() prints whatever is sent in and then moves the cursor to a new line.

Serial.print("Test"); Serial.print("Words");

The output of the above code will be:

TestWords

Here, the cursor remains where it is after printing "TestWords".

Now, if we use Serial.println() instead:

Serial.println("Test"); Serial.println("Words");

The output of the above code will be:

Test

Words

Here, after printing "Test", the cursor moves to a new line and then "Words" is printed.

You can also print multiple statements and then follow them with println() to move the cursor to a new line:

Serial.print("These "); Serial.print("Test"); Serial.println(" Words.");

The output of the above code will be:

These Test Words.

shundigital

Printing Text and Numbers

To print plain text on the Serial Monitor, the `Serial.print() function can be used. The `Serial.print()` function displays text and numbers from your sketch on a PC or Mac via a serial link.

To use the `Serial.print()` function, `Serial` needs to be initialized first (in the setup preferably). A typical implementation is shown below −

Arduino

Void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

}

Void loop() {

// put your main code here, to run repeatedly:

Serial.print("Hello!");

Delay(100);

}

Note that the argument of `Serial.begin()> indicates the baud rate. You need to set the baud rate of your serial monitor to this value in order to read the printed messages properly. Often, the difference between the baud rate at which your microcontroller is transmitting and the baud rate at which the Serial Monitor is listening, leads to garbage values getting printed on the Serial Monitor.

The output of the above program, on the serial monitor, will be −

Hello!Hello!Hello!Hello!

If you want to end the line after printing your message (or, in other words, make sure that the next message gets printed on a new line), you can use the `Serial.println() function instead of `Serial.print()`.

If you replace `Serial.print()` with `Serial.println()` in the above program, the output will be −

Hello!

Hello!

Hello!

Hello!

The `sprintf()` function is another way to print text and variables to the Serial Monitor. It is useful when you are looking for a more efficient way to combine text and variables into a string for output.

Arduino

Serial.print("The ");

Serial.print(numBurritos);

Serial.print(" burritos are ");

Serial.print(tempStr);

Serial.println(" degrees F");

In this example, there are five lines of code to print a single line of text. This can be done in just three lines using `sprintf()`:

Arduino

Char buffer [40];

Sprintf(buffer, "The %d burritos are %s degrees F", numBurritos, tempStr);

Serial.println(buffer);

The first line creates a character array to save the output string. The second line uses the `sprintf()` function to combine the text and variables into a string. The final line tells `Serial.print()` to display the formatted string.

The character array needs to be as large, or larger than the final output string. The `sprintf()` function requires a minimum of two arguments: the first argument is where you plan to store the string, and the second argument is the string you want to create, including any format specifiers where you want to insert your variables.

  • `d` or `i` – signed decimal integer
  • `u` – unsigned decimal integer
  • `s` – a string of characters

For example, if you use `%d`, you’re telling `sprintf()` to format the inserted variable as a signed decimal integer.

Frequently asked questions

To open the Serial Monitor, go to the Arduino IDE and click on the magnifying glass icon located on the top right corner of the window. Alternatively, you can use the shortcut Ctrl + Shift + M.

If you see garbled characters, it is usually a sign that correct communication could not be established due to a mismatch in baud rates. Ensure that the baud rate in Serial.begin() matches the one selected in the Serial monitor. The most common baud rate is 9600.

Also, make sure you are using double quotes (") and not single quotes (') when printing text strings.

By default, the baud rate of the Serial Monitor is set to 9600. To change it, you can use the Serial.begin() function in your Arduino code. For example, if you want to set the baud rate to 115200, you can use Serial.begin(115200). Remember to set the correct baud rate in both the Arduino code and the Serial Monitor.

The Serial.print() function prints data to the Serial Monitor without adding a new line character at the end. On the other hand, the Serial.println() function adds a new line character after printing the data, which moves the cursor to the next line.

To print variables, use the Serial.print() or Serial.println() functions along with the variable name. For example, if you have an integer variable called sensorValue, you can print its value using Serial.print(sensorValue).

Yes, you can format the data before printing it to the Serial Monitor. You can specify the number of decimal places for floating-point numbers or add leading zeros to integers. This can be done using the Serial.print() or Serial.println() functions with appropriate formatting syntax.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment