Skip to content

Commit a79bfaa

Browse files
authored
Merge pull request raspberrypilearning#4 from raspberrypilearning/motors
Add Motors page
2 parents b823e1c + e6c4691 commit a79bfaa

11 files changed

+321
-1
lines changed

analogue.md

+1
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,4 @@ while True:
187187
- Use potentiometers to control the visual settings of a Camera Module in real time
188188
- Find more analogue sensors that will work with the ADC
189189
- Use potentiometers to control a player in a [PyGame Zero](http://pygame-zero.readthedocs.io) game, or in [Minecraft](https://www.raspberrypi.org/learning/getting-started-with-minecraft-pi/)
190+
- Continue to the next worksheet on using [Motors](motors.md)

buzzer.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ Set up the circuit as shown below:
4444
```
4545

4646
## What Next?
47-
- Have a go at building a [traffic light](trafficlights.md) system using gpiozero.
47+
48+
- Continue to the next worksheet on building a [traffic lights](trafficlights.md) system using GPIO Zero.

distance.md

+1
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,4 @@ Now you've learned to use an ultrasonic distance sensor, you could:
124124
- Build a proximity sensor alert using a buzzer or a sound file
125125
- Build a Pi camera photo booth which is activated when a person is close enough to the camera
126126
- Build a robot with a distance sensor to stop it bumping into other objects
127+
- Continue to the next worksheet on [analogue inputs](analogue.md)

images/h-bridge.png

12.6 KB
Loading

images/mcb-wiring.png

48.1 KB
Loading

images/mcb.png

46.5 KB
Loading

ldr.md

+2
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ Use the following code to set up the light sensor:
6666
Run this code, then cover the LDR with your hand and watch the value change. Try shining a strong light onto the LDR.
6767

6868
## What Next?
69+
6970
- You could have a go at using your new knowledge of LDRs to build a [laser-tripwire](https://www.raspberrypi.org/learning/laser-tripwire/).
71+
- Continue to the next worksheet on using a [Passive Infra-red Sensor](pir.md)

motors.md

+311
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
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 can't be controlled directly from the Raspberry Pi's GPIO pins, because it needs a variable supply of 5 volts. This means you need to power it 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 motor. 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 can be driven forwards or backwards depending on which way around current flows through it. However, it would be awkward to have to rewire a motor, every time you want to change the direction it spins. To overcome this issue, motor controller boards include an H bridge. An H bridge uses 4 transistors to allow digital control of which way current flows through the motor. Most H bridges also contain *flyback diodes*. A flyback diode prevents the voltage spike that is generated by the motor when it is no longer powered (but still spinning) from damaging delicate electronics.
12+
13+
![H-Bridge](images/h-bridge.png)
14+
15+
*Image credit: [Wikipedia](https://en.wikipedia.org/wiki/H_bridge), CC BY-SA*
16+
17+
## Wiring
18+
19+
You'll need to wire up two motors and your battery pack using the motor controller.
20+
21+
1. With your Pi switched off, mount your motor controller board on the GPIO pins:
22+
23+
![Motor controller board](images/mcb.png)
24+
25+
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, and connect two motors:
26+
27+
![Connect battery pack](images/mcb-wiring.png)
28+
29+
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.
30+
31+
## Output devices
32+
33+
First, you should learn to control motors by controlling the pins individually.
34+
35+
1. Boot the Pi and open Python 3.
36+
37+
1. In the shell, enter the following line to import `OutputDevice` from the GPIO Zero library:
38+
39+
```python
40+
from gpiozero import OutputDevice
41+
```
42+
43+
After each line, press **Enter** and the command will be executed immediately.
44+
45+
1. Now create an instance of an `OutputDevice` on each of the pins for one motor:
46+
47+
```python
48+
a = OutputDevice(4)
49+
b = OutputDevice(14)
50+
```
51+
52+
1. Now you should be able to make the motor spin by turning one of the pins on:
53+
54+
```python
55+
a.on()
56+
```
57+
58+
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.
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+
1. To stop the motor, just make sure both motors are off:
70+
71+
```python
72+
a.off()
73+
b.off()
74+
```
75+
76+
1. Now try the same with the second motor:
77+
78+
```python
79+
c = OutputDevice(17)
80+
d = OutputDevice(27)
81+
c.on()
82+
```
83+
84+
1. And backwards:
85+
86+
```python
87+
c.off()
88+
d.on()
89+
```
90+
91+
1. And stop:
92+
93+
```python
94+
c.off()
95+
d.off()
96+
```
97+
98+
1. Try controlling one of the motors in a loop:
99+
100+
```python
101+
from time import sleep
102+
103+
for i in range(5):
104+
b.off()
105+
a.on()
106+
sleep(5)
107+
a.off()
108+
b.on()
109+
sleep(5)
110+
b.off()
111+
```
112+
113+
The motor should now spin forwards for 5 seconds then backwards for 5 seconds, repeat this 5 times, and then stop.
114+
115+
## PWM
116+
117+
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 sets the motor to go at full speed, but you can control this to make the motor go at a fraction of this speed.
118+
119+
1. Since you're going to reuse the same pins in a different way, you'll have to close the connections to the pins. The easiest way to do that is to restart the Python shell by clicking **Shell > Restart shell**.
120+
121+
1. Import the `PWMOutputDevice` class:
122+
123+
```python
124+
from gpiozero import PWMOutputDevice
125+
```
126+
127+
1. Create new connections to each of your pins as before, but using `PWMOutputDevice`:
128+
129+
```python
130+
a = PWMOutputDevice(4)
131+
b = PWMOutputDevice(14)
132+
c = PWMOutputDevice(17)
133+
d = PWMOutputDevice(27)
134+
```
135+
136+
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:
137+
138+
```python
139+
a.value = 0.5
140+
```
141+
142+
The motor should now be spinning at half speed.
143+
144+
1. To turn the motor in the opposite direction, turn `a` off (or set its value to `0`) and set `b`'s value to `0.5`:
145+
146+
```python
147+
a.value = 0
148+
b.value = 0.5
149+
```
150+
151+
The motor should now be spinning backwards at half speed.
152+
153+
1. Try controlling both motors at different speeds to compare:
154+
155+
```python
156+
a.value = 0.5
157+
b.value = 0
158+
c.value = 1
159+
d.value = 0
160+
```
161+
162+
1. Try increasing the speed in a loop:
163+
164+
```python
165+
b.off()
166+
d.off()
167+
for i in range(1, 11):
168+
speed = i / 10
169+
print(speed)
170+
a.value = speed
171+
b.value = speed
172+
```
173+
174+
The motors should now speed up from 0 (stopped) to 0.1, 0.2 and up to 1.
175+
176+
Be aware, though, that the motor may not move until it gets above a certain speed, as there may not be enough power to engage it.
177+
178+
## Motor class
179+
180+
Now you've learned how setting pins high and low can control a motor, you should proceed to using the built-in `Motor` class; this has all the functionality you just learned about, provided in a simple way, including PWM for speed.
181+
182+
1. Restart the shell again (**Ctrl + F6**).
183+
184+
1. Import the `Motor` class:
185+
186+
```python
187+
from gpiozero import Motor
188+
```
189+
190+
1. Now create a `Motor` instance using the pin numbers for each motor:
191+
192+
```python
193+
motor1 = Motor(4, 14)
194+
motor2 = Motor(17, 27)
195+
```
196+
197+
Note: to make it easier to see which pin is which, you can use `Motor(forward=4, backward=14)` for future reference.
198+
199+
1. Now drive one of the motors forward using the following code:
200+
201+
```python
202+
motor1.forward()
203+
```
204+
205+
1. And the other backwards:
206+
207+
```python
208+
motor2.backward()
209+
```
210+
211+
1. Or try half speed:
212+
213+
```python
214+
motor1.forward(0.5)
215+
motor2.backward(0.5)
216+
```
217+
218+
1. The `Motor` class also allows you to reverse the motor's direction. Try using this loop:
219+
220+
```python
221+
motor1.forward()
222+
motor2.backward()
223+
while True:
224+
sleep(5)
225+
motor1.reverse()
226+
motor2.reverse()
227+
```
228+
229+
This will make the motors spin in opposite directions, then switch every five seconds. Press **Ctrl + C** to exit the loop.
230+
231+
1. Now stop the motors:
232+
233+
```python
234+
motor1.stop()
235+
motor2.stop()
236+
```
237+
238+
## Robot class
239+
240+
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.
241+
242+
1. Restart the shell again (**Ctrl + F6**).
243+
244+
1. Import the `Motor` class:
245+
246+
```python
247+
from gpiozero import Robot
248+
```
249+
250+
1. Now create a `Robot` instance using the pin numbers for each motor:
251+
252+
```python
253+
robot = Robot((4, 14), (17, 27))
254+
```
255+
256+
Note: to make it easier to see which pin is which, you can use `Robot(left=(4, 14), right=(17, 27))` for future reference.
257+
258+
1. Now drive one of the motors forward using the following code:
259+
260+
```python
261+
robot.forward()
262+
```
263+
264+
Both motors should now be driving forwards.
265+
266+
1. And backwards:
267+
268+
```python
269+
robot.backward()
270+
```
271+
272+
Both motors should now be driving backwards.
273+
274+
1. Try reverse a few times:
275+
276+
```python
277+
robot.reverse()
278+
robot.reverse()
279+
robot.reverse()
280+
```
281+
282+
1. Or try half speed:
283+
284+
```python
285+
robot.forward(0.5)
286+
```
287+
288+
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:
289+
290+
```python
291+
robot.right()
292+
```
293+
294+
1. Then try this:
295+
296+
```python
297+
robot.left()
298+
```
299+
300+
1. Now stop the robot:
301+
302+
```python
303+
robot.stop()
304+
```
305+
306+
## What next?
307+
308+
Now you've learned how motors work, you can try:
309+
310+
- Making your own robot with our [Build a robot](https://www.raspberrypi.org/learning/robo-butler/) resource
311+
- Making a [spinning flower wheel](https://www.raspberrypi.org/learning/spinning-flower-wheel/)

pir.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ while True:
3131
```
3232

3333
## What Next?
34+
3435
- Now you know how to use a PIR, why not have a go at building a [Parent Detector](https://www.raspberrypi.org/learning/parent-detector)
36+
- Continue to the next worksheet on using an [Ultrasonic Distance Sensor](distance.md)

trafficlights.md

+1
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,4 @@ As well as controlling the whole set of lights together, you can also control ea
282282

283283
- Try adding a second button for the other side of the road. You'll probably need to use GPIO Zero `button.when_pressed` rather than `wait_for_press`, which can only be used for one button at a time.
284284
- Refer to the documentation at [gpiozero.readthedocs.org](https://gpiozero.readthedocs.org/) for more information on what can be done with GPIO Zero.
285+
- Continue to the next worksheet on using a [Light Dependent Resitor](ldr.md)

worksheet.md

+1
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,4 @@ There are lots of other things you can control or monitor with your Raspberry Pi
202202
- [Using a PIR Sensor](pir.md)
203203
- [Using an ultrasonic distance sensor](distance.md)
204204
- [Analogue inputs](analogue.md)
205+
- [Using motors](motors.md)

0 commit comments

Comments
 (0)