Servo Control with Johnny-Five and Arduino

 In this tutorial, we will be using the Johnny-Five library, a popular JavaScript robotics framework, along with an Arduino board to control a servo motor. If you're new to JavaScript programming, don't worry - this tutorial is beginner-friendly and will walk you through the basics of using Johnny-Five to interact with hardware components. You'll learn how to set up your Arduino board, wire up the servo motor, and write JavaScript code to control its movement. By the end of this tutorial, you'll have a solid understanding of how to use JavaScript to control servo motors and other hardware components. So whether you're an experienced JavaScript developer or just starting to learn JavaScript, this tutorial is a great way to get started with robotics and physical computing. 

 Before diving into this tutorial, it's highly recommended that you check out our previous Johnny-Five JavaScript tutorials, "Getting Started with NodeMCU and Johnny-Five", "DC Motor Control with Johnny-Five & Arduino" and Servo Motor Control with Motor Shield and Arduino. By exploring these previous tutorials(see also the references[1][2]), you'll have foundation for understanding the concepts covered in this tutorial. So if you're new to using Johnny-Five with Arduino or just need a refresher, be sure to check out these resources first!

Basic of Servo Control with Johnny-Five and Arduino

The basic Johnny-Five JavaScript program code to control a servo motor which is connected to pin 10 of Arduino Uno is below.

 
  const {Board, Servo} = require("johnny-five");
  const board = new Board();

  board.on("ready", () => {
    const servo = new Servo(10);
    servo.sweep();
  });


In the first two lines, the code imports the necessary modules, "Board" and "Servo", from the Johnny-Five library using destructuring assignment. Then, the code creates a new instance of the Board object.

The code sets up an event listener that runs a function when the board is ready. Within this function, the code creates a new Servo object and assigns it to the variable "servo". The number 10 passed to the Servo constructor represents the pin number to which the servo is connected on the Arduino board. You should replace this with the actual pin number that you are using for your servo.

Finally, the code calls the sweep() method on the servo object. This method makes the servo motor rotate back and forth between its minimum and maximum positions in a continuous motion.

Overall, this code initializes a servo motor connected to an Arduino board and makes it continuously sweep between its minimum and maximum positions. It serves as a basic starting point for controlling a servo motor using Johnny-Five and can be customized for more complex servo control applications.

basic servo control
 

Maximum and Minimum Position Servo Control with Johnny-Five and Arduino

The maximum and minimum position of the servo can be controlled using the max() and min() functions. The following is the JavaScript program that turns the servo to its maximum and minimum position 

 
  const {Board, Servo} = require("johnny-five");
  const board = new Board();

  board.on("ready", () => {
    const servo = new Servo(10);
    let currentPosition = -90; // -90 = minimum position, +90 = maximum position

    setInterval(() => {
      if (currentPosition === -90) {
        servo.max();
        currentPosition = +90;
      } else {
        servo.min();
        currentPosition = -90;
      }
    }, 2000); // repeat the loop every 2 seconds (2000 milliseconds)
  });
 

 This Javascript program code uses the Johnny-Five library to control a servo motor connected to an Arduino board, but it employs a more complex servo control pattern than the basic sweep motion in the previous code.

As in the previous code, the first two lines import the necessary modules and create a new Board object. The code sets up an event listener that runs a function when the board is ready.

Within this function, the code creates a new Servo object and assigns it to the variable "servo". The number 10 passed to the Servo constructor represents the pin number to which the servo is connected on the Arduino board.

The code also initializes a variable called "currentPosition" with the value of -90, which corresponds to the minimum position of the servo motor.

The code then sets up a setInterval() function, which runs repeatedly at a specified interval of 2 seconds (2000 milliseconds).

Within the setInterval() function, the code checks the current position of the servo motor, which is stored in the "currentPosition" variable. If the position is -90 (minimum), the code calls the max() method on the servo object to move the motor to its maximum position of +90 degrees. It also updates the "currentPosition" variable to +90.

If the current position is not -90, the code assumes that the motor is already in the maximum position of +90 degrees, so it calls the min() method on the servo object to move the motor back to its minimum position of -90 degrees. It then updates the "currentPosition" variable to -90.

This process of switching the servo motor back and forth between its minimum and maximum positions is repeated every 2 seconds.

Overall, this code creates a simple loop that makes the servo motor move back and forth between its minimum and maximum positions repeatedly. It can be used as a starting point for more complex servo control applications that require precise movement or positioning.

max and min position servo


Desired Position Servo Control with Johnny-Five and Arduino

The next method to control a servo motor with Johnny-Five Javascript library is to set the servo position to any desired position. This is done using the .to() method as illustrated by the following JavaScript program.

   
const { Board, Servo, Sensor } = require("johnny-five");
  const board = new Board();

  board.on("ready", () => {
    const servo = new Servo({
      pin: 10,
      range: [-90, 90] // set the range of motion to 0-180 degrees
    });
   
    const potValue = new Sensor("A1"); // create a new Sensor object for the potentiometer
   
    potValue.on("change", function() {
      const deg = this.scaleTo(-90, 90); // map the potentiometer value to the servo angle range
     
      servo.to(deg); // set the servo position based on the potentiometer value
    });
  });
 

This is a code snippet that uses the Johnny-Five JavaScript framework to control a servo motor based on the input from a potentiometer.

First, the code imports the necessary components from the Johnny-Five library:

 const { Board, Servo, Sensor } = require("johnny-five");

 The Board component is used to establish a connection with the Arduino board that controls the servo and potentiometer.

Then, a new Board object is created:

  const board = new Board();

Once the board is ready, the code creates a new Servo object and a new Sensor object for the potentiometer:

  board.on("ready", () => {
    const servo = new Servo({
      pin: 10,
      range: [-90, 90] // set the range of motion to -90 to +90 degrees
    });
   
    const potValue = new Sensor("A1"); // create a new Sensor object for the potentiometer

The Servo object is configured to use pin 10 on the Arduino board and to have a range of motion from -90 to 90 degrees.

The Sensor object is configured to read the analog input from pin A1 on the Arduino board.

Finally, the code sets up an event listener for changes to the potentiometer value:

    potValue.on("change", function() {
      const deg = this.scaleTo(-90, 90); // map the potentiometer value to the servo angle range
     
      servo.to(deg); // set the servo position based on the potentiometer value
    });
  });

 Whenever the potentiometer value changes, the code maps the value to the range of motion of the servo and sets the servo position accordingly using the to() method of the Servo object.

potentiometer controlled servo
 In summary, this code controls a servo motor based on the input from a potentiometer using the Johnny-Five JavaScript framework.

REPL Servo Control with Johnny-Five and Arduino

 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. Here it is illustrated how Johnny-Five REPL can be used to control servo motor.

Following is the Johnny-Five JavaScript program code that uses the Johnny-Five JavaScript framework to control a servo motor connected to an Arduino board. The servo is set up to move between 0 to 180 degrees, and the servo object is added to the REPL for interactive control.

  const { Board, Servo} = require("johnny-five");
  const board = new Board();

  board.on("ready", () => {
    const servo = new Servo({
      pin: 10,
      range: [0, 180] // set the range of motion to 0 to 180 degrees
    });

      // Add servo to REPL
  board.repl.inject({
    servo
  });

  });

The first line of the code imports the Board and Servo components from the Johnny-Five library using require("johnny-five"):

const { Board, Servo} = require("johnny-five");

 The Board component is used to establish a connection with the Arduino board that controls the servo, while the Servo component is used to control the servo motor itself.

The second line of the code creates a new Board object:

 const board = new Board();

This initializes the connection to the Arduino board.

The code then waits for the board to be "ready" before proceeding with the servo motor control. This is achieved using the board.on() method:

  board.on("ready", () => {
 
    });

 Inside the "ready" event listener, a new Servo object is created:

    const servo = new Servo({
      pin: 10,
      range: [0, 180] // set the range of motion to 0 to 180 degrees

This code sets up a new Servo object that is connected to the digital pin 10 on the Arduino board, and sets the range of motion of the servo to be from 0 to 180 degrees.

Finally, the code adds the servo object to the REPL, which is a command-line interface that allows for interactive testing and control of the Arduino board. This is achieved using the board.repl.inject() method:

  // Add servo to REPL
  board.repl.inject({
    servo
  });

This allows you to control the servo motor from the REPL by calling methods on the servo object.

RELP servo control

The following is video demonstration of how servo motor can be control with Johnny-Five and Arduino.



References and Further Readings:

[1] Servo Motor control using Simulink and Arduino

[2] Programming Arduino with Johnny-Five

Post a Comment

Previous Post Next Post