USART / UART / Serial Port

Serial Class in the Reference.

Espruino can respond to information on serial ports or USB. If you're plugged in via USB, you can create a very simple command interpreter on the first serial port with the following code:

var cmd="";
Serial1.on('data', function (data) {
  var idx = cmd.indexOf("\r");
  while (idx>=0) {
    var line = cmd.substr(0,idx);
    cmd = cmd.substr(idx+1);
    var s = "'"+line+"' = "+eval(line);
    idx = cmd.indexOf("\r");

Or you can do a quick 'loopback' test. Connect the RX and TX pins together for one of the serial ports (for example Serial4 on some boards), and then do:

Serial4.on('data', function (data) { print("<Serial4> "+data); });
Serial4.print("Hello World");

Most chips can also have the same Serial port on different pins, for example Serial1 TX is available on A9 or B6 on the Espruino Board (look at the datasheets for the board you have). Espruino will choose the first available pins by default - if you wish to use an alternate set of pins, you can specify them when you set up the Serial port:

Serial1.setup(9600, { tx:B6, rx:B7 });

For more information, please see the reference for your board.

USARTs (CK pin)

Some of the UARTs have a CK pin - this is an optional clock that can be generated alongside the normal serial output. It is not enabled by default but you can use it by specifying it when initialising:

Serial1.setup(9600, { tx:B6, rx:B7, ck:A8 });

Parity/Framing errors

If there are parity errors (if parity is enabled), you can get notified of them with the following event listener if errors:true was set when initialising the serial device:

  Serial1.setup(9600, { parity:"e", errors:true } );
  Serial1.on('parity', function() {
    console.log("Oh no!");

You can also get notified of framing errors (when the START and STOP bits are not correct) with the following (errors:true is also required):

  Serial1.on('framing', function() {
    console.log("Oh no!");

Console over serial

Espruino will by default connect its interactive console to the Serial1 port, or to the USB serial port if you are connected to a computer. When the console is on a port, a listener added with SerialX.on('data', ...) will no longer get called. See this troubleshooting post for more information.

To avoid this behaviour, for example if you wish to use Serial1 to talk to a device while disconnected from USB, explicitly set the console serial port using Serial.setConsole during the init event which Espruino runs on boot.

E.on('init', function() {

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.