DIO - Digital Input/Output#
The digital inputs and outputs can be controlled / read or set via GPIOs.
There are multiple ways on how to access the GPIOs of the Raspberry Pi, one way is by using the so called sysfs through /sys/class/gpio or another by using a library like pigpio.
Below both options are shown. The sysfs option can be used out of the box, while the pigpio library has to be installed first.
Overview of available IOs#
Four digital inputs/outputs (either or) are available. If used as output the state can be read back from the associated input.
The table below shows number and function of the available GPIOs.
| Name | direction | Accessible via | Connector |
|---|---|---|---|
| dout1 | output | GPIO44 |
X19_DIO1 |
| din1 | input | GPIO45 |
X19_DIO1 |
| dout2 | output | GPIO35 |
X19_DIO2 |
| din2 | input | GPIO43 |
X19_DIO2 |
| dout3 | output | GPIO34 |
X19_DIO3 |
| din3 | input | GPIO31 |
X19_DIO3 |
| dout4 | output | GPIO29 |
X19_DIO4 |
| din4 | input | GPIO28 |
X19_DIO4 |
Using sysfs to access the GPIOs#
Go to the path#
pi@raspberry:~ $ cd /sys/class/gpio
Make a GPIO accessible#
As an example we use GPIO44 which is digital output 1 (DO1) on the Pi-Tron. Writing the number of a GPIO to the export file in /sys/class/gpiomakes the GPIO accessible.
pi@raspberry:~ $ echo "44" > export
Changing the direction of a GPIO to input or output#
For our example we switch the direction of GPIO44 to output so we can turn DO1 on and off.
echo "out" > gpio44/direction
Switching a GPIO on#
Writing a 1 to the value property of a GPIO turns the GPIO on. For our example, we write a 1 to GPIO44 to turn it and thus DO1 on.
echo "1" > gpio44/value
Switching a GPIO off#
Writing a 0 to the value property of a GPIO turns the GPIO off. For our example, we write a 0 to GPIO44 to turn it and thus DO1 off.
echo "0" > gpio44/value
Using the pigpio library#
Install pigpio library#
sudo apt update
sudo apt install pigpio
Start the pigpio service#
The pigpio library cannot work on its own, it uses an accompanying service program called pigpiod. This service has to be started first, before any access to the GPIOs is possible.
sudo systemctl start pigpiod
If the daemon should start every time the Raspberry Pi/Pi-Tron starts, the following command can be used to enable the services automatic start.
sudo systemctl enable pigpiod
Changing the direction of a GPIO to input or output#
To make changes to the GPIOs from the console the program pigs has to be used in order to be able to turn GPIOs on and off.
For our example we switch the direction of GPIO44 to output so we can turn DO1 on and off.
pigs modes 44 w
Switching a GPIO on#
Writing a 1 to a GPIO number with pigs turns the GPIO on. For our example, we write a 1 to GPIO44 to turn it and thus DO1 on.
pigs w 44 1
Switching a GPIO off#
Writing a 0 to a GPIO number with pigs turns the GPIO off. For our example, we write a 0 to GPIO44 to turn it and thus DO1 off.
pigs w 44 0
Commands to control "pigpiod"#
pigpio-daemon turn on automatic start
sudo systemctl enable pigpiod
Deactivate automatic start
sudo systemctl disable pigpiod
pigpio-Daemon manual start:
sudo systemctl start pigpiod
pigpio-Daemon stop:
sudo systemctl stop pigpiod
Show status of the pigpio-Daemon:
sudo systemctl status pigpiod