Arduino, a popular open-source hardware platform, allows you to
create interactive projects that combine hardware and software.
Johnny-Five, a JavaScript library for Node.js, makes it easy to control
Arduino hardware using JavaScript. In this Javscript tutorial, you will
learn javascript hardware programming language Johnny-Five to blink a
LED connected to Arduino Uno.
Prerequisites
- Basic knowledge of Arduino hardware and concepts.
- Familiarity with JavaScript programming language.
- Arduino board with an LED and a resistor connected to a digital pin.
The circuit used here is show below where the LED is connected to digital pin 9 via 220Ohm resistor.
The above picture also shows visual studio code javascript code.
Setting Up the Environment
- Install Node.js and npm (Node Package Manager) on your computer.
- Install the Johnny-Five library by running the following command in your project directory: npm install johnny-five
- Connect your Arduino board to your computer and upload the Firmata firmware to it using the Arduino IDE or other compatible software.
These steps are explained in the following tutorials.
- Programming Arduino with Johnny-Five in Proteus
- Getting Started with Johnny-Five in Proteus
Writing the Code
There are many ways we can write Johnny-Five javascript code for LED blink and one of them is the following.
var five = require("johnny-five");
var board = new five.Board();
board.on("ready", function() {
var state = 0;
var led = 9;
this.pinMode(led, this.MODES.OUTPUT);
this.loop(500, () => {
this.digitalWrite(led, (state ^= 1));
});
});
Let's go through the javascript code step by step:
var five = require("johnny-five");
: This line imports the Johnny-Five library, which is a popular JavaScript library for controlling Arduino hardware. Therequire()
function is used to load the "johnny-five" module, and the returned object is stored in thefive
variable.var board = new five.Board();
: This line creates a newBoard
instance from thefive
module, which represents the Arduino board that we want to control.board.on("ready", function() { ... });
: This sets up an event listener for the "ready" event of theBoard
instance. The "ready" event is emitted when the Arduino board is ready for communication.var state = 0;
: This declares a variablestate
and initializes it with the value0
. This variable will be used to keep track of the current state of the LED (either ON or OFF).var led = 9;
: This declares a variableled
and assigns it the value9
, which represents the pin number where the LED is connected to the Arduino board.this.pinMode(led, this.MODES.OUTPUT);
: This sets the mode of the pin specified by theled
variable to "OUTPUT" using thepinMode()
method of theBoard
instance. This is necessary to configure the pin as an output pin so that we can control the LED connected to it.this.loop(500, () => { ... });
: This sets up a loop that runs every 500 milliseconds (0.5 seconds) using theloop()
method of theBoard
instance. The arrow function inside the loop is the callback function that will be executed on each iteration of the loop. See also Johnny-Five wait() vs loop().this.digitalWrite(led, (state ^= 1));
: This line toggles the state of the LED connected to theled
pin using thedigitalWrite()
method of theBoard
instance. The(state ^= 1)
expression uses the XOR bitwise operator to toggle the value ofstate
between0
and1
. This will cause the LED to blink on and off every time the loop iterates.
In summary, this JS code sets up an event listener for the "ready" event of the Arduino board, configures a pin as an output pin, and then sets up a loop that toggles the state of an LED connected to that pin, causing it to blink on and off every 0.5 seconds.
Watch the following video tutorial for this LED blink Arduino Johnny-Five Javascipt tutorial.
Conclusion
In this Johnny-Five JavaScript tutorial, we learned how to blink an LED with Arduino using Johnny-Five, a powerful and flexible JavaScript library for controlling Arduino hardware. We covered the setup process, code structure, and basic usage of Johnny-Five's Led class to control an LED connected to an Arduino board. With this knowledge, you can now start building your own Arduino projects with JavaScript and Johnny-Five, and explore the endless possibilities of hardware and software integration. Happy tinkering!
References