what is REPL in johnny five and what is its use?

REPL stands for "Read-Eval-Print Loop" It is a feature of the Johnny-Five JavaScript library that provides an interactive command-line interface for working with hardware devices like Arduino boards in real-time.

When you run a Johnny-Five JavaScript program, it opens a REPL session that allows you to send and receive commands to and from the Arduino board interactively. You can use the REPL to execute JavaScript code and control the hardware devices such as REPL servo control connected to the Arduino board in real-time without having to restart the entire program.

The REPL in Johnny-Five is useful for several purposes, including:

  1. Quick prototyping and testing: You can use the REPL to send commands to the Arduino board and see the immediate results, making it easy to experiment with different hardware configurations or test specific functionalities before integrating them into your main Johnny-Five program.
  2. Debugging and troubleshooting: The REPL allows you to inspect the state of the Arduino board, read sensor values, and send commands to troubleshoot issues or verify the expected behavior of your hardware devices.
  3. Learning and exploration: The REPL provides an interactive learning environment for understanding how the Johnny-Five library works, experimenting with different JavaScript commands and functions, and gaining hands-on experience with Arduino hardware.

Johnny-Five REPL code example

The following code is an example of using the Johnny-Five library REPL to interact with an Arduino board connected to the computer via a specific serial port ("COM37" in this case) in real time.

var five = require("johnny-five");
var board = new five.Board(

board.on("ready", function() {
  var led = new five.Led(6);

  // Start the REPL session
    // You can define any custom functions or variables that you want to use in the REPL
    myled: led

Here's a breakdown of the code:

  1. var five = require("johnny-five");: This line includes the Johnny-Five library in the Node.js program.

  2. var board = new five.Board({ port:"COM37" });: This line creates a new Board object, which represents the Arduino board. The port property is set to specify the serial port to which the Arduino board is connected. In this example, it is set to "COM37". You may need to change this value to match the serial port of your specific Arduino board.

  3. board.on("ready", function() { ... });: This line sets up an event listener for the "ready" event, which is emitted when the Arduino board is ready to receive commands. The callback function provided will be executed when the "ready" event is triggered.

  4. var led = new five.Led(6);: Inside the "ready" event callback function, a new Led object is created, representing an LED connected to pin 6 of the Arduino board.

  5. this.repl.inject({ myled: led });: This line injects a custom variable myled into the REPL (Read-Eval-Print Loop) session, which allows you to interactively control the LED from the REPL. In this case, the LED object created in the previous step is assigned to the myled variable, so you can directly control it from the REPL by typing myled.on(), myled.off(), etc.

For demonstration we will use visual code IDE to perform interactive real time control of the LED using the above REPL code.

REPL Johnny-Five

To run the code we use the following command on the VSCode terminal:

 node REPL

where, REPL.js is the name of the javascript file above.

Watch the following video demonstration of how to use REPL in johnny five for interactive LED control.

Overall, the REPL in Johnny-Five JavaScript library is a powerful tool for interactively working with Arduino boards, prototyping, testing, and debugging hardware interactions using JavaScript. The example javascript code sets up a connection to an Arduino board via a specific serial port, creates an LED object, and injects it into the REPL for interactive control. It demonstrates how Johnny-Five can be used to communicate with an Arduino board and interactively control its components using JavaScript programming language.

Reference and Further Readings

[1] LED Blink Arduino Johnny-Five JavaScript Tutorial 

[2] Reading Push Button or Switch with Johnny-Five & Arduino 

[3] PWM with Johnny-Five and Arduino: A Beginner's Guide

Post a Comment

Previous Post Next Post