Getting Started with NodeMCU and Johnny-Five: LED Blink

NodeMCU LED connection

NodeMCU is a popular development board based on the ESP8266 microcontroller, which is widely used for building Internet of Things (IoT) projects. It offers built-in Wi-Fi connectivity, making it ideal for creating smart devices that can be controlled remotely. Johnny-Five, on the other hand, is a JavaScript robotics framework that makes it easy to work with hardware devices, including the NodeMCU board, using the familiar syntax of JavaScript. The combination of JavaScript, Johnny-Five, and Arduino allows you to build a wide range of applications, from simple LED blinkers to complex robotics projects. Here, we will explore how to get started with NodeMCU, an open-source development board, and Johnny-Five, a JavaScript robotics framework, to control an LED and make it blink.


Before we dive into the fun part of blinking an LED with NodeMCU and Johnny-Five, here are a few prerequisites you will need:

  • Node.js and npm (Node Package Manager) installed on your computer. If this your first time. see tutorial Programming Arduino with Johnny-Five guides you how to setup Node.js and Johnny-Five.
  • A NodeMCU development board.
  • An LED and a resistor (around 220 Ohms).
  • Breadboard and jumper wires for connecting the components.

Step 1: Set up NodeMCU

The first step is to set up your NodeMCU development board. Follow these steps:

  1. Connect your NodeMCU board to your computer via a USB cable.
  2. Open the Arduino IDE
  3. Install supporting board packages and libraries. These files can be found in the following link.

    To install the board package and libraries open Arduino IDE > File > Preferences and copy paste the following link in the Additional Boards Manager URLs field.

  4. Go to the "Board Manager" in the "Tools" menu and install the ESP8266 boards.

esp8266 by ESP8266 community 

5.Select the appropriate NodeMCU board from the "Boards" menu. If you are have any problem in this tep see tht tutorial LED blink using ESP8266 & Arduino which guides you through this step with video and pictures.

Step 2: Upload StandardFirmataWiFi to NodeMCU

Here are the steps to install StandardFirmataWiFi into NodeMCU:

1. In the Arduino IDE, open the "StandardFirmataWiFi" from File>Examples>Firmata.

2.In the StandardFirmataWiFi.ino file, uncomment the following line(line 85):  #define SERIAL_DEBUG

3. When the "StandardFirmataWiFi" is opened, you will also see the wifiConfig.h file. 
a. In the wifiConfig.h file, look for the following lines of code:
b. On line 119, enter the name of your WiFi network, char ssid[] = "your_network_name"; replace your_network_name with your WiFi network name.
c. One line 186, enter the password of your WiFi network, char ssid[] = "your_wpa_passphrase"; replace your_wpa_passphrase with your WiFi network password.
4. Connect your NodeMCU board to your computer via a USB cable. Make sure the correct board and port are selected in the "Boards" and "Port" menu in the Arduino IDE. Click on the "Upload" button to upload the StandardFirmataWiFi sketch to your NodeMCU board.

5. Once the upload is complete, open the Serial Monitor in the Arduino IDE. Set the baud rate to 9600. Reset your NodeMCU board by pressing the "RST" button. You should see the output in the Serial Monitor showing the IP address of your NodeMCU, indicating that the StandardFirmataWiFi sketch has been successfully uploaded to your NodeMCU board. Note down the IP address because this is required in the following step.

Nodemcu wifi

Now the NodeMCU can transfer data over Wi-Fi and the NodeMCU board be controlled wirelessly from your JavaScript code.

Step 3: Install Johnny-Five

Next, we need to install Johnny-Five, the JavaScript robotics framework, to interact with the NodeMCU board. Follow these steps:

1. Open a command prompt or terminal window on your computer.

2. Navigate to the directory where you want to create your NodeMCU project.

3. Run the following command to install Johnny-Five globally:

npm install -g johnny-five

This will install Johnny-Five as a global package, making it accessible from any Node.js script. Futher help for this step can be found in the tutorial.

Step 4: Install etherport-client

To communicate with NodeMCU using Node.js and Johnny-Five JavaScript library we need to install the etherport-client via npm. On your terminal install it with the following command.

npm install etherport-client -g

This will install etherport-client javascript globally.

Step 5: Connect the LED to NodeMCU 

Now, let's connect the LED to the NodeMCU board. See the Circuit diagram below.

NodeMCU LED connection
 The LED is connected to D5 pin which is GPIO14.

Step 4: Write the Code 

Now it's time to write the code to make the NodeMCU connected LED blink using Johnny-Five. Following is the Johnny-Five and etherport-client libraries based JavaScript program to blink a LED connected to GPIO14(D5) pin on NodeMCU.

  const {EtherPortClient} = require('etherport-client');
    const five = require('johnny-five');
    const board = new five.Board({
      port: new EtherPortClient({
        host: '',
        port: 3030
      repl: false

  board.on("ready", function(){

      var state = 0;
      var led = 14;  //GPIO14:D5 pin

      this.pinMode(led, this.MODES.OUTPUT);
          this.digitalWrite(led, (state ^= 1));

Let's break down the JavaScript program code step by step:

  1. Importing required libraries:
const {EtherPortClient} = require('etherport-client'); const five = require('johnny-five');

This code imports the EtherPortClient module from the etherport-client library, and the five module from the johnny-five library. The EtherPortClient module is used to establish a connection to an Arduino board over Wi-Fi using the Firmata protocol, while the five module is a popular JavaScript framework for working with Arduino boards.

  1. Creating a new board instance:
const board = new five.Board({ port: new EtherPortClient({ host: '', port: 3030 }), repl: false });

This code creates a new Board instance using the five library, and configures it to communicate with an Arduino board over Wi-Fi using the EtherPortClient module. The host and port options specify the IP address and port number of the Arduino board, respectively. The repl option is set to false to disable the Read-Eval-Print Loop (REPL) functionality on the board.

  1. Handling the "ready" event:
board.on("ready", function(){ // Code here });

This code attaches an event listener to the "ready" event of the Board instance, which is triggered when the board is ready for communication.

  1. Configuring and controlling the LED:
var state = 0; var led = 14; //GPIO14:D5 pin this.pinMode(led, this.MODES.OUTPUT); this.loop(500,()=>{ this.digitalWrite(led, (state ^= 1)); });

This code sets up a basic blinking LED example. It declares a variable state to keep track of the LED state (either on or off), and sets the pin number for the LED to 14, which corresponds to the D5 pin on the NodeMCU board. It then configures the pin mode to OUTPUT using this.pinMode() method, and sets up a loop to toggle the LED state every 500ms using this.loop() method. The this.digitalWrite() method is used to set the pin state to either HIGH or LOW, toggling the LED on and off.

Overall, this code sets up a connection to an Arduino board over Wi-Fi, configures and controls an LED on the board to blink at a frequency of 500ms, using the EtherPortClient and johnny-five libraries in a Node.js environment.

Give the JavaScript program some name and save it, like nodemcu.js.

Step 5: Running the Javascript Program

The final step is to run the above led blink JavaScript program nodemcu.js. To do that, in the terminal issue the following command.

node nodemcu

where nodemcu is the name of your JavaScript program.

If everything proceeded smoothly, then the LED should be blinking. See the video demonstration below which shows this.


In this way you can get started with NodeMCU and Johnny-Five to control an LED and make it blink. This is just the beginning of what you can do with NodeMCU and Johnny-Five. You can explore more advanced projects, such as reading sensor data, controlling motors(see Nodemcu PWM controlled DC motor), and interacting with other hardware components, to create your own IoT devices[1][2].

Remember to always be cautious when working with hardware components and follow proper safety measures. Double-check your connections and make sure you are using the correct pins and resistors to avoid damaging your components or board.

We hope this NodeMCU with Johnny-Five JavaScript tutorial has been helpful in getting you started with NodeMCU and Johnny-Five. 

References and Further Readings:

[1] Node-Red ESP8266 MQTT Subscribe Example

[2] Node-Red ESP8266 MQTT Publish Example

Post a Comment

Previous Post Next Post