|
| 1 | +# Motors |
| 2 | + |
| 3 | +Motors are great for physical computing - they allow you to turn a wheel forwards and backwards, or make something spin around. |
| 4 | + |
| 5 | +A motor cannot be controlled directly from the Raspberry Pi's GPIO pins, because it needs a variable supply of 5 volts and it needs powering separately. However, motor controller add-on boards can be used to provide this functionality. |
| 6 | + |
| 7 | +In this guide, you'll be controlling two motors from your Raspberry Pi using Python on the desktop. First, it's best just to learn how to control the motors, then once you have it working you could easily use your code to drive a Raspberry Pi-powered robot, by detaching the monitor, mouse and keyboard and building a robot around a chassis. |
| 8 | + |
| 9 | +## H-Bridge |
| 10 | + |
| 11 | +A motor comprises two pins: positive and negative. If a 5V supply is provided to the positive pin, and 0V (ground) to the negative, it will drive forwards. If 0V is provided to the positive, and 5V to the negative, it will drive backwards. If no voltage is provided, the motor will stop. |
| 12 | + |
| 13 | +EXPLAIN H-BRIDGE... |
| 14 | + |
| 15 | +## Wiring |
| 16 | + |
| 17 | +You'll need to wire up two motors and your battery pack using the motor controller. |
| 18 | + |
| 19 | +1. With your Pi switched off, mount your motor controller board on the GPIO pins: |
| 20 | + |
| 21 | +  |
| 22 | + |
| 23 | +1. Connect a battery pack to the power ports of the motor controller, connecting the positive (red) battery wire to the positive (+) power terminal on the motor controller, and the negative (black) battery wire to the negative (-) power terminal on the motor controller: |
| 24 | + |
| 25 | +  |
| 26 | + |
| 27 | +1. You'll need to know which GPIO pins your motor controller uses. Refer to the board's documentation. This will usually be described as Motor A and Motor B, or MA1, MA2, MB1 and MB2. Make a note of these pin numbers. If you're not sure which is which, you can investigate this next. |
| 28 | + |
| 29 | +## Output Devices |
| 30 | + |
| 31 | +First, you should learn to control motors by controlling the pins individually. |
| 32 | + |
| 33 | +1. Boot the Pi and open Python 3 |
| 34 | + |
| 35 | +1. In the shell, enter the following line to import `OutputDevice` from the GPIO Zero library: |
| 36 | + |
| 37 | + ```python |
| 38 | + from gpiozero import OutputDevice |
| 39 | + ``` |
| 40 | + |
| 41 | + After each line, press **Enter** and the command will be executed immediately. |
| 42 | + |
| 43 | +1. Now create an instance of an `OutputDevice` on each of the pins for one motor: |
| 44 | + |
| 45 | + ```python |
| 46 | + a = OutputDevice(4) |
| 47 | + b = OutputDevice(14) |
| 48 | + ``` |
| 49 | + |
| 50 | +1. Now you should be able to make the motor spin by turning one of the pins on: |
| 51 | + |
| 52 | + ```python |
| 53 | + a.on() |
| 54 | + ``` |
| 55 | + |
| 56 | + The motor should now be spinning! If not, check you are addressing the right pin numbers. The two pins should be connected to the same motor. Also check your wiring and your batteries. |
| 57 | + |
| 58 | +  |
| 59 | + |
| 60 | +1. Now try turning that pin off, and turning the other pin on: |
| 61 | + |
| 62 | + ```python |
| 63 | + a.off() |
| 64 | + b.on() |
| 65 | + ``` |
| 66 | + |
| 67 | + The motor should now be spinning in the opposite direction. |
| 68 | + |
| 69 | +  |
| 70 | + |
| 71 | +1. To stop the motor, just make sure both motors are off: |
| 72 | + |
| 73 | + ```python |
| 74 | + a.off() |
| 75 | + b.off() |
| 76 | + ``` |
| 77 | + |
| 78 | +1. Now try the same with the second motor: |
| 79 | + |
| 80 | + ```python |
| 81 | + c = OutputDevice(17) |
| 82 | + d = OutputDevice(27) |
| 83 | + c.on() |
| 84 | + ``` |
| 85 | + |
| 86 | +1. And backwards: |
| 87 | + |
| 88 | + ```python |
| 89 | + c.off() |
| 90 | + d.on() |
| 91 | + ``` |
| 92 | + |
| 93 | +1. And stop: |
| 94 | + |
| 95 | + ```python |
| 96 | + c.off() |
| 97 | + d.off() |
| 98 | + ``` |
| 99 | + |
| 100 | +1. Try controlling one of the motors in a loop: |
| 101 | + |
| 102 | + ```python |
| 103 | + from time import sleep |
| 104 | + |
| 105 | + for i in range(5): |
| 106 | + b.off() |
| 107 | + a.on() |
| 108 | + sleep(5) |
| 109 | + a.off() |
| 110 | + b.on() |
| 111 | + sleep(5) |
| 112 | + b.off() |
| 113 | + ``` |
| 114 | + |
| 115 | + The motor should now spin forwards for 5 seconds, then backwards for 5 seconds, and repeat 5 times, then stop. |
| 116 | + |
| 117 | +## PWM |
| 118 | + |
| 119 | +So far, you have used simple on/off commands to control your motors. PWM (Pulse-width modulation) allows you to control the speed. The `on()` function set the motor to go at full speed, but you can control this to go at a fraction of this speed. |
| 120 | + |
| 121 | +1. Since you're going to reuse the same pins in a different way, you'll have to close the connections to those pins. The easiest way to do that is to restart the Python shell by clicking **Shell > Restart shell**. |
| 122 | + |
| 123 | +1. Import the `PWMOutputDevice` class: |
| 124 | + |
| 125 | + ```python |
| 126 | + from gpiozero import PWMOutputDevice |
| 127 | + ``` |
| 128 | + |
| 129 | +1. Create new connections to each of your pins, as before, but using `PWMOutputDevice`: |
| 130 | + |
| 131 | + ```python |
| 132 | + a = PWMOutputDevice(4) |
| 133 | + b = PWMOutputDevice(14) |
| 134 | + c = PWMOutputDevice(17) |
| 135 | + d = PWMOutputDevice(27) |
| 136 | + ``` |
| 137 | + |
| 138 | +1. You can still use `a.on()`, `a.off()` and so on, but you can also set the device's value to a number between `0` and `1`. Try half: |
| 139 | + |
| 140 | + ```python |
| 141 | + a.value = 0.5 |
| 142 | + ``` |
| 143 | + |
| 144 | + The motor should now be spinning at half speed. |
| 145 | + |
| 146 | +1. To turn the motor the opposite direction, turn `a` off (or set its value to `0`) and set `b`'s value to `0.5`: |
| 147 | + |
| 148 | + ```python |
| 149 | + a.value = 0 |
| 150 | + b.value = 0.5 |
| 151 | + ``` |
| 152 | + |
| 153 | + The motor should now be spinning backwards at half speed. |
| 154 | + |
| 155 | +1. Try controlling both motors at different speeds to compare: |
| 156 | + |
| 157 | + ```python |
| 158 | + a.value = 0.5 |
| 159 | + b.value = 0 |
| 160 | + c.value = 1 |
| 161 | + d.value = 0 |
| 162 | + ``` |
| 163 | + |
| 164 | +1. Try increasing the speed in a loop: |
| 165 | + |
| 166 | + ```python |
| 167 | + b.off() |
| 168 | + d.off() |
| 169 | + for i in range(1, 11): |
| 170 | + speed = i / 10 |
| 171 | + print(speed) |
| 172 | + a.value = speed |
| 173 | + b.value = speed |
| 174 | + ``` |
| 175 | + |
| 176 | + The motors should now speed up from 0 (stopped) to 0.1, 0.2 and up to 1. |
| 177 | + |
| 178 | + Note that the motor may not move until it gets to a certain speed as there may not be enough power to engage the motor. |
| 179 | + |
| 180 | +## Motor class |
| 181 | + |
| 182 | +Now you've learned how setting pins high and low can control a motor, you should proceed to using the built-in `Motor` class, which has all the functionality you just learned about, provided in a simple way, including PWM for speed. |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | +1. Restart the shell again (**Ctrl + F6**) |
| 187 | + |
| 188 | +1. Import the `Motor` class: |
| 189 | + |
| 190 | + ```python |
| 191 | + from gpiozero import Motor |
| 192 | + ``` |
| 193 | + |
| 194 | +1. Now create a `Motor` instance using the pin numbers for each motor: |
| 195 | + |
| 196 | + ```python |
| 197 | + motor1 = Motor(4, 14) |
| 198 | + motor2 = Motor(17, 27) |
| 199 | + ``` |
| 200 | + |
| 201 | + Note: to make it easier to see which pin is which, you can use `Motor(forward=4, backward=14)` for future reference. |
| 202 | + |
| 203 | +1. Now drive one of the motors forward using: |
| 204 | + |
| 205 | + ```python |
| 206 | + motor1.forward() |
| 207 | + ``` |
| 208 | + |
| 209 | +1. And the other backwards: |
| 210 | + |
| 211 | + ```python |
| 212 | + motor2.backward() |
| 213 | + ``` |
| 214 | + |
| 215 | +1. Or try half speed: |
| 216 | + |
| 217 | + ```python |
| 218 | + motor1.forward(0.5) |
| 219 | + motor2.backward(0.5) |
| 220 | + ``` |
| 221 | + |
| 222 | +1. The `Motor` class also allows you to reverse the motor's direction. Try using this a loop: |
| 223 | + |
| 224 | + ```python |
| 225 | + motor1.forward() |
| 226 | + motor2.backward() |
| 227 | + while True: |
| 228 | + sleep(5) |
| 229 | + motor1.reverse() |
| 230 | + motor2.reverse() |
| 231 | + ``` |
| 232 | + |
| 233 | + This will make the motors spin in opposite directions, then switching every 5 seconds. Press **Ctrl + C** to exit the loop. |
| 234 | + |
| 235 | +1. Now stop the motors: |
| 236 | + |
| 237 | + ```python |
| 238 | + motor1.stop() |
| 239 | + motor2.stop() |
| 240 | + ``` |
| 241 | + |
| 242 | +## Robot class |
| 243 | + |
| 244 | +If you had a robot with two wheels, you would want to control the two motors together, rather than separately, just like you did for the two pins of each motor. Luckily, there's also a `Robot` class in GPIO Zero. |
| 245 | + |
| 246 | + |
| 247 | + |
| 248 | +1. Restart the shell again (**Ctrl + F6**) |
| 249 | + |
| 250 | +1. Import the `Motor` class: |
| 251 | + |
| 252 | + ```python |
| 253 | + from gpiozero import Robot |
| 254 | + ``` |
| 255 | + |
| 256 | +1. Now create a `Robot` instance using the pin numbers for each motor: |
| 257 | + |
| 258 | + ```python |
| 259 | + robot = Robot((4, 14), (17, 27)) |
| 260 | + ``` |
| 261 | + |
| 262 | + Note: to make it easier to see which pin is which, you can use `Robot(left=(4, 14), right=(17, 27))` for future reference. |
| 263 | + |
| 264 | +1. Now drive one of the motors forward using: |
| 265 | + |
| 266 | + ```python |
| 267 | + robot.forward() |
| 268 | + ``` |
| 269 | + |
| 270 | + Both motors should now be driving forwards. |
| 271 | + |
| 272 | +1. And backwards: |
| 273 | + |
| 274 | + ```python |
| 275 | + robot.backward() |
| 276 | + ``` |
| 277 | + |
| 278 | + Both motors should now be driving backwards. |
| 279 | + |
| 280 | +1. Try reverse a few times: |
| 281 | + |
| 282 | + ```python |
| 283 | + robot.reverse() |
| 284 | + robot.reverse() |
| 285 | + robot.reverse() |
| 286 | + ``` |
| 287 | + |
| 288 | +1. Or try half speed: |
| 289 | + |
| 290 | + ```python |
| 291 | + robot.forward(0.5) |
| 292 | + ``` |
| 293 | + |
| 294 | +1. That's not all! What would happen if the left wheel went forwards and the right wheel went backwards? The robot would turn right. Try it: |
| 295 | + |
| 296 | + ```python |
| 297 | + robot.right() |
| 298 | + ``` |
| 299 | + |
| 300 | +1. Then obviously: |
| 301 | + |
| 302 | + ```python |
| 303 | + robot.left() |
| 304 | + ``` |
| 305 | + |
| 306 | +1. Now stop the robot: |
| 307 | + |
| 308 | + ```python |
| 309 | + robot.stop() |
| 310 | + ``` |
| 311 | + |
| 312 | +## What next? |
| 313 | + |
| 314 | +Now you've learned how motors work, why not try: |
| 315 | + |
| 316 | +- [Build a robot](http://raspberrypi.org/learning/robo-butler/) |
| 317 | +- Make a [spinning flower wheel](http://raspberrypi.org/learning/spinning-flower-wheel/) |
0 commit comments