Serial Debugging On the Particle Electron

As I started writing code for the Particle Electron, it wasn't long until I was fed up with my debugging setup. After a little bit of digging, I found out that it's quite simple to get USB or serial printing to work.

The Particle Electron has two USB channels and four USART channels that we can use for our debug output. These channels can be accessed using the various Serial classes provided by Particle.

A quick note: I am writing this guide for quick processing by experienced firmware hackers. If you are unfamiliar with communicating to hardware devices over a Serial port, I recommend trying the USB approach or checking this detailed tutorial for more information Particle USB/Serial debugging setup.

Serial

Particle Electron supports four serial ports:

  • Serial1
  • Serial2
  • Serial4
  • Serial5

Serial1 is the most convenient port to access, as it is wired out the the Electron's TX and RX pins.

Note that if you intend to use the USART port(s) to communicate with your host machine, you will need a USB-to-serial adapter (such as an FTDI device or their many clones). The TX and RX pin definitions will be flipped when connecting two devices: TX lines connect to RX lines on the other end, and vice-versa. Don't forget to connect the ground pin!

To configure the TX/RX pins to act as a serial port, you need to configure the Serial1 interface. Pulling an example from the Particle documentation:

Serial1.begin(speed);         // via TX/RX pins
Serial1.begin(speed, config); //  "

Serial1.begin(9600, SERIAL_9N1); // via TX/RX pins, 9600 9N1 mode
Serial1.begin(9600, SERIAL_DATA_BITS_8 | SERIAL_STOP_BITS_1_5 | SERIAL_PARITY_EVEN); // via TX/RX pins, 9600 8E1.5

For further serial port information, I recommend reading this Particle community tutorial.

USB

USB is certainly more convenient to use for our debugging purposes, as we don't need to bother with any adapters.

Particle provides two interfaces for USB serial comms:

  • Serial (no number!)
  • USBSerial1 (added as of system firmware v0.6.0)

These interfaces communicate through the USB port. When connected to a computer, each will show up on the host as a separate connected port. The USBSerial1 port is disabled by default.

Enabling and using the Serial interface is quite simple:

// EXAMPLE USAGE
void setup()
{
  Serial.begin(); //or USBSerial1.begin();
  Serial.println("Hello World!");
}

Now we are ready to rock and roll with our serial debugging

particle monitor

While you can use your favorite serial application (e.g. picocom, miniterm, screen), it's worth noting that the Particle CLI provides serial monitor functionality. The CLI only supports reading from the device, which is certainly good enough for debugging purposes.

Simply run:

$ particle serial monitor
Opening serial monitor for com port: "/dev/cu.usbmodem"
Hello world!

Further Reading

Getting up and running with serial debugging is a common task for experienced embeddded developers, so I have left out many details here. If you need more in-depth instructions, please see the links below.