Arduino Serial.print ( )

The serial.print ( ) in Arduino prints the data to the serial port. The printed data is stored in the ASCII (American Standard Code for Information Interchange) format, which is a human-readable text.

Each digit of a number is printed using the ASCII characters.

The printed data will be visible in the serial monitor, which is present on the right corner on the toolbar.

The Serial.print( ) is declared in two formats, which are shown below:

  • print( value )
  • print( value, format)

Note: In Serial.print( ), S must be written in uppercase.

Arduino Serial.print ( )

Where,

serial: It signifies the serial port object.

print: The print ( ) returns the specified number of bytes written.

value: It signifies the value to print, which includes any data type value.

format: It consists of number base, such as OCT (Octal), BIN (Binary), HEX (Hexadecimal), etc. for the integral data types. It also specifies the number of decimal places.

Serial.print( value )

The serial.print ( ) accepts the number using the ASCII character per digit and value upto two decimal places for floating point numbers.

Example 1:

Output:

15.45

It sends bytes to the printer as a single character. In Arduino, the strings and characters using the Serial.print( ) are sent as it is.

Example 2:

Output:

"Hello Arduino"

Serial.print( value, format )

It specifies the base format and gives the output according to the specified format. It includes the formats Octal -OCT (base 8), Binary-BIN (base 2), Decimal-DEC (base 10), and Hexadecimal-HEX (base 16).

Let's understand by few examples.

Example 1:

Output:

11001

It converts the decimal number 25 to binary number 11001.

Example 2:

Output:

3A

It converts the decimal number 58 to hexadecimal number 3A.

Example 3:

Output:

72

It converts the decimal number 58 to octal number 72.

Example 4:

Output:

25

The conversion is from decimal to decimal. So, the output will be the same.

Flash Memory based strings

If we want to pass the flash memory in Serial.print ( ) based on string, we need to wrap the function statement with F.

For example,

Serial.print( F ( "Hello Arduino") ).

Printing a Tab space

We can also print the tab in the output.

Let's consider the code below:

Here, Serial.print(" \ t '') is used to print the tab in the output program.

Serial.println ( )

The Serial.println ( ) means print line, which sends the string followed by the carriage return ('\r' or ASCII 13) and newline ('\n' or ASCII 10) characters. It has a similar effect as pressing the Enter or Return key on the keyboard when typing with the Text Editor.

The Serial.println( ) is also declared in two formats, which are shown below:

  • println( value )
  • println( value, format)

What is the difference between Serial.print( ) and Serial.println( )?

The text written inside the open and closed parentheses in the Serial.println( ) moves in a new line. With the help of Serial.print( ) and Serial.println( ), we can figure the order and execution of certain things in our code.

Let's understand with an example:

Consider the below code.

Click on the Upload button-> Serial monitor for the output.

In the output, the word Hello will appear followed by the word Arduino 1 second later. After 1.5 second, another line will be printed.

Output

Arduino Mega

The Arduino Mega board (type of Arduino board) has four serial ports. While programming the Mega, we need to declare all the four serial ports. The baud rates of all the four serial ports should be different.

It is shown below:






Latest Courses