Skip to content

GPIO control

Francois Corthay edited this page Apr 8, 2026 · 7 revisions

Table of Contents

GPIO control

The RPi has a 40-pin General Purpose Input/Ouput (GPIO) connector whose pins can be individually watched or controlled. Some are specified to be binary outputs and all others are considered as binary inputs.

Command line

Raspberry Pi OS comes with GPIO control command-line controllers:

raspi-gpio help
pinctrl -h

View pin numbers and associated GPIO:

pinctrl -p

GPIO input

Connect a switch between pin 13 and the ground. Test it:

pinctrl -p 13

GPIO=27
pinctrl set $GPIO ip pu
pinctrl get $GPIO ; sleep 1 ; pinctrl get $GPIO

GPIO output

Connect a resistor and a LED between pin 15 and the ground. Test it:

pinctrl -p 15

GPIO=22
pinctrl set $GPIO op pn
pinctrl set $GPIO dh ; sleep 1 ; pinctrl set $GPIO dl

Python RPi.GPIO

The GPIOs can be controlled via the RPi.GPIO Python interface. This is implemented in the xpl-RPi_GPIO.py script.

The script maintains a list of the input and output GPIOs. It sets all GPIOs to inputs with a pull-up resistor, except for the ones declared in the reserved_pins array. If one wants to use the I2C bus, GPIOs 2 and 3 should be part of the array. GPIO 7 are set to outputs by the system and used at boot for different configurations. Once a GPIO has been set to any level by an xPL command, it is moved to the list of the output GPIOs. It is no more pulled up and driven as an output pin. It can nevertheless further be read for its logic level.

Test

Make sure that the control user is in the gpio group:

cat /etc/group | grep gpio

Start the script in a terminal:

XPL_BASE_DIR=~/Controls/xPL
GPIO_SERVER='audioSwitch'
$XPL_BASE_DIR/utilities/xpl-RPi_GPIO.py -n $GPIO_SERVER -v

In a second terminal, start an xPL monitor:

XPL_BASE_DIR=~/Controls/xPL
$XPL_BASE_DIR/xPL-base/xpl-monitor.pl -vf

In a third terminal, send commands:

XPL_BASE_DIR=~/Controls/xPL
GPIO_SERVER='dspc-gpio.audioSwitch'
$XPL_BASE_DIR/xPL-base/xpl-send.pl -d $GPIO_SERVER -c gpio.basic led=22 set=on
$XPL_BASE_DIR/xPL-base/xpl-send.pl -d $GPIO_SERVER -c gpio.basic led=22
$XPL_BASE_DIR/xPL-base/xpl-send.pl -d $GPIO_SERVER -c gpio.basic led=22 set=off
$XPL_BASE_DIR/xPL-base/xpl-send.pl -d $GPIO_SERVER -c gpio.basic led=22

The LED attached to GPIO 22 (header pin 15) should reflect the command. The monitor shows what happens.

Python GPIO Zero

The GPIOs can be controlled via the GPIO Zero Python interface. This is implemented in the xpl-RPi_GPIO_zero.py script.

I had problems using I2C control together with or within GPIO Zero.

Installation

The service allows even non-sudoer users to control the pins through the pigpio service. Activate it:

sudo systemctl enable pigpiod
sudo systemctl start pigpiod

Test

Start the script in a terminal:

XPL_BASE_DIR=~/Controls/xPL
GPIOZERO_PIN_FACTORY=pigpio $XPL_BASE_DIR/utilities/xpl-RPi_GPIO_zero.py -o 20,21,23,24,25,26 -v

The -o parameters specifies which are the output pins.

In a second terminal, start an xPL monitor:

XPL_BASE_DIR=~/Controls/xPL
$XPL_BASE_DIR/xPL-base/xpl-monitor.pl -vf

In a third terminal, send commands:

XPL_BASE_DIR=~/Controls/xPL
GPIO_SERVER='dspc-gpio.lachesis'
$XPL_BASE_DIR/xPL-base/xpl-send.pl -d $GPIO_SERVER -c gpio.basic led=23 set=on
$XPL_BASE_DIR/xPL-base/xpl-send.pl -d $GPIO_SERVER -c gpio.basic led=23

The LED attached to GPIO 23 (header pin 16) should reflect the command. The monitor shows what happens.

Service

Edit the service specification file:

XPL_BASE_DIR=~/Controls/xPL
SERVICE='xpl-RPi_GPIO'
nano $XPL_BASE_DIR/services/$SERVICE.service

If using xpl-RPi_GPIO.py, be sure to have the WorkingDirectory declaration pointing to a directory writable by the user control. If using xpl-RPi_GPIO_zero.py, be sure to have Environment="GPIOZERO_PIN_FACTORY=pigpio" set and specify the output pins list with the -o parameter.

Copy the file to the system directory:

sudo cp $XPL_BASE_DIR/services/$SERVICE.service /lib/systemd/system/

Start the service:

sudo systemctl enable $SERVICE.service
sudo service $SERVICE start
sudo service $SERVICE status | cat

Integration

The central control service and its configuration file allow to pipe the press of a button to an actuation.

Pressing and releasing a button will trigger 2 xPL messages. If one wants to emulate an on-off switch button, the trigger messages also provide a toggle value which changes at each button press.

Clone this wiki locally