2.controlling Hardware
2.controlling Hardware
Connect an LED to one of the GPIO pins using a 470Ω or 1kΩ series resistor to limit the
current as shown in Figure 4.1. To make this, you will need:
• Breadboard and jumper wires
• 1kΩ resistor
• LED
Having connected the LED, we need to be able to turn it on and off using commands from
Python. To do this, install the RPi.GPIO Python library.
Start a Python console from the Terminal with superuser access and enter these commands:
$ sudo python
>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(18, GPIO.OUT)
>>> GPIO.output(18, True)
>>> GPIO.output(18, False)
This will turn your LED on and off.
If you wanted to extend the experiments that you made in the Python console into a program
that makes the LED blink on and off repeatedly, you could paste the following code into the
editors. Save the file as led_blink.py.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
while (True):
GPIO.output(18, True)
time.sleep(0.5)
GPIO.output(18, False)
time.sleep(0.5)
Remember that to run the program, you must have superuser privileges for the RPi.GPIO
library, so you need to use this command:
$ sudo python led_blink.py
To vary the brightness of an LED from a Python program. The RPi.GPIO library has a pulse-
width modulation (PWM) feature that allows you to control the power to an LED and its
brightness. Here is the code for varying brightness.
You can use the same LED blink program that you used in 4.1.1. If all is well, you’ll
hear a click from the relay each time the contacts are closed. However, relays are slow
mechanical devices, so don’t try to use them with PWM. It may damage the relay.
Relays:
Relays have been around since the early days of electronics and have the great
advantage of being easy to use, plus they’ll work in any situation where a switch would
normally work—for example, when you’re switching AC (alternating current) or in situations
where the exact wiring of the device being switched is unknown. If the relay contacts are
asked to exceed their specifications, then the relay’s life will be shortened. There will be
arcing, and the contacts may eventually fuse together. There is also the possibility of the relay
becoming dangerously hot. When in doubt, over specify the relay contacts.
Servo motors are used in remote control vehicles and robotics. Most servo motors are
not continuous; that is, they cannot rotate all the way around but rather just over an angle of
about 180 degrees.
The position of the servo motor is set by the length of a pulse. The servo expects to
receive a pulse at least every 20 milliseconds. If that pulse is high for 1 millisecond, the servo
angle will be zero; if it is 1.5 milliseconds, it will be at its centre position; and if it is 2
milliseconds, it will be at 180 degrees as shown in figure 4.8. The example program sets the
PWM frequency to 100 Hz, which will send a pulse to the servo every 10 milliseconds. The
angle is converted into a duty cycle between 0 and 100. This actually produces pulses shorter
than the 1 millisecond expected minimum value and longer than 2 milliseconds maximum.
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 500)
pwm.start(100)
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
scale = Scale(frame, from_=0, to=100,
orient=HORIZONTAL, command=self.update)
scale.grid(row=0)
def update(self, duty):
pwm.ChangeDutyCycle(float(duty))
root = Tk()
root.wm_title('PWM Power Control')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()
Note that you will need to run it with sudo because the RPi.GPIO requires you to have
superuser privileges to access the GPIO hardware.
$ sudo python gui_slider.py
Figure 4.12 shows the wiring diagram for using a ULN2803. Note that the chip can be
used to drive two such motors. To drive a second stepper motor, you will need to connect
four more control pins from the GPIO connector to pins 5 to 8 of the ULN2803 and connect
the second motor’s four pins to pins 11 to 14 of the ULN2803.
the coils in a certain order drives the motor around. The number of steps that the stepper
motor has in a 360-degree rotation is actually the number of teeth on the rotor.
Want to drive a four-lead bipolar stepper motor using a Raspberry Pi. Use a L293D H-Bridge
driver chip. An H-Bridge is required to drive a bipolar stepper motor because, as the word
bipolar suggests, the direction of current across the windings needs to be reversed, rather like
driving a DC motor in both directions.
To make this, you will need:
➢ 12V, four-pin bipolar stepper motor
➢ L293D H-Bridge IC
➢ Breadboard and jumper wires
The motor used here, a 12V, is somewhat larger than the previous unipolar stepper motor
example. The power for the motor itself is therefore supplied from an external power supply
rather than from the Raspberry Pi. Code is same as above 4.1.7. The design uses both H-
Bridges of the L293D, so you need one of these chips for each motor you want to control.