0% found this document useful (0 votes)
3 views9 pages

Serial Monitor

This tutorial explains how to use the Arduino Serial.print() and Serial.println() functions to print various data types to the serial port. It covers the differences between the two functions, how to use the Arduino Serial Monitor for debugging, and provides practical examples for printing strings, numeric variables, and floating-point numbers. The tutorial also includes code examples demonstrating the use of these functions in an Arduino project.

Uploaded by

Y. devesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Serial Monitor

This tutorial explains how to use the Arduino Serial.print() and Serial.println() functions to print various data types to the serial port. It covers the differences between the two functions, how to use the Arduino Serial Monitor for debugging, and provides practical examples for printing strings, numeric variables, and floating-point numbers. The tutorial also includes code examples demonstrating the use of these functions in an Arduino project.

Uploaded by

Y. devesh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

In this tutorial, you’ll learn how to use the Arduino [Link]() & [Link]() Functions.

We’ll
discuss how the Arduino [Link]() & [Link]() functions work and how to use them to print
various data types to the serial port.

We’ll also discuss how to use Arduino’s Serial Monitor For Debugging your Arduino projects. Without
further ado, let’s get right into it!

Table of Contents

1. Arduino [Link]() Function

2. Arduino [Link]() Function

3. Arduino Serial Monitor

4. Arduino Serial Print String Example

5. Arduino Serial Print String & Variable Example

6. Arduino Serial Print Float & Double Example

7. Arduino Serial Print Hex & Binary Example

8. Wrap Up

Arduino [Link]() Function

The [Link]() function is used to send data as human-readable text over the UART interface. It
accepts various data types such as integers, floating-point numbers, strings, and characters. It allows you
to display information, debug messages, or sensor readings in a readable format.
The [Link]() function does not append a newline character at the end.

1 [Link](val);

This function can also take another optional argument that specifies the format.

1 [Link](val, format);

Parameters

val: the value to print. Allowed data types: any data type.

