0% found this document useful (0 votes)
23 views

Serial Communication Arduino

The document provides reference information about various Serial communication functions in Arduino. It describes functions like Serial.println(), Serial.print(), Serial.read(), Serial.begin(), Serial.readString(), and Serial.parseInt(), explaining their syntax, parameters, returns, examples, and notes.

Uploaded by

Rod Supervlogs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Serial Communication Arduino

The document provides reference information about various Serial communication functions in Arduino. It describes functions like Serial.println(), Serial.print(), Serial.read(), Serial.begin(), Serial.readString(), and Serial.parseInt(), explaining their syntax, parameters, returns, examples, and notes.

Uploaded by

Rod Supervlogs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Reference > Language > Functions > Communication > Serial > Println

Serial.println()
Description

Prints data to the serial port as human-readable ASCII text followed by a


carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10,
or '\n'). This command takes the same forms as Serial.print().

Syntax

Serial.println(val)
Serial.println(val, format)

Parameters

Serial: serial port object. See the list of available serial ports for each board on
the Serial main page.
val: the value to print. Allowed data types: any data type.
format: specifies the number base (for integral data types) or number of
decimal places (for floating point types).

Returns

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

Example Code
/*
Analog input reads an analog input on analog in 0, prints the value
out.
created 24 March 2006
by Tom Igoe
*/
int analogValue = 0; // variable to hold the analog value

void setup() {
// open the serial port at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog input on pin 0:
analogValue = analogRead(0);

// print it out in many formats:


Serial.println(analogValue); // print as an ASCII-encoded
decimal
Serial.println(analogValue, DEC); // print as an ASCII-encoded
decimal
Serial.println(analogValue, HEX); // print as an ASCII-encoded
hexadecimal
Serial.println(analogValue, OCT); // print as an ASCII-encoded
octal
Serial.println(analogValue, BIN); // print as an ASCII-encoded
binary

// delay 10 milliseconds before the next reading:


delay(10);
}

Notes and Warnings

For information on the asyncronicity of Serial.println(), see the Notes and


Warnings section of the Serial.write() reference page .

Reference > Language > Functions > Communication > Serial > Print
Serial.print()
Description

Prints data to the serial port as human-readable ASCII text. This command
can take many forms. Numbers are printed using an ASCII character for each
digit. Floats are similarly printed as ASCII digits, defaulting to two decimal
places. Bytes are sent as a single character. Characters and strings are sent as
is. For example-

 Serial.print(78) gives "78"

 Serial.print(1.23456) gives "1.23"

 Serial.print('N') gives "N"

 Serial.print("Hello world.") gives "Hello world."

An optional second parameter specifies the base (format) to use; permitted


values 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-

 Serial.print(78, BIN) gives "1001110"

 Serial.print(78, OCT) gives "116"

 Serial.print(78, DEC) gives "78"

 Serial.print(78, HEX) gives "4E"

 Serial.print(1.23456, 0) gives "1"

 Serial.print(1.23456, 2) gives "1.23"

 Serial.print(1.23456, 4) gives "1.2346"

You can pass flash-memory based strings to Serial.print() by wrapping them


with F(). For example:
Serial.print(F("Hello World"))

To send data without conversion to its representation as characters,


use Serial.write().

Syntax

Serial.print(val)
Serial.print(val, format)

Parameters

Serial: serial port object. See the list of available serial ports for each board on
the Serial main page.
val: the value to print. Allowed data types: any data type.

Returns

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

Example Code
/*
Uses a for loop to print numbers in various formats.
*/
void setup() {
Serial.begin(9600); // open the serial port at 9600 bps:
}

void loop() {
// print labels
Serial.print("NO FORMAT"); // prints a label
Serial.print("\t"); // prints a tab

Serial.print("DEC");
Serial.print("\t");

Serial.print("HEX");
Serial.print("\t");
Serial.print("OCT");
Serial.print("\t");

Serial.print("BIN");
Serial.println(); // carriage return after the last label

for (int x = 0; x < 64; x++) { // only part of the ASCII chart,
change to suit
// print it out in many formats:
Serial.print(x); // print as an ASCII-encoded decimal - same
as "DEC"
Serial.print("\t\t"); // prints two tabs to accomodate the label
length

Serial.print(x, DEC); // print as an ASCII-encoded decimal


Serial.print("\t"); // prints a tab

Serial.print(x, HEX); // print as an ASCII-encoded hexadecimal


Serial.print("\t"); // prints a tab

Serial.print(x, OCT); // print as an ASCII-encoded octal


Serial.print("\t"); // prints a tab

Serial.println(x, BIN); // print as an ASCII-encoded binary


// then adds the carriage return with "println"
delay(200); // delay 200 milliseconds
}
Serial.println(); // prints another carriage return
}

Notes and Warnings

For information on the asyncronicity of Serial.print(), see the Notes and


Warnings section of the Serial.write() reference page .

Reference > Language > Functions > Communication > Serial > Read
Serial.read()
Description

Reads incoming serial data.

Serial.read() inherits from the Stream utility class.

Syntax

Serial.read()

Parameters

Serial: serial port object. See the list of available serial ports for each board on
the Serial main page.

Returns

The first byte of incoming serial data available (or -1 if no data is available).
Data type: int.

Example Code
int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:


Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}

Reference > Language > Functions > Communication > Serial > Begin
Serial.begin()
Description

