GPIO on Orange PI PC2 H5

The Orange PI PC2 H5 is one of the many Orange Pi boards offering interesting features:

  • 64-bit 4-core ARM H5 processor
  • 1GB ethernet
  • 3 USB 2
  • HDMI
  • Audio Jack
  • Camera interface
  • Microphone
  • Infra red
  • OS: ARMBIAN. See article here for installation
  • price <20 € at Aliexpress

To access the GPIO of an Orange PI, you can use one of the libraries like OPI.Gpio. Unfortunately it does not cover all Orange PI card models. In the case of the Orange PI PC2 H5, it does not work. An alternative solution is the Sysfs virtual file system which allows system commands to activate the GPIO pins.

Connector on Orange Py PC2 H5 board

GPIO Orange Pi PC2 H5

La carte One Plus dispose d’un connecteur 40 pins suivant le schéma ci dessous.

Pour attaquer une des pins en entrée ou en sortie, il faut trouver la référence du GPIO correspondant. Le tableau ci après est valable pour l’Orange Pi PC2 H5. Ainsi si on veut utiliser la pin physique 40, elle correspond au GPIO 199.

 +-----+----------+-Orange Pi+----------+-----+
 |GPIO |   Name   | Physical | Name     |GPIO |
 +-----+----------+----++----+----------+-----+
 |     |     3.3v |  1 || 2  | 5v       |     |
 |  12 |    SDA.0 |  3 || 4  | 5V       |     |
 |  11 |    SCL.0 |  5 || 6  | 0v       |     |
 |   6 |     PWM1 |  7 || 8  | TxD3     | 13  |
 |     |       0v |  9 || 10 | RxD3     | 14  |
 |   1 |     RxD2 | 11 || 12 | PD14     | 110 |
 |   0 |     TxD2 | 13 || 14 | 0v       |     |
 |   3 |     CTS2 | 15 || 16 | PC 4     | 68  |
 |     |     3.3v | 17 || 18 | PC 7     | 71  |
 |  64 |     MOSI | 19 || 20 | 0v       |     |
 |  65 |     MISO | 21 || 22 | RTS2     | 2   |
 |  66 |     SCLK | 23 || 24 | CE0      | 67  |
 |     |       0v | 25 || 26 | PA21     | 21  |
 |  19 |    SDA.1 | 27 || 28 | SCL.1    | 18  |
 |   7 |      PA 7| 29 || 30 | 0v       |     |
 |   8 |     PA 8 | 31 || 32 | RTS1     | 200 |
 |   9 |     PA 9 | 33 || 34 | 0v       |     |
 |  10 |     PA10 | 35 || 36 | CTS1     | 201 |
 |  20 |     PA20 | 37 || 38 | TxD1     | 198 |
 |     |       0v | 39 || 40 | RxD1     | 199 |
 +-----+----------+----++----+----------+-----+
 |GPIO |   Name   | Physical | Name     |GPIO |
 +-----+----------+-Orange Pi+----------+-----+

Example Oscillator on pin 40

Below is a program in python 3. Attention, important constraint. You must be root to access the GPIO.

#!/usr/bin/python3  Python 3 environment
#-- coding: utf-8 --
import os              # To execute system command
from time import sleep # this lets us have a time delay
numgpio=199
numgpio=str(numgpio)
print ("numgpio:"+numgpio)
os.system("sudo echo "+numgpio+" > /sys/class/gpio/export")
os.system("sudo echo out > /sys/class/gpio/gpio"+numgpio+"/direction")

try:
         print ("Square signal around 50Hz . Press CTRL+C to exit")
         while True:
              os.system("echo 0 > /sys/class/gpio/gpio"+numgpio+"/value")
              sleep(0.01)
              os.system("echo 1 > /sys/class/gpio/gpio"+numgpio+"/value")
              sleep(0.01)


except KeyboardInterrupt:
         # set port/pin value to 0/LOW/False
         os.system("echo 0 > /sys/class/gpio/gpio"+numgpio+"/value") 
         # Clean GPIO
         os.system("sudo echo "+numgpio+" > /sys/class/gpio/unexport")
         print ("Bye from F1ATB.")

to launch it, if the program is on your desktop:

python3 /home/user/Desktop/Nom_du_programme.py

You can enter commands manually from a terminal window.
The … export command activates the GPIO 199 which corresponds to pin 40.
The direction command, here ‘out’ to be in output.
The commands value alternately to 0 or 1 to make an oscillator.

Then in case of Ctrl-C, we finish properly by freeing the GPIO 199.

To get rid of the root problem, a solution can be to run the program when starting up the Orange PI which in this case is root before switching to user mode. Just modify the /etc/rc.local file (sudo nano /etc/rc.local). Put the command of launching with the path in absolute (python. /Home/……py) before the exit 0 of the file rc.local.

Sysfs Commands Summary

On Armbian, you see the GPIO hardware exposed in the file system under /sys class/gpio.

$ ls /sys/class/gpio
export gpio199 gpiochip0 gpiochip352 unexport

There are currently used gpio and GPIO controllers denoted “gpiochip”.

The basic steps for using a GPIO pin (here 199 or pin 40) from the sysfs interface are as follows:

Activation Gpio199 :

$ echo 199> /sys/class/gpio/export

Pin as input:

$ echo in> /sys/class gpio/gpio199 direction

Pin as output:

$ echo out> /sys /class/gpio/gpio199/direction

Replay the file to check the current status:

$ cat /sys/class/gpio/gpio199/direction

Output set:

$ echo 0> /sys/class/gpio/gpio199/value

or

$ echo 1> /sys/class/gpio/gpio199/value

Input read status:

$ cat /sys/class/gpio/gpio199/value

Gpio release:

If you have finished using the GPIO pin, just write the name of the pin to the export file.

$ echo 199> /sys/class/gpio/unexport

The entry for gpio199 will no longer appear in sysfs:
$ ls/sys/class/gpio

Remarks

  • A pin configured as an output can be read as input to know its status.
  • You cannot pull up or down an entry.
  • This very general solution is applicable on all Orange Pi