Sets the data rate in bits per second (baud) for serial data transmission. When communicating with the Serial Monitor, ensure that you use one of the baud rates listed in the Serial Monitor's baud rate dropdown menu. 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.
Use the following function to initialize and configure the serial communication:
Serial.begin(baud)Serial.begin(baud, config)The function admits the following objects and parameters:
Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
baud: in bits per second (baud). Allowed data types: long.
config: sets data, parity, and stop bits. Valid values are:
SERIAL_5N1SERIAL_6N1SERIAL_7N1SERIAL_8N1 (the default)SERIAL_5N2SERIAL_6N2SERIAL_7N2SERIAL_8N2SERIAL_5E1: even paritySERIAL_6E1SERIAL_7E1SERIAL_8E1SERIAL_5E2SERIAL_6E2SERIAL_7E2SERIAL_8E2SERIAL_5O1: odd paritySERIAL_6O1SERIAL_7O1SERIAL_8O1SERIAL_5O2SERIAL_6O2SERIAL_7O2SERIAL_8O2The function returns nothing.
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {}
// 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.
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.