Sets the data rate in bits per second (baud) for serial data transmission. For
communicating with Serial Monitor, make sure to use one of the baud rates
listed in the menu at the bottom right corner of its screen. You can, however,
specify other rates - for example, to communicate over pins 0 and 1 with a
component that requires a particular baud rate.

An optional second argument configures the data, parity, and stop bits. The
default is 8 data bits, no parity, one stop bit.

Syntax

Serial.begin(speed)
Serial.begin(speed, config)

Parameters

Serial: serial port object. See the list of available serial ports for each board
on the Serial main page.
speed: in bits per second (baud). Allowed data types: long.
config: sets data, parity, and stop bits. Valid values are:
SERIAL_5N1
SERIAL_6N1
SERIAL_7N1
SERIAL_8N1 (the default)
SERIAL_5N2
SERIAL_6N2
SERIAL_7N2
SERIAL_8N2
SERIAL_5E1: even parity
SERIAL_6E1
SERIAL_7E1
SERIAL_8E1
SERIAL_5E2
SERIAL_6E2
SERIAL_7E2
SERIAL_8E2
SERIAL_5O1: odd parity
SERIAL_6O1
SERIAL_7O1
SERIAL_8O1
SERIAL_5O2
SERIAL_6O2
SERIAL_7O2
SERIAL_8O2

Returns

Nothing

Example Code
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600
bps
}

void loop() {}

Arduino Mega example:

// Arduino Mega using all four of its Serial ports


// (Serial, Serial1, Serial2, Serial3),
// with different baud rates:

void setup() {
Serial.begin(9600);
Serial1.begin(38400);
Serial2.begin(19200);
Serial3.begin(4800);

Serial.println("Hello Computer");
Serial1.println("Hello Serial 1");
Serial2.println("Hello Serial 2");
Serial3.println("Hello Serial 3");
}
void loop() {}

Thanks to Jeff Gray for the mega example

Notes and Warnings

For USB CDC serial ports (e.g. Serial on the Leonardo), Serial.begin() is
irrelevant. You can use any baud rate and configuration for serial
communication with these ports. See the list of available serial ports for each
board on the Serial main page.

The only config value supported for Serial1 on the Arduino Nano 33 BLE
and Nano 33 BLE Sense boards is SERIAL_8N1.

Reference > Language > Functions > Communication > Serial > Readstring

Serial.readString()
Description

Serial.readString() reads characters from the serial buffer into a String.


The function terminates if it times out (see setTimeout()).

Serial.readString() inherits from the Stream utility class.

Syntax

Serial.readString()

Parameters

Serial: serial port object. See the list of available serial ports for each board on
the Serial main page.

Returns

A String read from the serial buffer

Example Code

Demonstrate Serial.readString()

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("Enter data:");
while (Serial.available() == 0) {} //wait for data available
String teststr = Serial.readString(); //read until timeout
teststr.trim(); // remove any \r \n
whitespace at the end of the String
if (teststr == "red") {
Serial.println("A primary color");
} else {
Serial.println("Something else");
}
}
Notes and Warnings

The function does not terminate early if the data contains end of line
characters. The returned String may contain carriage return and/or line feed
characters if they were received.
Reference > Language > Functions > Communication > Serial > Parseint

Serial.parseInt()
Description

Looks for the next valid integer in the incoming serial. The function
terminates if it times out (see Serial.setTimeout()).

Serial.parseInt() inherits from the Stream utility class.

In particular:

 Parsing stops when no characters have been read for a configurable


time-out value, or a non-digit is read;

 If no valid digits were read when the time-out (see Serial.setTimeout())


occurs, 0 is returned;

Syntax

Serial.parseInt()
Serial.parseInt(lookahead)
Serial.parseInt(lookahead, ignore)

Parameters

Serial: serial port object. See the list of available serial ports for each board
on the Serial main page.
lookahead: the mode used to look ahead in the stream for an integer. Allowed
data types: LookaheadMode. Allowed lookahead values:

 SKIP_ALL: all characters other than digits or a minus sign are ignored
when scanning the stream for an integer. This is the default mode.
 SKIP_NONE: Nothing is skipped, and the stream is not touched unless
the first waiting character is valid.

 SKIP_WHITESPACE: Only tabs, spaces, line feeds, and carriage returns


are skipped.

ignore: used to skip the indicated char in the search. Used for example to
skip thousands divider. Allowed data types: char

Returns

The next valid integer. Data type: long.


Reference > Language > Functions > Communication > Serial > Parsefloat

Serial.parseFloat()
Description

Serial.parseFloat() returns the first valid floating point number from the
Serial buffer. parseFloat() is terminated by the first character that is not a
floating point number. The function terminates if it times out
(see Serial.setTimeout()).

Serial.parseFloat() inherits from the Stream utility class.

Syntax

Serial.parseFloat()
Serial.parseFloat(lookahead)
Serial.parseFloat(lookahead, ignore)

Parameters

Serial: serial port object. See the list of available serial ports for each board
on the Serial main page.
lookahead: the mode used to look ahead in the stream for a floating point
number. Allowed data types: LookaheadMode. Allowed lookahead values:

 SKIP_ALL: all characters other than a minus sign, decimal point, or


digits are ignored when scanning the stream for a floating point
number. This is the default mode.

 SKIP_NONE: Nothing is skipped, and the stream is not touched unless


the first waiting character is valid.
 SKIP_WHITESPACE: Only tabs, spaces, line feeds, and carriage returns
are skipped.

ignore: used to skip the indicated char in the search. Used for example to
skip thousands divider. Allowed data types: char

Returns

Data type: float.

You might also like