• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
KaliTut

KaliTut

Kali Linux tutorial and Linux system tips

  • Home
  • Raspberry Pi
  • Privacy Policy
  • About us

Configure Raspberry Pi GPIO at system startup

Last Updated on May 31, 2020 by Kalitut 3 Comments

Raspberry Pi configure GPIO at system startup

Configure Raspberry Pi GPIO

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.

  • Control and program GPIO with “pigpio”

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.

Filed Under: Raspberry Pi Tagged With: Raspberry Pi GPIO

Comments

  1. Beka says

    January 15, 2022 at 10:09 pm

    Trying chmod + x /home/pi/gpio.sh gives error:
    cannot access ‘x’: no such file or directory

    Reply
    • Connor says

      February 11, 2022 at 7:28 am

      I think that was a typo. try:
      chmod +x /home/pi/gpio.sh

      Reply
  2. Paul says

    January 1, 2023 at 11:04 am

    Can also simply add the line to etc/crontab

    @reboot pigs mode 17 w

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow us

  • Facebook
  • Twitter
  • YouTube

Categories

  • Android pentesting tools
  • Arduino
  • Books
  • Darknet
  • database
  • General
  • Github Tools
  • Hacking
  • Kali Linux
  • Linux
  • Linux Commands
  • Network Administrator
  • Penetration Testing
  • Penetration Testing Tools
  • PowerShell
  • Raspberry Pi
  • resources
  • Review
  • Termux
  • Tutorials
  • Ubuntu
  • Uncategorized
  • Video Tutorials
  • vmware
  • WiFi Adapter
  • WiFi Pentesting
  • Wireless Router
  • Wireshark

Recent Posts

  • Hijacked Wi-Fi? Thorough explanation of hacking techniques
  • Windows PowerShell tutorial for beginners
  • Learn to Hack Steps from Beginner to Hacker
  • PowerShell Tutorial – GUIDE introduction with basics
  • Top Hacking Tools
  • Home
  • About us
  • Privacy Policy
  • Affiliate disclaimer

Copyright © 2023