DEV Community

Cover image for Let's program the Arduino with Javascript🤯
Patrick
Patrick

Posted on • Edited on • Originally published at blog.thepatik.com

Let's program the Arduino with Javascript🤯

Have you ever programmed an Arduino? Did you know that Arduino can be programmed with JavaScript?😏

Requirements

  • Arduino UNO microcontroller,
  • LED,
  • 220-ohm resistor,
  • Arduino IDE installed,
  • NodeJS installed (I recommend the latest LTS version),
  • Visual Studio Code (or another code editor).

We must first assemble the circuit we are going to work with.

A circuit similar to this is created in TinkerCad.

Arduino circuit

Once we have the circuit assembled we need to prepare our Arduino for programming with JavaScript not yet completely.

We must first install the Firmata library on it.

Navigate to File> Examples> Firmata> StandardFirmataPlus and upload the file that opens to our Arduino.

Where can find Firmata Library in Arduino IDE

Upload the code to the Arduino board by pressing the Upload.

After a successful upload, we need to install some pre-required tools.

Now is the time to make a directory where we will write our code for Arduino.

Since I'm using a Windows computer, I had to do a couple of things before I could start programming Arduino with JavaScript.

In the console with administrative privileges, enter two commands to install two more programs.



npm --add-python-to-path install --global --production windows-build-tools


Enter fullscreen mode Exit fullscreen mode

and install the node-gyp JavaScript library with the command



npm install -g node-gyp


Enter fullscreen mode Exit fullscreen mode

For your operating system, check what you need to install before starting at this link.

After installing everything you need, we can start working.

We will use the johnny-five library to program the Arduino, which is one of the better libraries for programming microcontrollers. It supports Arduino, Raspberry Pi and more ... A list of all is available at this link.

The library allows us to program many components for the Arduino. The advantage I see is that it makes many tasks easier for us compared to C ++.

Code for our simple circuit.



const {Board, Led} = require("johnny-five");
const board = new Board({
    port: "COM3" // Check if is your Arduino on this port (this you can make in Arduino IDE)
});

board.on("ready", () => {
  const led = new Led(3);
  led.blink(500);
});


Enter fullscreen mode Exit fullscreen mode

Now, all we have to do is run the program on our Arduino. To do this, type in the command line:



node main.js # In case if our file is named main.js


Enter fullscreen mode Exit fullscreen mode

Now it's your turn to start creating a variety of circuits with JavaScript and the Arduino microcontroller.

If you like the content I create, you can start following me on my Twitter account.

Top comments (17)

Collapse
 
pgradot profile image
Pierre Gradot • Edited

Just to be clear: you don't program the Arduino with JS; you run a JS program on your computer that controls the Arduino. If the computer is not connected to the Arduino, the Arduino doesn't "work".

Am I right?

Collapse
 
patik123 profile image
Patrick

Yes it's true. The purpose of the article was to show web developers how to manage Arduino with JavaScript, and thanks for comment.

Collapse
 
pgradot profile image
Pierre Gradot • Edited

My question was not some kind of value judgement, but really to clarify my understanding.

You don't always need the Arduino to be standalone.

Sometimes, it's OK to have the Arduino connected to your computer to run the program. For instance, when you use it to communicate with an electronic test equipment.

Thread Thread
 
patik123 profile image
Patrick

Sessions agree that no, if we want to use an Arduino standalone without a computer it's C ++, so. However, if we want to test a circuit or learn how to assemble electronic components together, it is easier to use JavaScript.

Collapse
 
fredericrous profile image
Frederic R.

There's a board that runs javascript. Have you tried espruino?

Collapse
 
edo78 profile image
Federico "Edo" Granata

A few years ago I tried something similar but using a raspberry pi to control a circuit with node.js

I have a few problem for lack of support and I even had to write a small package to controll an adc0832 (released with MIT licence) github.com/Edo78/ADC0832

Collapse
 
patik123 profile image
Patrick

Nice.

Collapse
 
edo78 profile image
Federico "Edo" Granata

Thanks, it was a little fun project to show my daughters what a developer can do :D
youtu.be/kGFI8ifeHwY

Thread Thread
 
patik123 profile image
Patrick

The developer has the power to control the world 😉.

Collapse
 
drumsticks profile image
Ryan • Edited

When attempting to set this all up and then running "node filename.js", I am receiving the following summarized error:
"Error: Cannot find module 'firmata' ... code: 'MODULE_NOT_FOUND'...

I then attempted to node install firmata, but it had a lot of errors during installation. I also attempted to node install arduino-firmata, but I don't think the latter is the correct module to use.

Any ideas on how I might go about fixing this issue?

On another issue, when attempting to execute the suggested script for windows-build-tools, errors occurred as it appears to be suggesting that this particular library is now a part of (baked in) node.
npm --add-python-to-path install --global --production windows-build-tools

Could this be the case?

Collapse
 
drumsticks profile image
Ryan

I got this working by installing Visual Studio Build Tools. I didn't think I would need something so heavy as it installed about 1.6 GB on my machine. My intention is to allow a user to use an Arduino from a website by using javascript. I'm rather new to the npm and node side of things, but if I would like a user to use the arduino by going to my website, would they have to install visual studio and the likes or will this VS only be needed on my web server?

Collapse
 
saksholm profile image
Joni Saksholm

Cool to see these kind of articles. I made quite big DIY really customized heatpump controller with mega2560+nano backpack with johnny-five.

github.com/saksholm/heatpump-node

Collapse
 
patik123 profile image
Patrick

Nice.

Collapse
 
juniordevforlife profile image
Jason F

Very cool. Thank you!

Collapse
 
patik123 profile image
Patrick

Thank you for reading.

Collapse
 
rammina profile image
Rammina

Seems fun to play with!

Collapse
 
patik123 profile image
Patrick

Yes it is.