Arduino Basic Tutorial

Arduino Basic Tutorial: Getting Started with Arduino Serial Monitor

Description:

The Arduino Serial Monitor is a powerful tool that allows you to communicate with your Arduino board. This tool is built into the Arduino Integrated Development Environment (IDE) and provides a convenient way to debug and troubleshoot your Arduino projects. With the Serial Monitor, you can view data sent from your Arduino board, send data to your board, and even control your board from your computer.




How to Access the Arduino Serial Monitor

To get to the Serial Monitor, just open the Arduino IDE and click on the Serial Monitor icon at the top right corner of the screen.

accessing Arduino Serial Monitor

Alternatively, you can use the shortcut key “Ctrl+Shift+M” on Windows or “Command+Shift+M” on a Mac to open the Serial Monitor. Once the Serial Monitor is open, you can configure the settings for communication with your Arduino board.

Using the Arduino Serial Monitor

The Serial Monitor provides two main features: reading data from the Arduino board and sending data to the board. To read data from the board, you must first configure the Serial Monitor to match the settings in your Arduino sketch. This includes setting the correct baud rate, data bits, parity, and stop bits. Once the settings are configured, you can begin reading data from the board by selecting the “Serial” option in your Arduino sketch.

To send data to the board, you can use the “Serial.print” or “Serial.write” commands in your sketch. These commands send data through the USB cable to the Serial Monitor, which displays the data in the text area. You can also use the Serial Monitor to send data back to your board using the text input field at the top of the Serial Monitor window.



Main functions of Arduino Serial Monitor:

Output Console

The output console in the Arduino Serial Monitor is a text area that displays data sent from the Arduino board to the computer. This data can include sensor readings, status updates, and other information about the operation of the Arduino board.

When the Serial Monitor is opened, it will display a blank text area until data is sent from the Arduino board. Once data is received, the text area will display the data in real-time. The data is displayed in a continuous stream, with each line of data appearing below the previous line.

The output console in the Arduino Serial Monitor can be used to debug and troubleshoot Arduino projects. For example, if you are working with a sensor that is not providing accurate readings, you can use the Serial Monitor to view the raw data being sent from the sensor to the Arduino board. This can help you identify issues with the sensor or with the way the data is being processed in your sketch.

output console window in Arduino Serial Monitor output console



Auto-scroll

The “Auto-scroll” checkbox in the Arduino Serial Monitor is a feature that automatically scrolls the text area to the bottom of the console as new data is received. This feature can be very helpful when working with projects that generate a large amount of data or when you want to monitor real-time data from a sensor.

When the “Auto-scroll” checkbox is selected, the text area will continuously scroll down to the bottom of the console as new data is received. This means that the most recent data will always be visible at the bottom of the console, even if there is a large amount of older data above it.

If the “Auto-scroll” checkbox is not selected, the text area will not automatically scroll down as new data is received. This can be useful if you want to freeze the console to inspect a particular section of data, or if you want to compare data from different parts of your project.

Enabling the “Auto-scroll” feature in the Arduino Serial Monitor can make it easier to monitor the output of your project in real-time. This can be particularly useful when working with sensors or other devices that generate a continuous stream of data. By selecting the “Auto-scroll” checkbox, you can ensure that the most recent data is always visible in the text area, which can help you identify issues or anomalies in your project more quickly and easily.

auto scrolling option in Arduino Serial Monitor



Show Timestamp

The “Show Timestamp” checkbox in the Arduino Serial Monitor is a feature that allows you to display a timestamp for each line of data that is received from the Arduino board. This feature can be very helpful when working with projects that generate a large amount of data or when you need to track the time at which certain events occurred in your project.

When the “Show Timestamp” checkbox is selected, the text area will display the time at which each line of data was received from the Arduino board. The timestamp is typically displayed in a format that includes the date and time, such as “2023/04/03 14:30:00”. This makes it easy to track when events occurred in your project, such as when a sensor reading was taken or when a certain action was triggered.

If the “Show Timestamp” checkbox is not selected, the text area will not display a timestamp for each line of data. This can be useful if you do not need to track the timing of events in your project or if you want to save space in the text area.

