JavaScript Robotics: Controlling Hardware with Johnny-Five and Arduino


JavaScript is a powerful language that can be used for a wide variety of applications, including robotics. Johnny-Five is a JavaScript framework that makes it easy to control hardware devices with JavaScript. With Johnny-Five, you can use JavaScript to control Arduino boards, Raspberry Pis, and other hardware devices.

The world of robotics and hardware control has become increasingly accessible to developers in recent years. One of the most popular platforms for experimenting with robotics is Arduino, an open-source electronics platform based on easy-to-use hardware and software. Coupled with JavaScript, one of the most widely used programming languages, we have a powerful combination for creating interactive and intelligent devices.

In this tutorial, we will explore how to use Johnny-Five to control hardware devices. We will start by discussing the basics of Johnny-Five, and then we will show you how to use Johnny-Five to control an Arduino board.

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)

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 and Arduino

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.

Next, open your favourite text editor and create a new JavaScript file. Let's name it led-control.js. In this file, we will write the code to control the LED.

Consider the code shown below.

index.js

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

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

// When the board is ready
board.on('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 initialise 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 explore how we can blink the LED every 500 milliseconds, in the below example we will explore how we can toggle() the LED lights.

Consider the code shown below.

index.js

var five = require("johnny-five");

var board = new five.Board();

board.on("ready", function() {
   var led = new five.Led(13);

   setInterval(function() {
      led.toggle();
   }, 1000);
});

Explanation

This code will first create a new instance of the Board object. Then, it will create a new instance of the Led class, and it will pass the digital pin number (13) to the Led constructor. Finally, it will create a new interval that will toggle the LED every 1000 milliseconds.

Exploring the Johnny-Five API

Now that you have a basic understanding of how to control hardware using Johnny-Five and Arduino, let's explore some other features of the Johnny-Five API.

  • 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 of servo motors.

  • 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 into your projects 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 the state of a hardware component, and trigger actions accordingly.

  • Robotics Integration  Johnny-Five integrates seamlessly with robotics platforms such as ROS (Robot Operating System) and ROS2, allowing you to control robots using JavaScript.

Conclusion

JavaScript robotics with Johnny-Five and Arduino opens up a world of possibilities for developers to build interactive and intelligent devices. With its simple API and extensive hardware support, Johnny-Five makes it easy for JavaScript developers to enter the world of physical computing and robotics.

In this article, we explored the basics of JavaScript robotics using Johnny-Five and Arduino. We learned how to control an LED using JavaScript, and we touched on some of the other features and capabilities of the Johnny-Five API.

With Johnny-Five, you can unleash your creativity and build a wide range of projects, from home automation systems to remote-controlled robots. So grab your Arduino board, fire up your favorite code editor, and start exploring the exciting world of JavaScript robotics.

Updated on: 25-Jul-2023

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements