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

PWM (Pulse Width Modulation) signals are a powerful tool in electronics as they allow for precise control of devices such as LEDs, motors, and servos. In this Arduino Johnny-Five JavaScript tutorial, we will explore how to use Johnny-Five, a popular JavaScript library for controlling Arduino boards, to generate and control PWM signals. We will walk through, step-by-step, Johnny-Five JavaScript programming of how to read an analog input from a potentiometer and use that value to generate a PWM signal with varying duty cycle on an output pin of an Arduino board.


If you're new to Johnny-Five and Arduino, we recommend checking out some of our previous tutorials for a solid foundation. In our "LED Blink Arduino Johnny-Five JavaScript Tutorial" we walk you through the basics of controlling an LED with Johnny-Five and Arduino, including setting up pin modes, writing digital outputs, and creating simple blinking patterns. Additionally, our "Reading Push Button or Switch with Johnny-Five & Arduino" tutorial covers how to read digital inputs from push buttons or switches using Johnny-Five and Arduino, providing insights into working with digital inputs and event handling. These tutorials will give you a good understanding of the fundamentals and prepare you for more advanced projects, such as generating PWM signals as shown in this blog post.

Before diving into the tutorial, let's make sure we have everything we need:

  • An Arduino board (e.g., Uno, Mega, etc.)
  • A computer with Node.js and npm (Node Package Manager) installed. Here we will use Proteus and Visual Studio Code IDE. See the tutorials Programming Arduino with Johnny-Five in Proteus and Getting Started with Johnny-Five in Proteus for setup and installation of Node.js, Johnny-Five JavaScript library etc needed to follow this tutorial.
  • Basic knowledge of JavaScript and electronics concepts, such as PWM and analog input/output

PWM with Johnny-Five JavaScript

 Pulse Width Modulation (PWM) is a powerful technique for controlling the intensity of electrical signals and is commonly used in robotics, automation, and electronics projects. With Johnny-Five JavaScript library, PWM becomes accessible and easy to implement with Arduino. In this Johnny-Five JavaScript tutorial, we will explore how to generate PWM signals using Johnny-Five JavaScript library and Arduino, allowing you to control the duty cycle and frequency of electrical signals. Whether you're a beginner or an experienced developer, understanding PWM with Johnny-Five can open up a whole new world of possibilities for your electronics, automation and IoT projects. 

Circuit Diagram

For the demonstration of PWM signal generation we will use the following circuit implementation. PWM signal is generated on Arduino Uno pin 6 and the PWM signal duty cycle is controlled using the 10KOhm potentiometer connected to analog pin A1.

PWM Johnny-Five Arduino Circuit Diagram

Connect your Arduino board to your computer using a USB cable. Then, connect a potentiometer to pin A1 on the Arduino board. Connect the middle pin of the potentiometer to A1, and connect the other two pins to 5V and GND on the Arduino board, respectively. This will allow us to read analog values from the potentiometer, which will be used to generate the PWM signal.

PWM JavaScript Code

The following is JavaScript code example that uses Johnny-Five library to generate PWM signal on pin 6 with varying PWM duty cycle controlled using 10KOhm potentiometer which is connected to A1 analog pin of Arduino Uno.

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

board.on("ready", function() {  
    // pin 6 is output pwm pin
    this.pinMode(6, ard.Pin.PWM);
    // Assuming potentiometer is attached to pin "A1"
    this.pinMode(1, ard.Pin.ANALOG);

    this.analogRead(1, function(voltage) {
    this.analogWrite(6, voltage);

Here's a breakdown of what the Johnny-Five JavaScript code does:

  • var ard = require("johnny-five");: This line imports the Johnny-Five library and assigns it to the variable ard.

  • var board = new ard.Board();: This creates a new Johnny-Five Board object, which represents the connection to the Arduino board.

  • board.on("ready", function() {...});: This sets up an event listener for the "ready" event of the Arduino board. The callback function inside the event listener will be executed once the board is ready.

  • this.pinMode(6, ard.Pin.PWM);: This sets pin 6 on the Arduino board as a PWM (Pulse Width Modulation) output pin using the pinMode method. This means that pin 6 will be capable of outputting analog signals with varying duty cycles.

  • this.pinMode(1, ard.Pin.ANALOG);: This sets pin 1 on the Arduino board as an analog input pin using the pinMode method. This means that pin 1 will be able to read analog values from connected sensors, such as a potentiometer.

  • this.analogRead(1, function(voltage) {...});: This sets up an event listener for reading the analog value from pin 1 on the Arduino board using the analogRead method. The callback function inside the event listener will be executed whenever a new analog value is read from the pin.

  • this.analogWrite(6, voltage);: This sets the PWM duty cycle of pin 6 on the Arduino board using the analogWrite method. The value of voltage read from pin 1 is used to set the duty cycle of the PWM signal on pin 6, effectively controlling the brightness or intensity of an output device connected to pin 6, such as an LED or a motor.

  • console.log(voltage);: This prints the read analog value to the console for debugging or monitoring purposes.

Overall, the code sets up an Arduino board with pin 6 as a PWM output pin, reads an analog value from pin 1, and uses that value to set the duty cycle of the PWM signal on pin 6, controlling the output device connected to pin 6 accordingly.

Video demonstration

Watch the following video which demonstrates Johnny-Five JavaScript programming with Visual Studio Code(VSCode), serial monitor message and fading of LED due to PWM signal.

Application of Johnny-Five PWM with Arduino

Johnny-Five, a JavaScript robotics and IoT platform for Arduino, provides powerful PWM (Pulse Width Modulation) capabilities that can be used in various applications. Here are some examples of how Johnny-Five PWM can be utilized in different projects:

  • "DC motor Speed control with Potentiometer and PWM using Arduino": PWM can be used to control the speed of a DC motor by varying the duty cycle of the PWM signal. Johnny-Five's PWM features can be used to read the value of a potentiometer, which acts as an input to control the speed of the motor using PWM. This can be used in projects such as robotics, automation, or motorized systems where precise control of motor speed is required.

  • "PWM - Programming Arduino using Matlab": Johnny-Five allows integrating Arduino with MATLAB, a popular numerical computing environment. PWM can be used to communicate between MATLAB and Arduino to control various aspects of Arduino-based projects, such as motor speed, LED brightness, or other actuators. This can be useful in applications where real-time data processing and control are required, such as data acquisition, signal processing, or control systems.

  • "Speed and direction control of DC motor using Arduino Fast PWM": Johnny-Five's PWM capabilities can be used to implement fast PWM signals for controlling the speed and direction of a DC motor. By manipulating the PWM duty cycle and frequency, the speed and direction of the motor can be controlled with high precision. This can be used in projects such as robotics, automation, or electric vehicle systems where precise motor control is required.

In summary, Johnny-Five's PWM capabilities can be used in a wide range of applications, including motor speed control, data communication with external software, and direction control of motors. Its flexibility and versatility make it a powerful tool for prototyping and building various IoT and robotics projects with Arduino. Next learn how to use PWM to control the speed of a DC motor using Johnny-Five and Arduino in the javascript tutorial DC Motor Control with Johnny-Five & Arduino.

Post a Comment

Previous Post Next Post