Arduino prints to console
This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor.
Arduino using the serial monitor to print to the console
The Arduino IDE has a console at the bottom, but we cannot print anything on it. The console is only used to display information during code verification and compilation.
The console displays the memory usage of the code in bytes and errors when verifying or uploading the code. To print or display the values stored in the variables, we can use the serial monitor of the Arduino IDE.
We can launch the serial monitor using the Serial tab on the Arduino IDE toolbar Tools
. We can Serial.begin()
start serial at a specific baud rate or speed using the Serial function.
We can use the Serial.print()
and Serial.println()
functions to print text and variables on the serial monitor.
The difference between these two functions is that print()
the function prints the variable and the cursor remains on the same line, but in println()
case of the function the cursor moves to the next line after printing the variable.
For example, suppose we want to print two variables on the same line and the third variable on the next line. In this case, we would use print()
the function to print the first variable, then use println()
the function to print the second variable and move the cursor to the next line, then use print()
the function again to print the third variable.
If we only want to print the variable once on the serial monitor, we can use the setup()
print() function inside the Serial Monitor function Serial.print()
as it only runs once when the Arduino board is turned on.
If we want to print a variable continuously, we have to loop()
print it inside a function because the code inside the loop function runs in a loop whenever the Arduino is turned on. We can print variables of all data types using the serial monitor.
For example, let's define some variables and print them on the serial monitor. See the code below.
int My_Int = 10;
unsigned long My_TimeStamp = 1416803716;
const char *My_CharArray = {"This is My_CharArray"};
float My_Float = 3.14159266759;
void setup() {
Serial.begin(9600);
Serial.println(My_Int);
Serial.println(My_TimeStamp);
Serial.println(My_CharArray);
Serial.print(My_Float, 5); // prints to five places right of the decimal
}
void loop() {}
Note that we can only run the serial monitor when the Arduino board is connected with the Arduino IDE. We can also Serial.print()
define a second parameter in the function, which is the format in which the variable value will be printed.
For example, in case of integer or long data type, we can define the number system we want to display like binary BIN
and decimal DEC
. By default, numbers will be displayed in decimal number system.
For floating point numbers we can define the number of decimal places of the number to be printed on the serial monitor. Check this link for Serial.print()
more details about the function.
Serial.print()
The data in the function will be converted to ASCII representation. If we want to send data as a byte stream, we can use Serial.write()
the function instead of Serial.print()
the function.
Check this link for Serial.write()
more details on the function.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Stopping a loop in Arduino
Publish Date:2025/04/16 Views:150 Category:C++
-
This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi
Arduino mills() function
Publish Date:2025/04/16 Views:156 Category:C++
-
This tutorial will discuss millis() the use of functions in different applications in Arduino. This tutorial will also discuss some examples to better understand millis() the functions. millis() Checking the elapsed time in Arduino using th
Arduino 打印到控制台
Publish Date:2024/03/13 Views:253 Category:C++
-
本教程将讨论使用 Arduino IDE 的串行监视器在控制台上打印文本或变量。
在C中将整数转换为字符
Publish Date:2024/01/03 Views:135 Category:C语言
-
本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。
如何在 C++ 中实现毫秒级的睡眠
Publish Date:2024/01/02 Views:238 Category:C++
-
本文介绍了在 C++ 中使用不同方法暂停程序执行,实现睡眠的方法。本文介绍了在 C++ 中睡眠毫秒的方法。使用 std::this_thread::sleep_for 方法在 C++ 中睡眠
如何在 C++ 中将双精度数四舍五入到整数上
Publish Date:2024/01/02 Views:156 Category:C++
-
本文演示了如何在 C++ 中把双精度数四舍五入到整数中。本文将为大家讲解几种在 C++ 中如何将双精度数四舍五入为整数的方法。使用 round() 函数将双精度数四舍五入到整数
如何在 C++ 中以毫秒为单位获取时间
Publish Date:2024/01/02 Views:149 Category:C++
-
本文介绍了如何在 C++ 中获取以毫秒为单位的时间。本文将介绍多种 C++ 方法,介绍如何以毫秒为单位获取时间。
如何在 C++ 中把 Char 数组转换为 Int
Publish Date:2024/01/02 Views:143 Category:C++
-
本文演示了在 C++ 中把 char 数组转换为 int 类型的方法。本文将介绍将 char 数组转换为 int 类型的 C++ 方法。使用 std::strtol 函数将 char 数组转换为 int 类型
如何在 C++ 中将 ASCII 码转换为字符
Publish Date:2024/01/02 Views:526 Category:C++
-
本文介绍了在 C++ 中如何将 ASCII 值转换为 char 的方法。本文将演示关于如何在 C++ 中把 ASCII 值转换为字符的多种方法。在 C++ 中使用赋值运算符将 ASCII 值转换为字符