JavaScript Robotics: Controlling Hardware with Johnny-Five and Arduino

JavaScript is no longer limited to web development; it has expanded into controlling hardware devices, thanks to frameworks like Johnny-Five. Johnny-Five is a powerful and user-friendly JavaScript robotics library that allows developers to interact with hardware components like LEDs, motors, sensors, and more using microcontrollers such as Arduino.

What is Johnny-Five?

Johnny-Five is a JavaScript robotics and IoT (Internet of Things) platform that allows you to control hardware devices using JavaScript. It provides a simple and intuitive API that abstracts the complexities of working with electronics, making it easier for developers to prototype and experiment with physical computing projects.

Johnny-Five supports a wide range of hardware platforms, including Arduino, Raspberry Pi, Intel Edison, and more. The basic idea is that you create a new instance of the Board class, and then you can use the Board object to control the hardware devices that are connected to your Arduino board.

Getting Started with Arduino and Johnny-Five

To get started with Arduino and Johnny-Five, you will need the following:

  • An Arduino board (such as the Arduino Uno)

  • A computer with Node.js installed

  • The Johnny-Five library (installable via npm)

  • Standard Firmata uploaded to your Arduino board

First, install Johnny-Five using npm:

npm install johnny-five

Once you have the necessary hardware and software in place, you can start building your JavaScript-powered robotics projects.

Controlling an LED with Johnny-Five

To demonstrate the capabilities of Johnny-Five and Arduino, let's build a simple project to control an LED using JavaScript.

First, connect an LED to your Arduino board. Connect the positive leg of the LED to pin 13 on the Arduino, and the negative leg to the ground (GND) pin through a 220-ohm resistor.

Arduino Pin 13 GND LED 220?

Next, create a JavaScript file called led-control.js and write the following code:

const { Board, Led } = require('johnny-five');

// Initialize a new Arduino board
const board = new Board();

// When the board is ready
board.on('ready', () => {
   console.log('Board is ready!');
   
   // Create a new LED instance
   const led = new Led(13);

   // Blink the LED every 500ms
   led.blink(500);
});

Explanation

In the above code, we import the necessary modules from Johnny-Five, namely Board and Led. We then initialize a new Arduino board using new Board(). Once the board is ready, indicated by the ready event, we create a new LED instance using new Led(13) where 13 represents the pin to which the LED is connected.

Finally, we call the blink() method on the LED object to make the LED blink every 500 milliseconds.

Save the file and run it using Node.js:

node led-control.js

If everything is set up correctly, you should see the LED connected to pin 13 on the Arduino board blinking at regular intervals.

Toggling the LED Light

In the previous example, we explored how to blink the LED every 500 milliseconds. In this example, we will explore how to manually toggle the LED lights using the toggle() method.

const { Board, Led } = require('johnny-five');

const board = new Board();

board.on('ready', () => {
   console.log('Board is ready!');
   
   const led = new Led(13);

   // Toggle the LED every 1000ms
   setInterval(() => {
      led.toggle();
   }, 1000);
});

Explanation

This code creates a new instance of the Board object and a new instance of the Led class, passing the digital pin number (13) to the Led constructor. It then creates an interval that toggles the LED every 1000 milliseconds, creating an on/off pattern.

Advanced Examples

Controlling a Servo Motor

const { Board, Servo } = require('johnny-five');

const board = new Board();

board.on('ready', () => {
   console.log('Board is ready!');

   // Connect a servo motor to pin 9
   const servo = new Servo(9);

   // Sweep the servo between 0 and 180 degrees
   servo.sweep();
});

Reading Sensor Data

const { Board, Sensor } = require('johnny-five');

const board = new Board();

board.on('ready', () => {
   console.log('Board is ready!');

   // Connect a potentiometer to A0
   const sensor = new Sensor('A0');

   // Log sensor value changes
   sensor.on('change', () => {
      console.log(`Sensor value: ${sensor.value}`);
   });
});

Button-Controlled LED

This example demonstrates how to control an LED with a button. When the button is pressed, the LED turns on, and when released, the LED turns off.

const { Board, Button, Led } = require('johnny-five');

const board = new Board();

board.on('ready', () => {
   console.log('Board is ready!');
   
   const button = new Button(2); // Button connected to pin 2
   const led = new Led(13); // LED on pin 13

   button.on('press', () => {
      console.log('Button Pressed!');
      led.on();
   });

   button.on('release', () => {
      console.log('Button Released!');
      led.off();
   });
});

Variable LED Brightness

This example shows how to control LED brightness using a potentiometer. The sensor value is scaled to control the brightness level.

const { Board, Sensor, Led } = require('johnny-five');

const board = new Board();

board.on('ready', () => {
   console.log('Board is ready!');
   
   const sensor = new Sensor('A0'); // Potentiometer on analog pin A0
   const led = new Led(9); // LED on PWM-capable pin 9

   sensor.on('change', () => {
      const brightness = sensor.scaleTo([0, 255]); // Scale to LED brightness range
      led.brightness(brightness);
      console.log(`Sensor Value: ${sensor.value}, Brightness: ${Math.round(brightness)}`);
   });
});

Johnny-Five API Features

  • Analog Inputs ? Johnny-Five allows you to read analog input values from sensors connected to your Arduino board. You can use the Sensor class to read values from sensors such as light sensors, temperature sensors, or potentiometers.

  • Servo Motors ? With Johnny-Five, you can control servo motors to build robotic arms, pan-tilt systems, or any other project that requires precise motor control. The Servo class provides methods to control the position, speed, and range of motion.

  • Sensors and Actuators ? Johnny-Five supports a wide range of sensors and actuators, including proximity sensors, accelerometers, temperature sensors, motors, and more. You can easily integrate these components using the corresponding Johnny-Five classes.

  • Event Handling ? Johnny-Five leverages the event-driven nature of JavaScript to handle hardware events. You can listen for events such as button presses, sensor readings, or changes in hardware component states.

Common Issues and Troubleshooting

Board Not Recognized:

  • Ensure the Standard Firmata is uploaded to the Arduino
  • Check the USB connection and try a different cable or port
  • Verify the correct COM port is being used

Johnny-Five Errors:

  • Confirm that Node.js and Johnny-Five are correctly installed
  • Check that your Arduino is on the correct port (e.g., /dev/ttyUSB0 on Linux)
  • Ensure no other programs are using the Arduino's serial port

Advantages of Using Johnny-Five

  • Cross-Platform: Runs on macOS, Windows, and Linux
  • Easy to Learn: Uses JavaScript, making it accessible to web developers
  • Extensive Hardware Support: Works with various microcontrollers and components
  • Event-driven: Built on Node.js, making it suitable for asynchronous tasks
  • Large Community: Active community with extensive documentation and examples

Conclusion

Johnny-Five bridges the gap between web development and hardware control, making JavaScript robotics accessible to developers familiar with Node.js. With its intuitive API and extensive hardware support, you can quickly prototype IoT projects and robotic systems using familiar JavaScript syntax and event-driven programming patterns.

Updated on: 2026-03-15T23:19:01+05:30

575 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements