Questions And Answers

More Tutorials

NodeJS Arduino communication

Node Js communication with Arduino via serialport

Node js code

Sample to start this topic is Node.js server communicating with Arduino via serialport.

npm install express --save
npm install serialport --save

Sample app.js:

const express = require('express');
const app = express();
var SerialPort = require("serialport");
var port = 3000;
var arduinoCOMPort = "COM3";
var arduinoSerialPort = new SerialPort(arduinoCOMPort, {
 baudrate: 9600
arduinoSerialPort.on('open',function() {
 console.log('Serial Port ' + arduinoCOMPort + ' is opened.');
app.get('/', function (req, res) {
 return res.send('Working');
app.get('/:action', function (req, res) {

 var action = req.params.action || req.param('action');

 if(action == 'led'){
 return res.send('Led light is on!');
 if(action == 'off') {
 return res.send("Led light is off!");

 return res.send('Action: ' + action);
app.listen(port, function () {
 console.log('Example app listening on port' + port + '!');

Starting sample express server:

node app.js

Arduino code

// the setup function runs once when you press reset or power the board
void setup() {
 // initialize digital pin LED_BUILTIN as an output.
 Serial.begin(9600); // Begen listening on port 9600 for serial
 digitalWrite(LED_BUILTIN, LOW);
// the loop function runs over and over again forever
void loop() {
 if(Serial.available() > 0) // Read from serial port
 char ReaderFromNode; // Store current character
 ReaderFromNode = (char);
 convertToState(ReaderFromNode); // Convert character to state
void convertToState(char chr) {
 digitalWrite(LED_BUILTIN, HIGH);
 digitalWrite(LED_BUILTIN, LOW);

Starting Up

1. Connect the arduino to your machine.

2. Start the server

Control the build in led via node js express server.

To turn on the led:

To turn off the led:


In this page (written and validated by ) you learned about NodeJS Arduino communication . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS N API.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.