Serial Communication Arduino
Serial Communication Arduino
Serial.println()
Description
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);
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-
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
Reference > Language > Functions > Communication > Serial > Read
Serial.read()
Description
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();
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() {}
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() {}
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
Syntax
Serial.readString()
Parameters
Serial: serial port object. See the list of available serial ports for each board on
the Serial main page.
Returns
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()).
In particular:
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.
ignore: used to skip the indicated char in the search. Used for example to
skip thousands divider. Allowed data types: char
Returns
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()).
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:
ignore: used to skip the indicated char in the search. Used for example to
skip thousands divider. Allowed data types: char
Returns