Orange Pi PC 2 GPIO and Onboard Button

08/25/18

Categories: Computers Tags: Orange Pi

~/contents

I was excited to try out this board, but I quickly learned compared to the OP Zero, which is H2, the OPPC2’s H5 has much less support and information. My first few days working with this SBC have been frustrating to say the least. Two issues that immediately arose were how to access The Button–that is, the onboard button (SW4) and how to access the GPIO with Python.

GPIO

I tried multiple H5 pyA20 libraries as well as WiringOP, ArmbianIO, and UserspaceIO and encountered many problems. A recurring issue is many libraries are unable to detect the PC2. In both ArmbianIO and UserspaceIO I had difficulty getting the Python bindings to install correctly. I worked a little with sysfs, but ultimately abandoned it when I realized it might be depreciated.

Finally, I found a solution in this thread. duxingkei33’s orangepi_PC_gpio_pyH3 library can be used, but 2 files must be modified before the library is installed. In gpio_lib.h and gpio_lib.c you must replace all references to SUNXI_PIO_BASE from “unsigned int” to “unsigned long.” There are 3 replacements to be made, and this needs to be done before installing the library. (Alternately, you could try adding PC2 mapping to chwe17’s pyGPIO library. I was going to attempt this if the H3 library didn’t work.)

The LED example doesn’t work for me, so here’s a momentary button test:

#!/usr/bin/python
from pyA20.gpio import gpio
from pyA20.gpio import port
import time

BUTTON = port.PA11
gpio.init()
gpio.setcfg(BUTTON, gpio.INPUT)
gpio.pullup(BUTTON, gpio.PULLUP)

while True:
    input_state = gpio.input(BUTTON)
    if not input_state:
        print('Button Pressed')
        time.sleep(0.1)

So why doesn’t port.STATUS_LED trigger the onboard LED? I knew my LED worked becuase I could trigger it through sysfs.

sudo sh -c "echo 255 > /sys/class/leds/orangepi:red:status/brightness"
sudo sh -c "echo 0 > /sys/class/leds/orangepi:red:status/brightness"

The library maps it thus: { "STATUS_LED", SUNXI_GPA(15), 2 }

And the documenation says–

The board has two LEDs mid-way between the Micro USB jack (CN1) and the H5 SoC (U1): - A red PWR LED, D7, connected to the PA20 pin. - A green STATUS LED, D8, connected to the PL10 pin.
This contradiction is a good example of some of the challenges users face with this board. The schematic indicates the STATUS LED is connected to GPIO PA15, but in a different section (CPU) it lists PA20. I find port.PA20 does indeed trigger the red status LED.

“The” Button

A number of Orange Pi boards are blessed with an onboard momentary button. Certain OS map this button in various ways to shut the system down, but Armbian does not. The documentation refers to button SW4 as PWR/KEYADC and indicates it pulls PL3.

You may be able to use acip to detect the key event…

sudo apt isntall acipd
sudo acip-listen

In which case you can follow pargles solution. acip didn’t work for me, so I tried evtest.

sudo evtest /dev/input/event0

God willing, you’ll see something like this:

Event: time 1535234339.182935, type 1 (EV_KEY), code 256 (BTN_0), value 1
Event: time 1535234339.182935, -------------- SYN_REPORT ------------

There are a few ways to handle this button. One is a device tree overlay, which I found too complicated as a newcomer. I ended up using triggerhappy, a hotkey daemon. Shut down your Raspberry Pi, the easy way will walk you through this process.


EV_KEY  BTN_0   1       /dev/input/event0
# BTN_0 1       command
EV_KEY  BTN_0   0       /dev/input/event0
# BTN_0 0       command

BTN_0 1 /etc/trigger.sh