bobbyg603 bobbyg603 - 1 year ago 251
Node.js Question

Node.js SerialPort Arduino Timing Issue

I'm trying to get my Raspberry Pi/Node.js to communicate with an Arduino Uno using node-serialport. I'm having trouble with the following block of code:

Raspberry Pi

var SerialPort = require("serialport").SerialPort;
var serialPort = new SerialPort("/dev/ttyACM0", {
baudrate: 9600

serialPort.on("open", function () {

serialPort.on('data', function(data) {
console.log('data received: ' + data);

serialPort.write(new Buffer('4','ascii'), function(err, results) {
console.log('err ' + err);
console.log('results ' + results);


// LED on pin 12
int led = 12;

// Incoming serial data
int data=0;

void setup() {
// Pin 12 set to OUTPUT
pinMode(led, OUTPUT);

// Start listening on the serialport

void loop() {


// Read from serialport
data =;

// Check and see if data received == 4
if(data=='4') {
// Blink the LED 3 times
for(int i=0;i<3;i++){
digitalWrite(led, HIGH);

// Reset data to 0

I am only able to get the Arduino to blink the LED if I wrap a for loop around the serialPort.write() function. At the 40th loop iteration or so the LED finally starts blinking and will continue blinking until serialPort.write() loop is finished. This leads me to believe that I'm encountering some sort of timing issue. I'm looking for a more elegent (and non-blocking) solution to blinking the LED on the Arduino via the Pi. Any help would be greatly appreciated.


Answer Source

The problem had to do with Arduino's "AutoReset" see more here.

I didn't actually disable autoreset... I went ahead and implemented the code I posted above. I require user interaction to trigger SerialPort.write(). This works as long as there is a few seconds between when the serial connection is opened and the first write.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download