Arduino talks back to Node.js - Drama on the Serial Port

Play Arduino talks back to Node.js - Drama on the Serial Port
Sign in to queue


Ever wanted to communicate with an Arduino using Node? We'll explore how to write values from an Arduino and get capture those values in a Node app using serial communication. 

This tutorial will show you how to use the node-serialport module to communicate with an Arduino, in this case an Arduino Uno.

You will need:


1. Program Your Arduino


 Create a new Arduino sketch in the Arduino IDE, and set the Serial to begin at 9600. Then have it write back a few lines.

void setup(){

void loop(){

To upload this to your Arduino board, you need to make sure the right board is selected under Tools>Board and that you have the right serial port selected. If you don't know what serial port your Arduino is on, you can do the following: 


Open your device manager. Look at the COM ports. See what com port the Arduino shows up on. 


Open terminal. Type: 

ls /dev/tty.*
ls /dev/cu.*


2. Install the node-serialport module 

npm install serialport --save


3. Create an appj.js script in the same folder that you installed the module. Require the module. 

var serialport = require("serialport");


4. We will pass in the port that the Arduino is running on via the command line as an argument when we run this node application. We will need to get that from the arguments in the script.

We will call the script like this:

node app.js COM8

And we will capture that argument in the node application: 

var port = process.argv[2];


5. We will create an instance of the serialport module and then we will add listeners to some of its events that it emits. We are configuring the instance to listen at a baud rate of 9600 ( baud rate is how fast the data is transmitted and received ) and we will configure the instance to use the read line parser, so we can get the whole line and not data bits. Whatever baud rate  you set your arduino to communicate at , you must also use here. 

var sp = new SerialPort(port, {
  baudrate: 9600,
  parser: serialport.parsers.readline("\n")

sp.on('open', onPortOpen);
sp.on('data', onData);
sp.on('close', onClose);
sp.on('error', onError);

function onPortOpen(){
    console.log("YESSIR THE PORT IS OPEN COS CAPS");

function onData(d)
    console.log("data dis, data dat "+d)

function onClose(){
    console.log("Port is closed, yo");
function onError(){
    console.log("something went horribly wrong");

 And that's it. Simple communication between Node.js and an Arduino. Now time to make all the weather apps and blink blink blanks you can possibly imagine! 




Right click to download this episode

The Discussion

  • User profile image

    So much awesome can be made from this tutorial, thank you! 

  • User profile image
    Jim Stanley

    Thanks for your tutorial. I'm having problems installing serialport on my system -- I get compile errors.

    I'm using Visual Studio 2015 on Windows 8.1 and node version 4.1.1.

    Please tell me what versions of these tools you are using.


Add Your 2 Cents