/

Johnny Five Tutorial: Learn How to Interface with Electronic Devices using JavaScript

Johnny Five Tutorial: Learn How to Interface with Electronic Devices using JavaScript

Johnny Five is a fantastic library that allows developers to communicate with electronic devices using JavaScript. While devices like Arduino are typically programmed in the Arduino Language, Johnny Five enables us to interface with Arduino using JavaScript, particularly in Node.js.

Setting up your Arduino to work with Johnny Five

To get started, follow these steps:

  1. Download the Arduino IDE from http://arduino.cc/en/main/software.
  2. Connect your Arduino board to a USB port on your computer.
  3. Open the Arduino IDE and ensure the correct port is selected (e.g., /dev/cu.usbmodem14101) under Tools -> Port.
  4. Select the appropriate board under Tools -> Board (e.g., Arduino Uno).
  5. Open the File -> Examples -> Firmata menu and choose StandardFirmataPlus.
  6. Click the right arrow icon on the toolbar to compile and load the program onto the Arduino board.

Please note that the Arduino device must remain connected to the computer in order to use Johnny Five.

Johnny Five Functionality Overview

Johnny Five provides access to various APIs for controlling electronic components such as LEDs, buttons, sensors, servo motors, stepper motors, thermometers, LCD screens, joysticks, gyroscopes, accelerometers, and more.

To utilize Johnny Five, install the johnny-five npm package by running npm install johnny-five. Here’s an example of initializing a board and waiting for it to become available:

1
2
3
4
5
6
const { Board } = require('johnny-five');
const board = new Board();

board.on('ready', () => {
// Board is ready!
});

While the entire API can be found at http://johnny-five.io/api, let’s focus on an example of working with an LED.

To control an LED, obtain the Led class from the library and create a new Led object by specifying the pin number:

1
2
3
const { Led } = require('johnny-five');
// ...
const led = new Led(13);

Once you have the led object, you can use its methods:

  • led.on() to turn it on
  • led.off() to turn it off
  • led.toggle() to switch its current state
  • led.blink() to make it blink indefinitely (default interval is 100ms)
  • led.stop() to stop the blinking

This is just the beginning of what you can do with Johnny Five. Stay tuned for the next tutorial, where we’ll delve further into its usage!

tags: [“JavaScript”, “Johnny Five”, “Arduino”, “Node.js”, “IoT”, “Electronics”]