How to Control Your Arduino Using JavaScript?
Control Arduino Using Javascript
Arduino cards are one of the simplest ways to get into hardware piracy for programmers to control arduino using javascript. There are a wide range of beginner projects that are suitable for almost all those with programming training. Even so, starting may seem intimidating to those who lack time.
Learning a completely new language just to try a microcontroller can seem like a lot of work. What if you wanted to start playing with home electronics in a program language you already know ? Good news: you can !
We have already shown you how to control an Arduino card with Python, and today we are going to show you how to do the same with JavaScript. Rather than just the most basic flashing LED tutorial, today we will use the Johnny-Five framework to control a servo with the computer keyboard, all programmed in JavaScript.
Material list for Your Arduino Project
For this project you will need :
- Arduino Uno (or compatible card): $ 22 in the official shop, although you can get them cheaper, clone cards are available for as little as $ 3.20 on AliExpress.
- Servo de loisi: any Arduino compatible servo from a leisure store will do the trick, I use a $ 1.60 servo from AliExpress.
- A few pieces of connection wire
- USB cable: to connect Arduino to your computer
Today’s tutorial will use an Arduino Uno card. The Johnny-Five framework that we will use later in this project supports most of Arduino compatible microcontrollers, although your card must have a PWM capability to keep the servo happy.

Circuit configuration
Attach your servo to your Arduino like this :
In short, the VCC (ROUGE) line connects to the 5v pin of Arduino, the GND line (LE NOIR or BRUN) connects to the Arduino GND pin and to the Pulse line (Yellow or Orange) connect to pin 10 of the Arduino. Note that although you do not have to connect it specifically to pin 10, it must be connected to a PWM pin, usually referred to as a ~.
Make sure you haven’t mixed a wire and connect the Arduino to your computer. We will use Windows 10 for this project. All elements of this project are also available for Mac and Linux, although some installation instructions may differ slightly.
If you have not already done so, download the Arduino IDE and choose your Plate and Port of the Tools menu. If this is the first time you have done this and everything is a bit mysterious, our beginner’s guide Arduino could help you through these steps.
Once you have connected it, download the StandardFirmataMore example of a sketch on the board. You can find this sketch in the menu drop under Examples> Firmata> StandardFirmataPlus. You do not need to modify the sketch at all, it essentially configures the Arduino so that it awaits external instructions – which we will provide later.
JavaScript robotics with Johnny-Five
The framework we will use to control our Arduino using Javascript is called Johnny-Five. Not surprisingly, given its homonym of film, the project is oriented towards working with robotics.
To install Johnny-Five, we must first install Node.js. You can download their latest version to the Node.js website. We use the recommended version, which at the time of writing is 8.9.4 LTS.
Open the .msi and follow the installation instructions, making sure it is added to your CHEMIN. Node.js’ current installation program adds PATH as standard, although it is worth checking it during installation, as it is required for our next step.
Once the installation is complete, we will have access to the Node Package Manager (NPM) package manager from the Windows (CMD) command line. Click Start and type CMD. Before continuing, we must initialize the NPM to avoid possible installation errors. This does not require any specialized knowledge, just type :
npm init
Follow the guests on the screen. For today’s project, you have nothing to change, just press Enter until you are back at the command prompt, then type :
npm install johnny-five
This will install all the important packages, which will speak with our Arduino. We need something else for this project to work, and it’s Pressing the Package key that will allow us to read the keystrokes.
Install it on entering :
npm install keypress
Once all these packages are installed, we are ready to code !
If you have problems during installation, try to run the installation of johnny-five again by pressing the key. It may just be an oddity in the NPM version used here, but it now avoids a problem that you may encounter later, as I did.

The code
Today we will use an example controlling arduino using Javascript. This is an example of code provided in the johnny-five documentation, which allows us to control our servo motor using the arrow keys on the keyboard. The full code is available on johnny-five.io, but we will go through it here in detail to fully understand how it works.
We are using Eclipse IDE for coding today, although you can use any IDE or text editor for this project.
Create a new file and name the test.js, and save it in a place you can easily access later from the command line. The script begins by creating variables for the required libraries and initializing the Press the library key to listen to incoming data, while calling the Planche () method to set up the table.
var five = require ("johnny-five"); var keypress = require ("keypress"); keypress (process.stdin); var board = new five.Board ();
Note that the configuration of the card here is automatic, no need to specify the ports. If you have a specific port configuration or if you just don’t have a chance with automatic detection, you may need to specify your port explicitly.
Then we want to “wake up” our card and configure it for the bonding control. The edges on the call are waiting for the Arduino pins to be ready before continuing. The johnny-five library supports integrated servos, and we call Servo.Continu (10) on pin 10 to allow direct control.
board.on ("ready", function () { console.log ("Use Up and Down arrows for CW and CCW respectively. Space to stop. "); var servo = new five.Servo.Continuous (10); process.stdin.resume (); process.stdin.setEncoding (" utf8 "); process.stdin.setRawMode (true);
Process.stdin calls guarantee that all data we receive from the keyboard will be usable in the next block of code. Now we want to “listen” to the strikes and use them to move our servo clockwise (CW), counterclockwise (CCW) or to stop in its tracks.
process.stdin.<strong>on ("keypress", function (ch, key) {if (!key) {// if no key is pressed, return i</strong>.e do nothing. return; } if (key.name === "q") { console.log ("Quitting"); process.<strong>exit (); } else if (key</strong>.name === "up") { console.log ("CW"); servo.cw ();} else if (key.name === "down") { console.log ("CCW"); servo.ccw (); } else if (key.<strong>name === "space") { console</strong>.log ("Stopping"); servo.stop (); }});});
Make sure to include all the closing hooks down here and refer to the entire code block as linked above if you get errors. Save this script and open the command prompt.
Hello Hello !
Now access the directory in which you have saved your script and run it by typing :
node test.js
The program must start immediately with information about the card, before giving you the instructions indicated in the code. Try to press the up and down arrow keys, and the space bar, and Q to stop. The screen should look like this :
And all is well, the servo should dance to the rhythm of your strikes ! Look at this little wave of beast !
Modest beginnings
Although we have undertaken a slightly more ambitious project than the usual tutorial on flashing LEDs for beginners, we have barely touched on the full extent of what can be done with Arduino cards and similar microcontrollers.
Seasoned JavaScript users should find the intuitive Johnny-Five package to work with. The library can also be installed on the Raspberry Pi natively, making it the perfect package for manufacturers of budding robots.
The advantage of this library is that, although it is designed for robots, the same input and output data can be used to create DIY smart home configurations and even home security systems.
Communicating with microcontrollers in this way is an excellent introduction to the world of DIY material without having to devote time to learning a whole new programming language. Have fun there, and if you happen to build a killer robot, remember how we helped you at first.