Serial
(inherits from Stream
)
Serial, Serial1, Serial2, Serial3, Serial4, Serial5
Device | Serial | USBSerial1 | Serial1 | Serial2 | Serial3 | Serial4 | Serial5 |
---|---|---|---|---|---|---|---|
Argon | ✓ | ✓ | |||||
Boron | ✓ | ✓ | |||||
B Series SoM | ✓ | ✓ | |||||
Tracker SoM | ✓ | 2 | |||||
P2 | ✓ | ✓ | ✓ | ✓ | |||
Photon 2 | ✓ | ✓ | ✓ | ✓ | |||
Photon | ✓ | ✓ | ✓ | 1 | |||
P1 | ✓ | ✓ | ✓ | ✓ | |||
Electron | ✓ | ✓ | ✓ | 1 | ✓ | ✓ | |
E Series | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
- (1)
Serial2
on the Photon and Electron uses the same pins as the RGB status LED, and cannot be used without physically disconnecting the status LED on the device by removing the LED or current limiting resistors. - (2)
Serial1
on the Tracker One shares the same pins asWire3
on the external M8 connector. Serial
is a USB serial emulation, not a hardware UART.USBSerial1
is available on Device OS 0.6.0 and later, and is a second USB serial emulation.
There are also other interfaces that can be used for communication:
Interface | Maximum Speed (Gen2) | Maximum Speed (Gen 3) | Maximum Peripheral Devices |
---|---|---|---|
UART Serial | 230 Kbit/sec | 1 Mbit/sec | 1 (point-to-point) |
I2C | 400 Kbit/sec | 400 Kbit/sec | Many (limited by addresses) |
SPI | 60 Mbit/sec | 32 Mbit/sec | Many (limited by CS GPIO pins) |
Serial:
This channel communicates through the USB port and when connected to a computer, will show up as a virtual COM port.
// EXAMPLE USAGE - NOT RECOMMENDED
void setup()
{
Serial.begin();
Serial.println("Hello World!");
}
// EXAMPLE USAGE - PREFERRED
SerialLogHandler logHandler;
void setup()
{
Log.info("Hello World!");
}
Instead of using Serial
directly, you should use it using the the SerialLogHandler
if you are writing debugging messages. Using the Log
method makes it easy to switch between Serial
and Serial1
(or both!) as well as providing thread-safety. It also allows log messages to be intercepted by code, and even saved to things like SD cards, with additional libraries.
You should also avoid mixing the use of Serial.printlnf
and Log.info
(and similar calls). Since Serial.print
is not thread safe, it can interfere with the use of Log
calls.
Serial1:
This channel is available via the device's TX and RX pins.
Gen 3 Devices (B Series SoM, Tracker SoM, Tracker One, Boron, Argon, and E404X):
Hardware flow control for Serial1 is optionally available on pins D3(CTS) and D2(RTS) on the Gen 3 devices (Argon, Boron, B Series SoM, and Tracker SoM).
The Tracker SoM can use the TX and RX pins as either Wire3
or Serial1
. If you use Serial1.begin()
the pins will be used for UART serial. If you use Wire3.begin()
, RX
will be SDA
and TX
will be SCL
. You cannot use Wire3
and Serial1
at the same time. Likewise, you cannot use Wire
and Wire3
at the same time, as there is only one I2C peripheral, just different pin mappings. This is primarily use with the Tracker One as TX/RX are exposed by the external M8 connector. By using Wire3.begin()
you can repurpose these pins as I2C, allowing external expansion by I2C instead of serial.
P2 and Photon 2 Devices:
The P2 and Photon 2 support three serial interfaces, two with hardware flow control:
Pin | Pin Name | Description | Interface | MCU |
---|---|---|---|---|
30 | D10 / WKP | D10 GPIO, Serial 3 CTS, WKP. (Was WKP/A7 on P1.) | Serial3 (CTS) | PA[15] |
40 | S0 / D15 | S0 GPIO, PWM, SPI MOSI, Serial3 TX. (Was P1S0 on P1.) | Serial3 (TX) | PA[12] |
41 | S1 / D16 | S1 GPIO, PWM, SPI MISO, Serial3 RX. (Was P1S1 on P1.) | Serial3 (RX) | PA[13] |
42 | S2 / D17 | S2 GPIO, SPI SCK, Serial3 RTS. (Was P1S2 on P1.) | Serial3 (RTS) | PA[14] |
45 | D2 | D2 GPIO, Serial2 RTS, SPI1 MOSI | Serial2 (RTS) | PA[16] |
51 | D3 | D3 GPIO, Serial2 CTS, SPI1 MISO | Serial2 (CTS) | PA[17] |
52 | D4 | D4 GPIO, Serial2 TX, SPI1 SCK | Serial2 (TX) | PA[18] |
53 | D5 | D5 GPIO, Serial2 RX, SPI1 SS | Serial2 (RX) | PA[19] |
63 | RX / D9 | Serial1 RX (received data), GPIO | Serial1 (RX) | PA[8] |
64 | TX / D8 | Serial1 TX (transmitted data), GPIO | Serial1 (TX) | PA[7] |
- The UART pins are 3.3V and must not be connected directly to a RS-232C port or to a 5V TTL serial port
- Hardware flow control is optional; if not used then the RTS and CTS pins can be used as regular GPIO
- Serial1 uses the RTL872x UART_LOG peripheral
- Serial2 uses the RTL872x HS_UART0 peripheral
- Serial3 uses the RTL872x LP_UART peripheral
Serial2:
On the P2 and Photon 2, this channel is available. It uses the same pins as SPI1
, so you can only use Serial2
or SPI1
, not both.
On the Photon, this channel is optionally available via pins 28/29 (RGB LED Blue/Green). These pins are accessible via the pads on the bottom of the PCB See PCB Land Pattern. The Blue and Green current limiting resistors should be removed. If the user enables Serial2, they should also consider using RGB.onChange() to move the RGB functionality to an external RGB LED on some PWM pins.
On the Electron, this channel is shared with the RGB Green (TX) and Blue (RX) LED pins. If used for Serial2, the LED or current limiting resistors should be removed. As there are no test pads for these LED pins on the Electron, Serial2 will be difficult to use.
On the P1 and E Series, this channel is shared with the RGB Green (TX) and Blue (RX) LED pins. Since you supply your own LEDs on these devices, you can move them to other pins using RGB.mirrorTo().
Other devices do not support Serial2
. On the Argon and Boron, the hardware port is used to communicate with the network coprocessor (NCP) and is not available for user use.
To use Serial2, add #include "Serial2/Serial2.h"
near the top of your app's main code file.
Serial3
: This channel is optionally available on the P2 and Photon 2 only. It optionally supports hardware flow control. To use Serial3, add #include "Serial3/Serial3.h"
near the top of your app's main code file.
Serial4:
This channel is optionally available via the Electron and E Series C3(TX) and C2(RX) pins. To use Serial4, add #include "Serial4/Serial4.h"
near the top of your app's main code file. This port is not available on other devices.
Serial5:
This channel is optionally available via the Electron and E Series C1(TX) and C0(RX) pins. To use Serial5, add #include "Serial5/Serial5.h"
near the top of your app's main code file. This port is not available on other devices.
USBSerial1
: Available on Gen 2 (Photon, P1, Electron, E Series) with Device OS 0.6.0. and later: This channel communicates through the USB port and when connected to a computer, will show up as a second virtual COM port. This channel is disabled by default.
To use the Serial1 or other hardware UART pins to communicate with your personal computer, you will need an additional USB-to-serial adapter. To use them to communicate with an external TTL serial device, connect the TX pin to your device's RX pin, the RX to your device's TX pin, and ground.
// EXAMPLE USAGE
void setup()
{
Serial1.begin(9600);
Serial1.println("Hello World!");
}
// EXAMPLE USAGE Serial4 on Electron and E Series
#include "Serial4/Serial4.h"
void setup()
{
Serial4.begin(9600);
Serial4.println("Hello World!");
}
To use the hardware serial pins of (Serial1, etc.) to communicate with your personal computer, you will need an additional USB-to-serial adapter. To use them to communicate with an external TTL serial device, connect the TX pin to your device's RX pin, the RX to your device's TX pin, and the ground of your device to your device's ground.
NOTE: Please take into account that the voltage levels on these pins operate at 0V to 3.3V and should not be connected directly to a computer's RS232 serial port which operates at +/- 12V and will damage the device.
NOTE: On Windows 10, using USBSerial1
on the Electron, E Series, and P1 may not be reliable due to limitations of the USB peripheral used for those 2 devices. Characters may be dropped between the computer and device. USBSerial1
is reliable on other operating systems and also on the Photon. USBSerial1
is not available on Gen 3 devices (Argon, Boron, B Series SoM, and Tracker SoM).
For more information about serial ports, see learn more about serial.