DC Motor Control with Johnny-Five & Arduino

If you want to learn how to control DC motors using PWM with Johnny-Five and Arduino, you're in the right place. In this hardware control with Johnny-Five JavaScript tutorial, we will be using Johnny-Five, a popular JavaScript robotics library, along with an Arduino board to control the speed of a DC motor using pulse width modulation (PWM) and a potentiometer.

Before we dive into the code, it is encouraged to review two earlier tutorials: "DC motor speed control with Arduino PWM" and "Joystick controlled DC motor with Arduino and TIP122" These tutorials provide a solid foundation for understanding the basics of controlling DC motors with Arduino, including how to use PWM to adjust motor speed and how to interface with external components such as potentiometers and joysticks.

Now, let's move on to the current tutorial, where we will be using Johnny-Five, a powerful and easy-to-use robotics javascript programming language, to control a DC motor. First lets see the hardware setup. The following shows the circuit diagram of connecting DC motor with Arduino using 2N2222 bipolar transistor, 1N4007 diode, 0.1uF capacitor and 1KOhm resistor. The DC motor is connected to the pwm pin 9 of Arduino Uno. A 10KOhm potentiometer is connected to the analog pin A1 of Arduino Uno.

arduino dc motor on breadboard
arduino dc motor circuit diagram

The JavaScript program below utilizes Johnny-Five to communicate with Arduino and control DC motor using the potentiometer and other hardware components.

  var ard = require("johnny-five");
  var board = new ard.Board({port:"COM37"});

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

      this.analogRead(1, function(PWMvalue) {
      this.analogWrite(9, PWMvalue);
      console.log(PWMvalue);
    });
  });

The above Johnny-Five JavaScript program is explained below step by step:

  1. First, we require the "johnny-five" library and create a new board object, specifying the port to which the Arduino board is connected. In this example, the port is set to "COM37," but you may need to modify it to match the port of your Arduino board.
  2. Next, we use the "on" event handler to listen for the "ready" event, which indicates that the Arduino board is ready for communication.
  3. Inside the "ready" event handler, we use the "pinMode" function to set pin 9 as an output PWM pin. PWM is a technique that allows us to control the speed of the DC motor by varying the duty cycle of the PWM signal.
  4. We also use the "pinMode" function to set pin 1 as an input analog pin, assuming that a potentiometer is connected to this pin. The potentiometer will be used to control the speed of the DC motor.
  5. We then use the "analogRead" function to read the value of the potentiometer from pin 1. The "analogRead" function takes a callback function as an argument, which will be executed when the analog value is read from the Arduino.
  6. Inside the callback function, we use the "analogWrite" function to set the PWM value of pin 9 to the value of the potentiometer, which will control the speed of the DC motor. We also print the PWM value to the console for debugging purposes.

To run the code you have have firmata compiled and uploaded into Arduino and have installed Node.js on your PC. How to do this and setup is explained in Programming Arduino with Johnny-Five. To run the Javascript program code above you need to open a terminal and type in node followed by the name of the javascript file which in this case is pwm.js. So you will need to type the following in the terminal.

node pwm 

 Here Visual Studio Code IDE was used. It is free for download and the  above command was typed in the VSCode terminal. See the following video demonstration of running the Javascript program,  and rotating the potentiometer to run the DC motor in real time.


In conclusion, Johnny-Five is a powerful and user-friendly library that makes it easy to interface with Arduino and control various hardware components, including DC motors. By combining Johnny-Five with Arduino and external components such as potentiometer and DC motors you can create a wide range of robotics projects. In this JavaScript tutorial with Johnny-Five library, we showed how to implement DC motor speed control using Arduino.

References and Further Readings

[1] what is REPL in johnny five and what is its use?

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

[3] LED Blink Arduino Johnny-Five JavaScript Tutorial 

Post a Comment

Previous Post Next Post