Arduino and Node.js via Serial port

Reading from the serial port in arduino is one of the most common things to do, and yet it’s not always easy to find good/simple documentation and/or articles about it. In this case, I’ll focus on one simple scenario, communicating with a node.js process via the serial port.

One of the reasons why you might want to do this, is either to communicate to the arduino from your local computer, or perhaps to communicate from a raspberry pi with a USB cable. While Arduino has proven to be an amazing board with very powerful capabilities, there are some complex tasks that for DIY’ers can be daunting and unnecessary, for example anything ML related, image processing, complex communications (www, websockets, SSL, etc…), etc…

So, if you find yourself wondering how to “talk” to your arduino in the most simple way, USB cable and Serial might as well be what you need. (If you are looking to connec to your Arduino via serial to do complex stuff, I urge you to check firmata.js).

Writing to serial in Arduino, reading from node.js

Writing to the serial port in Arduino is actually quite trivial. If you ever saw any arduino samples, this will look awfully familiar:

From node.js there are a number of different approaches, but the one that I liked best because of how simple it is, is to use serialport with parser-readline. It's very convenient to know how/when to split messages, and parser-readline will do just that. Whenever parser-readline finds the delimiter you configure, it will emit the data received as a string. The code for this is super simple as well:

A few things to notice from this snipet:

  • In order to use serialport you have to npm install serialport it.
  • You will have to find the name of the serial port in your computer. An easy way to do this is from Arduino IDE, tools > port menu.
  • Make sure to use the same baud rate as the arduino (defined in setup function).
  • Since we used Serial.println (print line), we used the \n delimiter in the Readline parser.
  • Notice how we subscribe to the data event on the parser, and not the port directly.

Reading from the serial in arduino

Reading from the arduino is where things become slightly more fuzzy. In our case, since we control the other end of the serial port, we control how the data will be sent.

In order to simplify the process considerably, I strongly recommend you:

  1. define a max length for your messages.
  2. define a 1 character long message separator. ie: ‘\n’

Sending a message from node.js as you might expect is very simple:

Due to the fact that we defined a max length for messages, we can define a variable “data” to reserve the space in memory.

The function for reading from the serial port becomes pretty simple:

For as long as you don’t send messages larger than DATA_MAX_SIZE, you should be fine. Like the comment in the code suggests, make sure that you parse your incoming messages in a robust way.

Notice the use of memset to "clean" the data buffer.

Notice that anything you print using Serial.println will be received by the node.js process so, make sure to parse your messages accordingly in the node.js code as well!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store