Controlling the speed of a DC motor

In any smart home, chances are you will find a DC motor somewhere that you will need to automate. It could, for example, be on electric window blinds, or on an automated garage door. In this section, we are going to see how to control the speed of a simple DC motor and you will then be able to apply this to any motors you already have in your home.

Let's first see how to connect the DC motor to your Raspberry Pi Zero board. We actually won't connect the motor directly to the Raspberry Pi, as this would require a lot of external components, such as transistors, diodes, and so on. Instead, we'll use the L293D chip, which is a dedicated IC to control DC motors.

First, place the L293D on the board. The following diagram shows the pinout of the L293D:

Controlling the speed of a DC motor

You basically need to connect the components to the L293D as follows:

  • GPIO14 of the Raspberry Pi to pin 1A
  • GPIO15 of the Raspberry Pi to pin 2A
  • GPIO18 of the Raspberry Pi to pin 1,2EN
  • DC motor to pin 1Y and 2Y
  • 5V of the Raspberry Pi to VCC1
  • GND of the Raspberry Pi to GND
  • Battery pack to VCC2 and GND

The following image shows the final result:

Controlling the speed of a DC motor

We are now going to see how to perform a simple test of the DC motor to see if it is working correctly. We are simply going to make the motor accelerate from null speed to its maximum speed. The following is the code to do exactly that:

// Modules
var Gpio = require('pigpio').Gpio;

// Create motor instance
var motorSpeed = new Gpio(18, {mode: Gpio.OUTPUT});
var motorDirectionOne = new Gpio(14, {mode: Gpio.OUTPUT});
var motorDirectionTwo = new Gpio(15, {mode: Gpio.OUTPUT})

// Init motor direction
motorDirectionOne.digitalWrite(0);
motorDirectionTwo.digitalWrite(1);

var dutyCycle = 0;

// Go from 0 to maximum speed
setInterval(function () {
  motorSpeed.pwmWrite(dutyCycle);

  dutyCycle += 5;
  if (dutyCycle > 255) {
    dutyCycle = 0;
  }
}, 20);

You can now save this code in a JavaScript file, and use a Terminal to navigate to the folder where this file is located. Then, type the following:

sudo npm install pigpio

This will install the required module to use this test file. Then, launch the test with the following:

sudo node motor_test.js

You should immediately see the motor going from zero to its maximum speed, and then start again.

Just like the LED previously, we want to be able to control the speed of the motor using a graphical interface. As the code for this part is really similar to the code for the previous section, I will only highlight the main differences here.

First, we create instances of the GPIO module to control each output pin we need:

var motorSpeed = new Gpio(18, {mode: Gpio.OUTPUT});
var motorDirectionOne = new Gpio(14, {mode: Gpio.OUTPUT});
var motorDirectionTwo = new Gpio(15, {mode: Gpio.OUTPUT});

We also define a route to set the speed of the motor:

app.get('/set', function (req, res) {

  // Set motor speed
  speed = req.query.speed;
  motorSpeed.pwmWrite(speed);

  // Set motor direction
  motorDirectionOne.digitalWrite(0);
  motorDirectionTwo.digitalWrite(1);

  // Answer
  answer = {
    speed: speed
  };
  res.json(answer);

});

For now, we'll set the direction inside the code for convenience, but you can of course change it now.

As for the LED control, we'll use an interface that will just display a single slider to control the speed of the motor.

It's now time to test the project! Grab all the code from this book's GitHub repository and inside the folder where you extracted the code, type the following:

sudo npm install express

This will install the Express module that is required for the project. Then, you can start the server using the following:

sudo node motor_control.js

You can now navigate to the main interface of the project (of course replacing the IP with the one for your Pi):

http://192.168.0.103:3000

You should see an interface displaying a simple slider to control the speed of the motor:

Controlling the speed of a DC motor

You can now try it: moving the slider and releasing the mouse should immediately change the speed of the motor to the desired speed. You can, of course, now also add a switch to the interface to also change the direction of the motor using this simple graphical interface. This will allow us to control a motor for which you might want to reverse the direction of rotation, for example, to open or close a garage door.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset