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:
- Download the Arduino IDE from http://arduino.cc/en/main/software.
- Connect your Arduino board to a USB port on your computer.
- Open the Arduino IDE and ensure the correct port is selected (e.g.,
/dev/cu.usbmodem14101
) under Tools -> Port. - Select the appropriate board under Tools -> Board (e.g., Arduino Uno).
- Open the File -> Examples -> Firmata menu and choose
StandardFirmataPlus
. - 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:
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:
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 onled.off()
to turn it offled.toggle()
to switch its current stateled.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!