Read Arduino Serial Monitor Using NodeJS by@madhavbahl10
27,915 reads

Read Arduino Serial Monitor Using NodeJS

Read on Terminal Reader

Too Long; Didn't Read

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Read Arduino Serial Monitor Using NodeJS
Madhav Bahl HackerNoon profile picture


Madhav Bahl
react to story with heart

This is a short tutorial article on how to read the serial port values from arduino to in NodeJS.


So, there was a project I was working on where I had to fetch the data from serial monitor in my node console and then using web sockets display the data in a web based application. But, I was having a hard time figuring out how to fetch the data, I explored and found serialport npm module, but the output it gave was in form of buffer, not the exact word I wanted. After spending some time, I was able to figure out how to do it. Hence, I am sharing this so that it can help anyone who is trying to do the same.

How does arduino and other peripherals communicate with Laptop?

In most of the cases, the answer is “Serial Communication”. With serial communication, the data can be sent bit by bit “in serial” using a wire.The image given below is self explanatory, and gives a clear idea about serial and parallel communication.


Keeping in mind the main focus of this article, I will stop this discussion here and move to the point directly. However, if you want to read more about serial communication, you can refer wikipedia and if you want to read about serial communication in arduino, you can refer the official website of arduino.

About my project!

I made a tap water wastage detection system using arduino. It was based on a simple idea of combining rain water module with ultrasonic sensor.


**What has ultrasonic sensor got to do with water wastage detection?**Simple, using rain water module, I was sensing whether water drops fall on it, but, even if water drops fall over the rain water module, it would be wrong to conclude that water is being wasted. It can be a case that a person is drinking water from the tap, and somehow some water falls over the module. Clearly, I don’t want to activate my water wastage detection system in this case. So, to prevent wrong output, Ultrasonic sensor is being used to detect whether a person is present nearby at the time when rain drop module senses water wastage. The idea is quite simple, if tap is off it means water is not being wasted (Genius!), if tap is on, and a person is in it’s proximity (say 70cm), again it would mean water is not being wasted (The person is probably using) . But, if tap is on, and there is no one nearby, it clearly implies that water is being wasted and the detector can give an alert now.


So, basically this project combines the usage of rain drop module and ultrasonic sensor to detect water wastage.Download the code here

Required Output…


This is the output on serial monitor which I intend to fetch on my Node server and then using web sockets, give real time updates about the tap. Let’s write a script for it :)

A good question that arises is can we combine hardware and JavaScript? Originally, JS was not intended to be used with hardware, but now much advances in the language have been made and yes, it can be used. Read this interesting article: NodeBots — The Rise of JS Robotics.

Getting Started — Installing the serialport library

The serialport library provides a great support for combining hardware projects and JavaScript.

Imagine a world where you can write JavaScript to control blenders, lights, security systems, or even robots. That’s right — robots! Thanks to Node Serialport, that world is here.

Visit this link for complete API documentation.

So, first of all, as we do with any node module, we will install it.

$ npm install --save serialport

After this node module has been downloaded, we can finally start :)

Opening port and reading values

Just like in arduino programs, before any real communication can take place, we need to actually open the port. It can be done by the following piece of code.

var SerialPort = require('serialport');

var serialPort = new SerialPort('/dev/ttyACM0', {baudrate: 9600});

Instead of /dev/ttyACM0 you must write the name of your port name. It would be something like COM17 on windows, or something like the above mentioned on UNIX based systems.

Now that the port is open, we can finally plug in our arduino and fetch the data using the following code.

// Switches the port into "flowing mode"serialPort.on('data', function (data) {console.log('Data:', data);});

// Read data that is available but keep the stream from entering //"flowing mode"serialPort.on('readable', function () {console.log('Data:',;});

AWESOME!!! We are done with code, let’s try it out.

$ node filename.js


Wait! What!?

So, this was what I was talking about earlier. Instead of giving the text, it gives the data buffer.

No need to worry!The text data can be easily parsed and obtained. Use the code given below to do so!

// Require the serialport node modulevar serialport = require('serialport');var SerialPort = serialport.SerialPort;

// Open the portvar port = new SerialPort("/dev/ttyACM0", {baudrate: 9600,parser: serialport.parsers.readline("\n")});

// Read the port dataport.on("open", function () {console.log('open');port.on('data', function(data) {console.log(data);});});

Okay, let’s run this code!


Yay! It worked!


This article was mainly about JavaScript and hardware, and focused on communicating with serial port using NodeJs. Hope it will help you in case you are stuck at that data buffer thing just like I was.

Thanks a lot for keeping your calm and reading till end. All the best and Happy coding!You can contact me in case of any doubts or if you need any assistance:Email: [email protected]:


. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa