JavaScript Arduino Electronics, Complete Introduction— Part 1: Getting Started

In this series of posts, (after a brief introduction in Part 1) you will learn how to build a circuit with an LED light and the Arduino UNO and switch it on and off from JavaScript inside the Node.js environment (go to Part 2). Next, we will look at electronic sensors and how to measure temperature and light (go to Part 3).

You can buy an extended printed version of all these posts on Amazon for only $4.95 / £4.95:
Introduction to JavaScript Electronics on Amazon.com
Introduction to JavaScript Electronics on Amazon.co.uk

Otherwise, you can download a free PDF on www.webondevices.com

If you liked this brief introduction, have a look at my JavaScript Electronics book in which you will learn how to build a Smart Talking Plant:
JavaScript Electronics on Amazon.com
JavaScript Electronics on Amazon.co.uk

Introduction

In recent years, JavaScript has evolved past merely being a tool for DOM manipulation and begun to expand its influence beyond the realms of the browser window.

We now see JavaScript appearing on operating systems (Chrome OS, Firefox OS), on the server (Node.js), and even on tiny microcontroller chips. It has also begun to become commonplace to see developers building hardware prototypes and electronic toys using JavaScript on prototyping platforms like the Arduino.

These platforms and development boards made working with electronics possible for those without a background in electrical engineering.

This series of articles will help you take the first step into the world of Creative Technology, and start building electronic prototypes with the Arduino UNO development board and JavaScript.

Restrictions of the browser

If we compare it to desktop and mobile applications, JavaScript running in the browser is quite limited. Access to communication ports, hardware components, and the file system are all restricted. This level of security is necessary on the Internet as not every website visited can be trusted, therefore, you need to ensure your computer can’t be infiltrated by malicious code. However, we somehow need to gain access to the USB port to be able to control electronic circuits on the Arduino UNO.

One way to work around the security blocks is to run your application outside the browser, on a server computer, from where you can stream all the required data to the front-end application in the browser. The server can trust the code running on itself and will grant access to its hardware resources.

There are many ways to write server applications, for example, running PHP on an Apache Linux server supported by a database (MySQL, Postgres, etc.), sometimes known as a LAMP stack. Instead of this setup JavaScript can also be used with our own computer as a server.

The next step before we can start building the circuits is to write the server application that will access the USB port and handle the Arduino; an ideal task for Node.js.

JavaScript outside the browser

Node.js is built on top of Google’s V8 JavaScript engine which can run independently from the browser.

Using JavaScript and Node.js for electronics hacking is not a new endeavour. The community is already using it to power successful projects like nodebots.io, Firmata, Cylon or Johnny-Five. The Node.js ecosystem is even stronger, with over a hundred thousand packages available via the node package manager.

The event-driven, asynchronous properties are also great for working with electronics and sensors, enabling applications that communicate through NFC, RFID or Bluetooth, all of which are currently impossible in the browser.

Using Node.js opens up new possibilities for application development using devices like the Xbox Kinect, Leap Motion, midi controllers, drones and smart home accessories like the Nest thermostat or the Philips HUE light bulb.

Development boards

The development board revolution started around 2005 when the first Arduino was released. The Arduino project was started by a few Italian university teachers trying to make it easy for their students to work with electronic components and program microcontroller chips. Arduino is still by far the most popular platform today.

The microcontroller is the brain of the board that controls and manages everything. This is a low cost, low power, low-performance processor capable of running a single application at a time. The microcontroller is only responsible for managing low-level input and output electric signals and performing some basic calculations with them. This chip is capable of switching electronic components on and off, measuring electricity from sensors, and interfacing with other components.

These chips can work independently, however, development boards can streamline the building process. They help power, communicate, and interface with the microcontroller. The addition of a USB port to upload the code on the UNO and a barrel plug for powering the system from a 9V battery are both helpful. Currently, there’s an overwhelming number of boards available on the market to choose from. Most of these are Arduino compatible, so how do they all differ from each other?

As a start, they greatly differ in size. Some of the smallest boards available are the DFRobot Beetle and the Tinyduino. These are great for wearable or drone projects where size and weight are important factors. With the smaller size, you have a stripped down feature sets and fewer components too. This means fewer things to power, so they are perfect for battery-powered prototypes.

Other boards enable features like Wi-Fi, Ethernet or Bluetooth connection, while others can act as a mouse, keyboard or another USB controller when plugged into your computer. There are also boards designed for building home automation and home security systems.

Part 2

In part 2, we will learn how to build a circuit with an LED light and the Arduino UNO and switch it on and off from JavaScript inside the Node.js environment!

Senior Front-end Web Developer, Creative Technologist with over 12 years experience. Loves pushing the limits of HTML and CSS.