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)

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.

Example 1: 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();
});

Example 2: 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}`);
});
});

More Functionalities in Johnny-Five and Arduino

Controlling Multiple LEDs

Turn on/off multiple LEDs sequentially?

This example demonstrates how to control multiple LEDs by cycling through them sequentially. Each LED turns on and off in order, creating a chasing light effect.

const { Board, Button, Led } = require("johnny-five");
const board = new Board();

board.on("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();
  });
});

Reading a Button Press

Control an LED with a button?

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

const { Board, Sensor, Led } = require("johnny-five");
const board = new Board();

board.on("ready", () => {
  const sensor = new Sensor("A0"); // Potentiometer on analog pin A0
  const led = new Led(9); // LED on pin 9 (PWM pin)

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

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.

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.
Johnny-Five Error:
  • 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).

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.

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: 2024-12-09T21:56:02+05:30

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements