Controlling The Speed Of The Motor
This is a continuation to my first lesson on how we can control a DC motor using a Raspberry Pi and Python. If you would like to follow along with this lesson, and have not yet seen the first one, please read the first article found here.
In this lesson, we are going to see how we can alter the speed of our DC motor using Pulse Width Modulation.
What is Pulse Width Modulation
Pulse width modulation (PWM) is a technique of varying a signal’s width in proportion to the signal’s amplitude, to convey information about the signal’s frequency and amplitude. PWM is often used to regulate the amount of power that is delivered to the motor of an electric motor and the intensity of an LED. It simply turns off and on the pins at a very fast rate.
How Can We Use it with the Pi?
The Raspberry Pi comes with built-in support for PWM. In fact, almost all of the pins on the Pi support it!
Adding Speed Control
Before we begin programming the PWM functionality, we must first add a new pin to the L289N board.
Wiring
On the far left and far right are two pins that are covered with a small piece of plastic. This shorts the connection between ground and the L298N’s PWM pins, causing them to stay on as long as there is power to the board. We must remove the cover in order to access and control the pin ourself. Connect the PWM pin from the breakout board to a GPIO pin on the Pi; I’m using pin number 15
.
Updating our Code
The Raspberry Pi provides a pre-built method that allows us to create PWM pins easily. To add a PWM pin, simply begin the same way as you would with a normal pin. After, we have to tell the Pi that we want it to be a PWM pin by calling the GPIO.PWN()
method. Finally we can start the modulation by calling the start()
method. With these changes in mind, our motor.py
will look like the following:
import RPi.GPIO as GPIO
class motor:
def __init__(self, F, B, PWM):
self.pinFwd = F
self.pinBwd = B
self.pinPWM = PWM
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.pinFwd, GPIO.OUT)
GPIO.setup(self.pinBwd, GPIO.OUT)
GPIO.setup(self.pinPWM, GPIO.OUT) # PWM Pin
self.PWM = GPIO.PWM(self.pinPWM, 1000) # Sets the rate to 1000
self.PWM.start(100) # Starts at 100% intensity
def __del__(self):
GPIO.cleanup()
def forward(self):
GPIO.output(self.pinFwd, GPIO.HIGH)
GPIO.output(self.pinBwd, GPIO.LOW)
def backward(self):
GPIO.output(self.pinFwd, GPIO.LOW)
GPIO.output(self.pinBwd, GPIO.HIGH)
def stop(self):
GPIO.output(self.pinFwd, GPIO.LOW)
GPIO.output(self.pinBwd, GPIO.LOW)
def speed(self, val):
self.PWM.ChangeDutyCycle(val) # Changes the intensity
Notice that we’ve created a new method called speed
that takes in a number. This method allows us to change the speed of the motor any time we want.
In our main.py
file, we will test our new additions.
from motor import motor
import time
def main():
motor1 = motor(7, 11, 15)
motor1.backward()
time.sleep(2.5)
motor1.speed(50) # Changes speed to 50%
motor1.forward()
time.sleep(2.5)
motor1.stop()
if __name__ == '__main__':
main()
Run the program by typing python main.py
and the motor will begin to spin backwards at 100% speed for 2.5 seconds, then spin forwards at 50% speed for 2.5 seconds.