Enabling the “Show Timestamp” feature in the Arduino Serial Monitor can make it easier to track the timing of events in your project and debug issues related to timing. By selecting the “Show Timestamp” checkbox, you can quickly identify when certain events occurred and correlate them with other data in your project. This can be particularly useful when working with complex projects or when debugging issues related to timing.
show timestamp chechbox in Arduino Serial Monitor

Clear Output

The “Clear Output” button in the Arduino Serial Monitor is a feature that allows you to clear the text area of the console. This feature can be very helpful when working with projects that generate a large amount of data or when you need to clear the console for a fresh start.

When the “Clear Output” button is clicked, the text area of the console will be cleared of all data. This means that all previous data, including any previous sensor readings, status updates, or other information, will be removed from the console.

If you click the “Clear Output” button accidentally, you can still retrieve the data that was displayed in the console before the button was clicked. This is because the Arduino Serial Monitor has a “Copy” button that can be used to copy the text area to the clipboard. This allows you to paste the data into a text editor or other application for further analysis or processing.

clear output button in Arduino Serial Monitor




Baud Rate

The “Baud Rate” in the Arduino Serial Monitor refers to the speed at which data is transmitted over the serial port between the Arduino board and the computer. It is a measure of the number of bits of data that can be transmitted per second.

The Baud Rate is typically set by the user in the Arduino IDE, and the same value must be set in the Serial Monitor for communication to be established. The Baud Rate is usually set to a standard value, such as 9600, 19200, or 115200 bits per second (bps).

The Baud Rate is an important setting because it determines the speed at which data can be transmitted between the Arduino board and the computer. If the Baud Rate is set too low, it can result in slow data transfer, while setting it too high can result in errors and data loss. Therefore, it is important to choose an appropriate Baud Rate for your project and set it correctly in the Arduino IDE and Serial Monitor.

In the Arduino IDE, the Baud Rate can be set using the “Serial.begin()” function, which initializes the serial communication between the Arduino board and the computer. The same value must be set in the Serial Monitor for the communication to be established correctly.

how to set baud rate in Arduino Serial Monitor

Textbox

The “Textbox” in the Arduino Serial Monitor is a text entry field located at the top of the Serial Monitor window. It is a feature that allows the user to send data from the computer to the Arduino board.

The textbox is particularly useful for projects that require the user to send commands or other data to the Arduino board in real-time. For example, if you are working on a project that involves controlling the brightness of an LED using a potentiometer connected to an analog input pin of the Arduino board, you can use the textbox to send the desired brightness level to the board.

To use the textbox in the Arduino Serial Monitor, simply type the data you want to send into the text entry field located at the top of the Serial Monitor window, and then press the “Send” button or the “Enter” key on your keyboard. The data will then be transmitted over the serial port to the Arduino board, where it can be processed and acted upon.

It is important to note that the textbox is only used for sending data from the computer to the Arduino board. To receive data from the Arduino board, you must use the Serial.print() or Serial.println() functions in your Arduino code, and the data will be displayed in the Serial Monitor’s text area.

textbox in Arduino Serial Monitor



Newline selection

The “Newline” selection in the Arduino Serial Monitor is a feature that allows the user to specify how line breaks are handled when sending data from the computer to the Arduino board. It is located in the drop-down menu next to the “Send” button in the textbox area of the Serial Monitor window.

When the “Newline” selection is set to “No line ending”, the Serial Monitor will not send any line breaks with the data entered in the textbox. This can be useful when sending data that does not require a line break, such as a single value or command.

When the “Newline” selection is set to “Newline”, the Serial Monitor will automatically append a newline character (also known as a line feed or “\n” character) to the end of the data entered in the textbox. This is useful when sending data that requires a line break, such as a sequence of values separated by commas.

When the “Newline” selection is set to “Carriage return”, the Serial Monitor will append a carriage return character (also known as a “\r” character) to the end of the data entered in the textbox. This is useful when working with devices that require a carriage return character to be sent after each command, such as some printers and other serial devices.

When the “Newline” selection is set to “Both NL & CR”, the Serial Monitor will append both a newline and a carriage return character to the end of the data entered in the textbox. This is useful when working with devices that require both characters to be sent after each command, or when working with protocols that require a specific line-ending sequence.

newline selection option in Arduino Serial Monitor



Send button

The “Send” button in the Arduino Serial Monitor is a button that allows the user to send data from the computer to the Arduino board over the serial port. It is located next to the textbox area in the Serial Monitor window.

