Raspberry Pi configure GPIO at system startup

Before you can use a GPIO as input or output, it must be configured or set. Unfortunately, Raspberry Pi does not remember this setting. Therefore, the setting must be executed again each time the system is restarted.
Note
There are several ways to program and configure GPIOs. For example via “sysfs”, “wiringPi” or “pigpio”. We use “pigpio” here. This requires that the pigpio daemon be installed and enabled at system startup.
Solution: GPIO configuration in the file “/etc/rc.local”
The file “/etc/rc.local” is automatically loaded at system startup. Therefore, it is very well suited for configuring GPIOs at system startup.
For configuration we open the file “/etc/rc.local” and configure the configuration of the GPIOs.
sudo nano /etc/rc.local
Here we enter the following line before “exit 0” if we want the GPIO 17 (pin 11) to be configured as an output with the default state “low” (0).
pigs modes 17 w
Or we enter here the following lines before “exit 0”, if we want to have the GPIO 17 (pin 11) configured as output with the basic state “high” (1).
pigs modes 17 w
pigs w 17 1
Or we enter the following lines before “exit 0”, if we want the GPIO 17 to be configured as an input.
pigs modes 17 r
Then save and close: Ctrl + O, Return, Ctrl + X.
To try a restart is necessary.
sudo reboot
The file “/etc/rc.local” is executed relatively late. To be precise, just before the login request appears. This may be too late for one application or another. The question is, what state do the GPIOs have before running the file “/etc/rc.local”?
Raspberry Pi GPIO configuration via a Bash script
Typically, a script will be started at each boot to set the function of the required GPIO pins. This has the advantage that you can change the file without any root rights at any time.
First we create a script file:
sudo nano /home/pi/gpio.sh
In principle, we enter the same command in the script file as in the file “/etc/rc.local”.
#! /Bin/bash
{COMMAND}
exit 0
Then save and close: Ctrl + O, Return, Ctrl + X.
The file should then still be executable.
chmod + x /home/pi/gpio.sh
The call to the script is then entered in the file “/etc/rc.local”, if you want it to be executed automatically at boot time.
...
# Run a script for setting up GPIO pins
/home/pi/gpio.sh> /dev/null 2> & 1
exit 0
Then save and close: Ctrl + O, Return, Ctrl + X.
To test if this really works a reboot is necessary.
Trying chmod + x /home/pi/gpio.sh gives error:
cannot access ‘x’: no such file or directory
I think that was a typo. try:
chmod +x /home/pi/gpio.sh
Can also simply add the line to etc/crontab
@reboot pigs mode 17 w