ad space

Stepper Motor Control with Johnny-Five and Arduino

 Stepper motors are widely used in various applications, ranging from robotics to industrial automation. In this tutorial, we will learn how to control a stepper motor using a Javascript program with Johnny-Five and an Arduino board.

Johnny-Five is a popular Javascript library for controlling hardware, including Arduino boards. It provides an easy-to-use interface that allows users to interact with sensors, motors, and other hardware components using Javascript code.

In this tutorial, we will cover the basics of stepper motor control with Johnny-Five and Arduino. We will explain the code step by step, and show you how to wire and configure the stepper motor with your Arduino board. This tutorial assumes that you have some basic knowledge of programming Arduino with Johnny-Five. If you are new to Javascript, we recommend that you take some time to learn Javascript before proceeding with this tutorial.

Advantages of Using Johnny-Five

Johnny-Five provides a simple and elegant way to control hardware using Javascript. It allows you to write hardware control programs in the same language that you use to write web applications, which makes it easy to learn and use.

Learning Javascript & Johnny-Five

If you are new to Johnny-Five, this tutorial is a great way to get started. We will explain the basics of using Johnny-Five to control hardware, and show you how to wire and configure a stepper motor with your Arduino board. In addition to learning how to control a stepper motor with Johnny-Five, this tutorial also serves as a Javascript tutorial. We will explain the Javascript code step by step, so that you can understand how it works and how to modify it to suit your needs. If you are interested in learning more about hardware control with Johnny-Five and Arduino, we recommend that you check out our previous tutorials on NodeMCU and L293D DC motor control, and Servo control with Johnny-Five and Arduino.

Stepper Motor, L298N and Arduino Circuit Diagram

The L298N is a popular dual H-bridge motor driver IC that can be used to control DC motors, stepper motors, and other actuators. It can handle up to 2 amps of current per channel and can drive motors with voltages up to 46V. The L298N has four input pins (en1, en2, en3, and en4) that can be used to enable or disable the H-bridges, and four output pins (out1, out2, out3, and out4) that are used to control the direction of the motor.

To interface the L298N with an Arduino board, you can connect the four input pins to any digital output pins on the Arduino board (in this case, pins 10, 11, 12, and 13), and the four output pins to the corresponding inputs on the motor or actuator. You will also need to connect a power source (such as a battery or a power supply, here +12V) to the L298N to power the motor or actuator. Once you have wired everything up correctly, you can use an Arduino program to control the direction and speed of the motor by toggling the input pins. See the circuit diagram of interfacing Arduino with Stepper motor using L298N stepper motor driver IC.

arduino l298n IC stepper circuit

Programming Stepper Motor with Johnny-Five

Note that before you can run the stepper motor with Johnny-Five you need to use the AdvancedFirmata which is available at https://github.com/soundanalogous/AdvancedFirmata.

Below is the Node.js script that uses the Johnny-Five library to control a stepper motor connected to an Arduino board. 

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

    board.on("ready", () => {
    /**
     * In order to use the Stepper class, your board must be flashed with
     * either of the following:
     *
     * - AdvancedFirmata https://github.com/soundanalogous/AdvancedFirmata
     * - ConfigurableFirmata https://github.com/firmata/arduino/releases/tag/v2.6.2
     *
     */

    const stepper = new Stepper({
        type: Stepper.TYPE.FOUR_WIRE,
        stepsPerRev: 200,
        pins: {
        motor1: 10,
        motor2: 11,
        motor3: 12,
        motor4: 13
        }
    });

    // set stepp[er to 180 rpm, CCW, with acceleration and deceleration
    stepper.rpm(180).direction(Stepper.DIRECTION.CCW).accel(1600).decel(1600);
   
    // make 10 full revolutions
    stepper.step(2000, () => {
        console.log("done moving CCW");

        // once first movement is done, make 10 revolutions clockwise at previously
        //      defined speed, accel, and decel by passing an object into stepper.step
        stepper.step({
        steps: 2000,
        direction: Stepper.DIRECTION.CW
        }, () => console.log("done moving CW"));
    });
    });

 Here is a breakdown of the code:

  • The first line imports the Board and Stepper classes from the Johnny-Five library.
const {Board, Stepper} = require("johnny-five");
  • A new Board instance is created.
const board = new Board();
  • The "ready" event is attached to the board instance. This event is triggered when the board is ready to communicate with Johnny-Five.
board.on("ready", () => { ... });
  •  Inside the "ready" event callback, a new Stepper instance is created with the following configuration:
const stepper = new Stepper({ type: Stepper.TYPE.FOUR_WIRE, stepsPerRev: 200, pins: { motor1: 10, motor2: 11, motor3: 12, motor4: 13 } });
  • This creates a Stepper object that uses the four-wire configuration, with 200 steps per revolution, and the pins connected to the stepper motor are defined in the pins object.
  •  The stepper is configured to move at 180 rpm, in a counter-clockwise (CCW) direction, with an acceleration and deceleration of 1600 steps/s^2.
stepper.rpm(180).direction(Stepper.DIRECTION.CCW).accel(1600).decel(1600);

  • The stepper is instructed to make 10 full revolutions in the CCW direction using the step() method, which takes the number of steps to move and an optional callback function.
stepper.step(2000, () => { ... });
  •  When the first movement is completed, the stepper is instructed to make 10 revolutions in the clockwise (CW) direction, using an object as an argument to step(). The object includes the number of steps and the direction of movement.
stepper.step({
  steps: 2000,
  direction: Stepper.DIRECTION.CW
}, () => console.log("done moving CW"));
  •  Finally, when the second movement is completed, a message is logged to the console.


Conclusion

In this tutorial, we have learned how to control a stepper motor using Johnny-Five and an Arduino board. We have covered the basics of stepper motor control, and explained the code step by step. We hope that this tutorial has been helpful, and that you now have a good understanding of how to use Johnny-Five to control hardware with JavaScript.

References

[1] Arduino stepper motor position control potentiometer

[2] Stepper motor speed control arduino 

 

Post a Comment

Previous Post Next Post