To use the “Send” button, simply enter the data you want to send into the textbox, select the appropriate line-ending option from the “Newline” drop-down menu (if needed), and then click the “Send” button. The data will be transmitted over the serial port to the Arduino board, where it can be processed and acted upon by the Arduino sketch.

The “Send” button is particularly useful for projects that require the user to send commands or other data to the Arduino board in real-time. For example, if you are working on a project that involves controlling a motor connected to the Arduino board using a potentiometer, you can use the “Send” button to send the desired motor speed to the board.

It is important to note that the “Send” button is only used for sending data from the computer to the Arduino board. To receive data from the Arduino board, you must use the Serial.print() or Serial.println() functions in your Arduino code, and the data will be displayed in the Serial Monitor’s text area.

send button in Arduino Serial Monitor


Arduino Sending Data to Serial Monitor Examples:

Example1: Sending Hello Programming Digest every second to serial monitor

Arduino code that sends the message “Hello Programming Digest” to the Serial Monitor every second:

Code explanation:

  • void setup(): This is a function that is executed once when the Arduino board starts up. It is used to initialize any necessary settings and configurations.
  • begin(9600);: This line of code initializes serial communication between the Arduino board and the computer using a baud rate of 9600. The Serial.begin() function must be called before any other serial communication functions can be used.
  • void loop(): This is a function that runs continuously after the setup() function has completed. It is where you put your main code.
  • println(“Hello Programming Digest”);: This line of code prints the message “Hello Programming Digest” to the Serial Monitor. The Serial.println() function sends the message over the serial connection to the computer, where it can be viewed in the Serial Monitor.
  • delay(1000);: This line of code causes the Arduino board to pause for 1000 milliseconds, or 1 second, before executing the next line of code. This delay ensures that the message is only sent once every second.

To use this code, simply copy the code and upload it to your Arduino board and open the Serial Monitor. You should see the message “Hello Programming Digest” printed to the Serial Monitor every second.

how to send data to Arduino Serial Monitor every second




Example2: how to control a built-in LED on an Arduino board through serial communication

Output:

Type command 1 in the textbox and press the send button or press enter button on the keyboard

how to control built in led using Arduino Serial Monitor

After pressing the button it will turn on the LED

how to control built in led using Arduino Serial Monitor

Type command 0 in the textbox and press the send button or press enter button on the keyboard

how to control built in led using Arduino Serial Monitor

After pressing the button it will turn off the LED

how to control built in led using Arduino Serial Monitor



Code Explanation:

This code is an example of how to control a built-in LED on an Arduino board through serial communication with a connected device, such as a computer.

int ledPin = 13;

This line declares a variable ledPin as an integer and assigns it the value of 13, which is the pin number of the built-in LED on most Arduino boards.

void setup() {

This is the setup() function, which is called once when the board is powered on or reset.

Serial.begin(9600);

This line initializes serial communication with a baud rate of 9600. This means that the board will be able to communicate with a connected device (such as a computer) at a data transfer rate of 9600 bits per second.

pinMode(ledPin, OUTPUT);

This line sets the ledPin as an output, meaning that it will be able to send a voltage signal to the LED.

void loop() {

This is the loop() function, which runs continuously as long as the board is powered on or reset.

if (Serial.available() > 0) {

This line checks if there is data available on the serial port. If there is, it will continue with the following code block.

char command = Serial.read();

This line reads the incoming data from the serial port and assigns it to a character variable called command.

if (command == ‘1’) {

This line checks if the value of command is equal to ‘1’. If it is, the following code block will be executed.

digitalWrite(ledPin, HIGH);

This line sends a high voltage signal to the ledPin, turning on the LED.

Serial.println(“LED is ON”);

This line prints a message to the Serial Monitor saying that the LED is on.

else if (command == ‘0’) {

This line checks if the value of command is equal to ‘0’. If it is, the following code block will be executed.

digitalWrite(ledPin, LOW);

This line sends a low voltage signal to the ledPin, turning off the LED.

Serial.println(“LED is OFF”);

This line prints a message to the Serial Monitor saying that the LED is off.

So, essentially, this code waits for incoming data on the serial port, and if the data matches either ‘1’ or ‘0’, it will turn the LED on or off respectively, while also printing a message to the Serial Monitor.

Related Articles

Leave a Reply

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

Back to top button