To start with, we will control only the relay from the Raspberry Pi connected to it. This will be done using GPIO pins. First, we need to connect the relay to our Raspberry Pi in the correct way. The following table shows the connection scheme:
Wire (color |
GPIO pin |
Relay pin |
Red |
3.3V (pin 1) |
Power/VCC (+) |
Black |
GND (pin 6) |
GND (-) |
Yellow |
GPIO 17 (pin 11) |
IN (Signal/S) |
When this scheme is physically applied, it will look like the following figure. Remember that the exact order of the relay pins might look different between different models.
When the connection to the relay is complete, we can continue with next step: turning on or off the small LED connected to the relay. Most relays have a small LED attached to them. If your relay doesn't have an LED indicator, don't worry; we will look at some methods of checking from the command line whether the GPIO pin is active or not. There exist a number of different methods to control the GPIO pins on the Raspberry Pi; in this book, we will learn how to use sysfs and RPi.GPIO to control the GPIO pins.
We will start by learning how to use sysfs to turn the LED on and off. First, we need to export the GPIO pins and thereby make them visible from the userspace:
$ echo 17 > /sys/class/gpio/export
Next, we can see that a gpio17
directory with some files has popped up in /sys/class/gpio
:
$ ls /sys/class/gpio17/
After that, we need to set the direction (in/out) for the GPIO pin; in our example, we want the GPIO to act as an output pin:
$ echo "out" > /sys/class/gpio/gpio17/direction
Finally, we can enable the GPIO pin, which will cause the LED to turn on, by writing 1
to the value
file:
$ echo 1 > /sys/class/gpio/gpio17/value
To turn off the light, we can write 0
to the value
file:
$ echo 0 > /sys/class/gpio/gpio17/value
When done, you need to unexport
the GPIO pin:
$ echo 17 > /sys/class/gpio/unexport
Now, the gpio17
directory in sysfs is gone. In our project, we will not manipulate the sysfs structure by hand. Instead, we will use the gpio-packt
recipe added to our image earlier in this chapter, but under the hood, it uses the sysfs structure in a similar way as just described. The gpio-packt
recipe produces a binary called gpio_example
; it can be used to control GPIO17 using the following syntax:
$ gpio_example --led=1 $ gpio_example --led=0
Another way of controlling the GPIO pins is by using RPi.GPIO
, which is a Python module that uses direct register access in the background to access GPIO. Controlling GPIO17 using RPi.GPIO requires that you add the following to a file called, for example, ledon.py
:
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.output(17, 1)
Then, we can run the script. Turning off the pin can easily be done by replacing 1
with 0
in the GPIO.output()
function:
$ python ledon.py
RPi.GPIO can also use the physical GPIO header pin numbering. In that case, GPIO17 will be placed on pin 11. To use this mode, the script must be modified as follows:
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) GPIO.output(11, 1)
The GPIO header diagram from Chapter 8 , Diving into the Raspberry Pis Peripherals and Yocto Recipes can be used as a reference for understanding and playing with the GPIO pins.
3.144.77.71