format: format options are BIN(binary, or base 2), OCT(octal, or base 8), DEC(decimal, or base
10), HEX(hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of
decimal places to use. For example:

 [Link](78, BIN) gives “1001110”

 [Link](78, OCT) gives “116”

 [Link](78, DEC) gives “78”

 [Link](78, HEX) gives “4E”


 [Link](1.23456, 0) gives “1”

 [Link](1.23456, 2) gives “1.23”

 [Link](1.23456, 4) gives “1.2346”

Returns

[Link]() returns the number of bytes written, though reading that number is optional. Data
type: size_t.

Arduino [Link]() Function

The [Link]() function is similar to the [Link]() function with only one difference which is
following the printed text with a carriage return character (ASCII 13, or ‘\r’) and a newline character
(ASCII 10, or ‘\n’). So it basically, sends whatever text you want with a newline termination special
character.

Syntax

1 [Link](val);

Everything else is exactly the same as the [Link]() function that we’ve discussed before.

Arduino Serial Monitor

The Arduino IDE is equipped with a graphical interface tool called “Serial Monitor” that you can use to
communicate with your Arduino board over the serial port (UART). You just need to set the baud rate to
the same value you’re using in your Arduino project and you’re good to go. The received text from the
Arduino board will show up in the serial monitor and you can also send text messages over UART using
this graphical interface.
The Arduino Serial Monitor saves you the time and effort to use external serial terminals on your PC. And
it’s really helpful for debugging your Arduino projects by sending the value of variables and flags to check
them in real time using the serial monitor.

In the next sections, we’ll do some practical examples to test


the [Link]() and [Link]() functions using the Arduino Serial Monitor to achieve the following
tasks:

 Serial Print a string text message

 Serial Print string text & numeric variables

 Serial Print Float & Double variables

 Serial Print Hex & Binary format variables

So, let’s get started with the Arduino serial print code examples!

Arduino Serial Print String Example

This is a simple Arduino example code to print the message “ Hello World!!” over the serial port and
we’ll view it on the serial monitor.

Code Example
Here is the full code listing for this example.

1 /*

2 * LAB Name: Arduino Serial Print String (Text)

3 * Author: Khaled Magdy

4 * For More Info Visit: [Link]

5 */

6 void setup() {

7 [Link](9600);

8 [Link]("Hello World!!");

9 }

10 void loop(){

11

12 }

Code Explanation

setup()

in the setup() function, we’ll initialize the UART module & set its baud rate to 9600 bps using
the [Link]() function. And we’ll also print the string of text “ Hello World!!” which is the message
that we’d like to display only once, that’s why we’re sending it in the setup() function not in
the loop() function.

1 [Link](9600);

2 [Link]("Hello World!!");

loop()

in the loop() function, nothing needs to be done.

Testing Results

Here is the result of testing this project on my Arduino UNO board. The text message is showing up on
the Arduino serial monitor as expected. If you didn’t catch the message, you can restart your Arduino
board using the reset button to resend the message again while it’s booting up. Because the message is
sent only once during initialization (in the setup function).
Arduino Serial Print String & Variable Example

In this example, we’ll send a string text message alongside a numeric variable on the same line. We’ll
create a counter variable and print its value and keep incrementing it and send the data over UART every
250ms.

Code Example

Here is the full code listing for this example.

1 /*

2 * LAB Name: Arduino Serial Print String + Variable

3 * Author: Khaled Magdy

4 * For More Info Visit: [Link]

5 */

6 int counter = 0;

8 void setup() {

9 [Link](9600);

10 }

11 void loop(){
12 [Link]("Counter = ");

13 [Link](counter++);

14 delay(250);

15 }

Code Explanation

We’ll create a counter variable with an initial value of 0.

1 int counter = 0;

setup()

in the setup() function, we’ll initialize the UART module & set its baud rate to 9600 bps using
the [Link]() function.

1 [Link](9600);

loop()

in the loop() function, we’ll print the string of text “ Counter =” which is the message that we’d like to
display before the numeric value of the counter variable itself. Then we’ll use the [Link]() function
to print the counter value with a line termination character, increment the counter, insert a small delay,
and keep repeating.

1 [Link]("Counter = ");

2 [Link](counter++);

3 delay(250);

Testing Results

Here is the result of testing this project on my Arduino UNO board. The text message + numbers are
showing up on the Arduino serial monitor as expected.
Arduino Serial Print Float & Double Example

In this example, we’ll send a float and double variables with a different number of precision digits (after
the decimal point). The floating-point number is chosen to be pi (π).

The float data type has 7 digits of precision, while the double has 15 digits of precision (after the decimal
point).

Code Example

Here is the full code listing for this example.

1 /*

2 * LAB Name: Arduino Serial Print Float + Double

3 * Author: Khaled Magdy

4 * For More Info Visit: [Link]

5 */

6 double trouble = 3.141592653589793;

7 float boat = 3.1415927;

9 void setup() {

10 [Link](9600);
11

12 [Link]("Float = ");

13 [Link](boat);

14 [Link]("Float F.3 = ");

15 [Link](boat, 3);

16 [Link]("Float F.7 = ");

17 [Link](boat, 7);

18

19 [Link]("double = ");

20 [Link](trouble);

21 [Link]("double D.3 = ");

22 [Link](trouble, 3);

23 [Link]("double D.10 = ");

24 [Link](trouble, 10);

25 [Link]("double D.15 = ");

26 [Link](trouble, 15);

27 }

28 void loop(){

29 // Do Nothing!

30 }

Code Explanation

We’ll create a double variable and a float variable and save into them the pi (π) number.

1 double trouble = 3.141592653589793;

2 float boat = 3.1415927;

setup()

in the setup() function, we’ll initialize the UART module & set its baud rate to 9600 bps using
the [Link]() function. And we’ll send both the float and double variables. The first printed value
with the default precision, is followed by other print operations with a specific number of precision digits
for each.

Print the float variable with default precision, 3 digits, and 7 digits.
1 [Link]("Float = ");

2 [Link](boat);

3 [Link]("Float F.3 = ");

4 [Link](boat, 3);

5 [Link]("Float F.7 = ");

6 [Link](boat, 7);

Print the double variable with the default precision, 3 digits, 10 digits, and 15 digits.

1 [Link]("double = ");

2 [Link](trouble);

3 [Link]("double D.3 = ");

4 [Link](trouble, 3);

5 [Link]("double D.10 = ");

6 [Link](trouble, 10);

7 [Link]("double D.15 = ");

8 [Link](trouble, 15);

You might also like