Changing the speed
Now that we have understood how to change the direction of the motor using the motor driver, it's time to take it a step further and control the speed of the motor using the motor driver. To do this, we don't really have to do much. The motor drivers are built to understand the PWM signals. Once the PWM signal to the motor driver is provided, then the motor driver in turn adjusts the output voltage for the motor and hence changes the speed of the motor driver. The PWM has to be provided on the same input ports IN 1 and IN 2 for motor A, and IN 3 and IN 4 for motor B. It is obvious that the pin on which the PWM is provided will decide the direction in which the motor will move, and the duty cycle of the PWM will decide the speed at which the motor will be spinning.
Now we have understood how speed control in motor driver works. It's time to do it by ourselves. To do so, we do not need to make any changes to the connections; all we need to do is to upload the following code:
import RPi.GPIO as GPIO
from time
import sleep
GPIO.setmode(GPIO.BCM)
Motor1R = 20
Motor1L = 21
GPIO.setup(Motor1R, GPIO.OUT)
GPIO.setup(Motor1L, GPIO.OUT)
pwm = GPIO.PWM(Motor1R, 100)
pwm.start(0)
try:
while True:
GPIO.output(Motor1L, GPIO.LOW)
for i in range(0, 101):
pwm.ChangeDutyCycle(i)
sleep(0.1)
except KeyboardInterrupt:
pwm.stop()
GPIO.cleanup()
What happened after you ran this code? I'm sure the motor started slowly and then started increasing its speed and, upon reaching its top speed, it eventually stopped—exactly what we wanted it to do. If you remember, this code looks very familiar. Remember changing the brightness of the LED in the first chapter? It is almost the same; there are a few differences, though, so let's see what they are:
pwm = GPIO.PWM(Motor1R, 100)
In this line, we are simply defining the pin we have to give the PWM on—as in, on Motor1R, which corresponds to pin number 20. Also, we are are defining the frequency of the PWM as 100 hertz or 100 times in a second:
pwm.start(0)
If you remember, the preceding command from the previous chapters, pwm.start(), is primarily used for defining the duty cycle of the signal. Here, we are giving it the duty cycle as 0 that is the pin would be off:
GPIO.output(Motor1L,GPIO.LOW)
As we are running motor in one specific direction and which is 1R hence the other half of the H bridge should be turned off. this would be done by the above line by putting the line 1L LOW. If we don't do this then the pin 21 can be in an arbitrary state, hence it can be either on or off. This might conflict with the direction in which the motor is moving and the hardware would not work properly:
for i in range(0,101):
Here comes the real deal; this line, for i in range(0,101):, will keep on running the program contained in it until the time the value of i is between 0 to 101. It will also increment the value of i every time this loop runs. Here, every time, the value will increase by one:
pwm.ChangeDutyCycle(i)
Now, this is a slightly new command. Previously, we have used the line pwm.start(0) to assign a duty cycle to the PWM. As we have already assigned a duty cycle value to the PWM, to change it we would use the previously mentioned command. The duty cycle would be the same as the value of i.
Hence, every time the code passes through the for loop, the value or the duty cycle will increase by one. Super easy, isn't it?
Everything in robotics is very easy if you do it right. The idea is to break your problem into small pieces and solve them one by one; trust me, once you do that, nothing will look difficult to you.