1.3.1 Motor

Introduction

In this project, we will learn to how to use L293D to drive a DC motor and make it rotate clockwise and counterclockwise. Since the DC Motor needs a larger current, for safety purpose, here we use the Power Supply Module to supply motors.

Components

../_images/list_1.3.1.png

Schematic Diagram

Plug the power supply module in breadboard, and insert the jumper cap to pin of 5V, then it will output voltage of 5V. Connect pin 1 of L293D to GPIO22, and set it as high level. Connect pin2 to GPIO27, and pin7 to GPIO17, then set one pin high, while the other low. Thus you can change the motor’s rotation direction.

../_images/image336.png

Experimental Procedures

Step 1: Build the circuit.

../_images/image117.png

Note

The power module can apply a 9V battery with the 9V Battery Buckle in the kit. Insert the jumper cap of the power module into the 5V bus strips of the breadboard.

../_images/image118.jpeg

Step 2: Go to the folder of the code.

cd ~/davinci-kit-for-raspberry-pi/nodejs/

Step 4: Run the code.

sudo node motor.js

As the code runs, the motor first rotates clockwise for 1s then stops for 1s, after that, it rotates anticlockwise for 1s; subsequently, the motor stops for 1s. This series of actions will be executed repeatedly.

Code

const Gpio = require('pigpio').Gpio;

MotorPin1 = new Gpio(17, { mode: Gpio.OUTPUT });
MotorPin2 = new Gpio(27, { mode: Gpio.OUTPUT });
MotorEnable = new Gpio(22, { mode: Gpio.OUTPUT });


// Define a motor function to spin the motor
// direction should be
// 2(clockwise), 1(counterclockwise), 0(stop)
function motor(direction) {
    switch (direction) {
        case 2: // Clockwise
            // Set direction
            MotorPin1.digitalWrite(1)
            MotorPin2.digitalWrite(0)
            // Enable the motor
            MotorEnable.digitalWrite(1)
            console.log('Clockwise')
            break;
        case 1:  // Counterclockwise
            // Set direction
            MotorPin1.digitalWrite(0)
            MotorPin2.digitalWrite(1)
            // Enable the motor
            MotorEnable.digitalWrite(1)
            console.log('Counterclockwise')
            break;
        case 0: // Stop
            // Disable the motor
            MotorEnable.digitalWrite(0)
            console.log('Stop')

    }
}

process.on('SIGINT', function () {
    MotorEnable.digitalWrite(0)
    process.exit();
})

let index=-1
setInterval(() => {
    index=(index+1)%3
    motor(index)
}, 1000)

Code Explanation

MotorPin1 = new Gpio(17, { mode: Gpio.OUTPUT });
MotorPin2 = new Gpio(27, { mode: Gpio.OUTPUT });
MotorEnable = new Gpio(22, { mode: Gpio.OUTPUT });

Import the pigpio module and create three Gpio class objects to control the three IO ports of Gpio17, Gpio27, and Gpio22.

function motor(direction) {
    switch (direction) {
        case 2: // Clockwise
            // Set direction
            MotorPin1.digitalWrite(1)
            MotorPin2.digitalWrite(0)
            // Enable the motor
            MotorEnable.digitalWrite(1)
            console.log('Clockwise')
            break;
        case 1:  // Counterclockwise
            // Set direction
            MotorPin1.digitalWrite(0)
            MotorPin2.digitalWrite(1)
            // Enable the motor
            MotorEnable.digitalWrite(1)
            console.log('Counterclockwise')
            break;
        case 0: // Stop
            // Disable the motor
            MotorEnable.digitalWrite(0)
            console.log('Stop')

    }
}

Define a motor() function to control the motor,

  1. When the direction is equal to 2, the MotorPin1 port writes a high level, the MotorPin2 port writes a low level, and the enable port MotorEnable writes a high level, and the motor rotates clockwise.

  2. When the direction is equal to 1, the MotorPin1 port writes a low level, the MotorPin2 port writes a high level, and the enable port MotorEnable writes a high level, and the motor rotates counterclockwise.

  3. When the direction is equal to 0, the enable port MotorEnable is written to a low level, and the motor stops rotating.

let index=-1
setInterval(() => {
    index=(index+1)%3
    motor(index)
}, 1000)

Let the motor rotate clockwise and counterclockwise alternately, with an interval of 1 second.

process.on('SIGINT', function () {
    MotorEnable.digitalWrite(0)
    process.exit();
})

When it is detected that ctrl+c is pressed, MotorEnable is written low to stop the motor from spinning.

Phenomenon Picture

../_images/image119.jpeg