JavaScript in the Embedded World: From a Light Bulb to IoT
When we hear "embedded systems," associations with C, C++, or at worst, Python on a Raspberry Pi usually come to mind. But what if I told you that JavaScript — the very language you use for frontend and backend — is increasingly penetrating the world of microcontrollers, sensors, and smart devices?
Yes, you heard that right. Embedded development with JavaScript is no joke, but a very real trend gaining momentum thanks to frameworks like Espruino and Johnny-Five. Today, we'll figure out how to make your "hardware friend" blink LEDs, read sensor data, and send it to the cloud — all in pure JavaScript.
Why JavaScript in Embedded? Three Compelling Reasons
Why drag a scripting language into a resource-constrained world? Here are three arguments that flip the script:
- Low entry barrier. If you already know JS (and who doesn't these days?), you don't need to learn C and deal with pointers. All your experience — from frontend or Node.js — transfers to hardware.
- Rapid prototyping. You can write firmware in JS in an evening. No need to wait for compilation or re-flashing — many platforms support live code loading via Wi-Fi or USB.
- IoT ecosystem. JavaScript fits perfectly into the architecture of "smart" devices: you easily integrate with MQTT, HTTP, WebSockets, and cloud services (AWS IoT, Google Cloud IoT).
Of course, for production with strict power consumption and speed requirements, C++ remains king. But for prototypes, hobby projects, and educational tasks, embedded JavaScript is a real gem.
Toolkit: What We Work With
Before writing code, let's decide on the "hardware." The most popular options for embedded JS:
- Espruino — a microcontroller that understands JavaScript "out of the box." It runs on STM32 and has a JS interpreter on board. Ideal for learning.
- Raspberry Pi Pico + Espruino — an inexpensive microcontroller (only $4) onto which you can flash the Espruino firmware.
- Johnny-Five + Arduino — a classic. You write JS on your computer, and Johnny-Five controls the Arduino via the Firmata protocol. Suitable for those who already have an Arduino.
- Node.js on Raspberry Pi (full Linux) — if you need not just to blink an LED but to set up a web server, control GPIO and cameras — go for Pi 4/5.
In this article, we'll focus on Espruino and Johnny-Five, as they provide the purest JS experience without extra layers.
Writing Code: Blinking an LED in JS
Let's start with the "Hello, World!" of the embedded world — a blinking LED. Connect an LED (with a 220 Ohm resistor) to pin D13 on Espruino (or to pin 13 on Arduino).
Example 1: Espruino (Native JS)
// Connect pin D13 as outputvar led = LED1; // Built-in LED on Espruino
// Blink functionfunction blink() { led.toggle(); // Toggle state console.log('LED is', led.read() ? 'ON' : 'OFF');}
// Set interval to 500 mssetInterval(blink, 500);Upload this code via the Espruino Web IDE (or via terminal). The LED will start blinking at 1 Hz. Notice: we use setInterval — exactly the same as in a browser or Node.js. No registers, timers, or interrupts in C — just JS.
Example 2: Johnny-Five + Arduino
If you have an Arduino, install the Johnny-Five library via npm:
npm install johnny-five