Introduction: Controlling Arduino Uno With Javascript!

I have been learning Javascript of late and I really like the language. So naturally, I thought is there a way to program the Arduino boards with Javascript!

A quick search found johnny-five library in Javascript that uses the firmata protocol to communicate to Arduino. So I quickly hooked up a design with a generic version of the BMP085 pressure & temperature sensor. I was surprised to find out that it was even easier than the programming in processing language that Arduino uses. I am not sure yet but you are most likely going to loose control of low power modes and interrupts but for most people I think it just might work. So if you are a web programmer who wants to try some electronics, give this a shot.

Step 1: Installation and Setup

My setup is as follows:
  • Arduino Uno board
  • VirtualBox running Lubuntu 15.0 64bit
  • Lubuntu running node js and johnny-five (install both nodejs and node-js legacy for this to work). Run the two commands below:
    1. sudo apt-get install nodejs nodejs-legacy
    2. sudo npm install johnny-five

Reason I used virtual box is because I am more comfortable with Linux and I just find it easier to setup and debug hardware issues on Linux. Technically, you should be able to run this on windows too.

After installing the node related software, its time to setup your Arduino. To do so, install the StandardFirmata sketch on your Arduino. Follow the steps below to get that done.

  1. (optional if you are using virtualbox) If you are running the software on a VM, then the first thing to do after you connect your arduino to your computer, is to pass the USB-Serial device to VirtualBox. Look at the picture to see that.
  2. Open arduino gui. If this is the first time you have installed arduino, it will ask your usename to be added to dialout group to be able to use the USB-Serial convertor without root permissions every time. Windows users should not face this problem.
  3. If you have not added your username to "dialout" group then please do so.
  4. Open "StandardFirmata" sketch (Examples->Firmata->StandardFirmata"), select your board and the port (/dev/ttyUSB0 by default).
  5. Upload the sketch. Then open serial monitor. Set the speed to 57600 baud. You should see the message "standardfimata.ino" on the screen. Your setup is complete.

Step 2: Voila!

Copy the bmp085.js file to a working directory. Go there and run

node bmp085.js

Node will connect to Arduino and start getting sensor data.

Type CTRL-C two times to exit the script.

Good luck! Give it a shot if you already have a board. I was amazed that johnny-five connects to so many devices. Check out their API here. http://johnny-five.io/api/

So where can we go from here. For one, the whole thing can be shrunk down to run on $5 Raspberry Pi Zero. With that, you can even skip Arduino if you want since johnny five supports RPI. Then data collected from sensors can be sent to a webserver to be plotted in realtime. This is my first step to series of steps! Add me to your favorites to see how I progress..

Arduino All The Things! Contest

Participated in the
Arduino All The Things! Contest