ad space

LED Blink Arduino Johnny-Five JavaScript Tutorial

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.

Arduino Johnny-Five LED Blink with VSCode

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:

  1. var five = require("johnny-five");: This line imports the Johnny-Five library, which is a popular JavaScript library for controlling Arduino hardware. The require() function is used to load the "johnny-five" module, and the returned object is stored in the five variable.

  2. var board = new five.Board();: This line creates a new Board instance from the five module, which represents the Arduino board that we want to control.

  3. board.on("ready", function() { ... });: This sets up an event listener for the "ready" event of the Board instance. The "ready" event is emitted when the Arduino board is ready for communication.

  4. var state = 0;: This declares a variable state and initializes it with the value 0. This variable will be used to keep track of the current state of the LED (either ON or OFF).

  5. var led = 9;: This declares a variable led and assigns it the value 9, which represents the pin number where the LED is connected to the Arduino board.

  6. this.pinMode(led, this.MODES.OUTPUT);: This sets the mode of the pin specified by the led variable to "OUTPUT" using the pinMode() method of the Board instance. This is necessary to configure the pin as an output pin so that we can control the LED connected to it.

  7. this.loop(500, () => { ... });: This sets up a loop that runs every 500 milliseconds (0.5 seconds) using the loop() method of the Board 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().

  8. this.digitalWrite(led, (state ^= 1));: This line toggles the state of the LED connected to the led pin using the digitalWrite() method of the Board instance. The (state ^= 1) expression uses the XOR bitwise operator to toggle the value of state between 0 and 1. 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

[1]  Arduino LED blinking using Matlab

[2] Arduino Nano LED blink  Code

Post a Comment

Previous Post Next Post