Power & Source of Big Ideas

Using GPIOs via command line

Moderators: chensy, FATechsupport

This is just to help people out. Initially, you have to be root to be able to use the GPIO pins via the command line. So, to get beyond having to be root, here are some tips.

sudo groupadd gpio
sudo usermod -aG gpio user-name

and, then (add these lines to the 99-gpio.rules file):

sudo nano /etc/udev/rules.d/99-gpio.rules

SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys/class/gpio/export /sys/class/gpio/unexport ; chmod 220 /sys/class/gpio/export /sys/class/gpio/unexport'"
SUBSYSTEM=="gpio", KERNEL=="gpio*", ACTION=="add", PROGRAM="/bin/sh -c 'chown root:gpio /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value ; chmod 660 /sys%p/active_low /sys%p/direction /sys%p/edge /sys%p/value'"

Save and reboot. Now, you should be able to do things like (turn an LED on and off on pin 8):

echo 8 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio8/direction
echo 1 > /sys/class/gpio/gpio8/value
echo 0 > /sys/class/gpio/gpio8/value
echo 8 > /sys/class/gpio/unexport
You could also use WiringNP for the GPIOs - according to the NanoPi Duo Wiki:

Make a shell script:

Code: Select all

vi test.sh

Type the following lines:

Code: Select all

gpio mode $LED out
while true; do
  gpio write $LED 1
  sleep 0.5
  gpio write $LED 0
  sleep 0.5

Run the script:

Code: Select all

sudo source test.sh

You can see the LED is blinking.

The wiringPi library was initially developed by Gordon Henderson in C.
It contains libraries to access GPIO, I2C, SPI, UART, PWM and etc.
The wiringPi library contains various libraries, header files and a commandline utility:gpio.
The gpio utility can be used to read and write GPIO pins.

FriendlyElec integrated this utility in Duo's system allowing users to easily access GPIO pins.
For more details refer to WiringNP

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest (based on users active over the past 5 minutes)
Most users ever online was 47 on Wed Feb 21, 2018 7:00 am

Users browsing this forum: No registered users and 1 guest