Yesterday, one person asked me to make a video showing GPIO control on Orange Pi 2 mini on YouTube, and since I have just completed a post about Orange Pi camera, I thought it might be fun to check GPIO support too. This post focuses on Allwinner H3 boards, but the instructions and status should be very similar for Allwinner A20 and A31s versions.
One of the first things you want before starting playing with GPIOs is the expansion header’s pinout chart, and I could not find any until I had the idea to check for schematics, which are available on Orange Pi resources page. I downloaded the schematics for Orange Pi 2, which should be the same as for Orange Pi 2 mini as the only difference is the lack of WiFi module. Orange Pi PC is a little different, and the schematics are nowhere to be found, but it would be surprising if the 40-pin connector had a different layout.
Some documentation would be nice too, and there’s a WiringPi for Orange Pi page in the wiki, which points to WiringOP on github, but unfortunately it does not exist [Update Nov 3, 2015: WiringOP is up on Github with the last changes 4 days ago]. A more generic GPIO page refers to RPi.GPIO, BCM2835 and WebioPi libraries, but again those sections are empty. After cheking in the forums, it seems there’s no GPIO library for Orange Pi boards. If somebody is interested in working on a GPIO library for Orange Pi, starting with LeMaker’s WiringBP for Banana Pro might be a good idea, as GPIO, SPI and I2C support is implemented. Banana Pro is based on Allwinner A20, so some registers and addresses might be different, but required technical information should be available in Allwinner H3 datasheet.
So instead I used sysfs to control the board’s I/Os. GPIO (gpio-sunxi), SPI (spi-sunxi), and I2C (i2c-sunxi) drivers are pre-installed in the Debian Jessie image I installed in Orange Pi 2 mini board, but today, I only played with GPIO on my board.
Using sysfs is pretty easy:
1 2 3 4 5 6 7 8 |
su modprobe gpio-sunxi ls /sys/class/gpio_sw/ normal_led PA10 PA13 PA18 PA20 PA6 PA9 PC2 PC7 PG7 PL10 PA0 PA11 PA14 PA19 PA21 PA7 PC0 PC3 PD14 PG8 standby_led PA1 PA12 PA15 PA2 PA3 PA8 PC1 PC4 PG6 PG9 echo 1 > /sys/class/gpio_sw/standby_led/data echo 0 > /sys/class/gpio_sw/standby_led/data |
The few commands above load the GPIO driver, list the GPIOs, and turn on and off a green LED (D7) on the board.
To film a more entertaining video, I used the LED board from my Raspberry Pi perfboard enclosure which I connected to 9 GPIOs on the board, as well as 5V and GND. You can ignore IO-0, IO-2 on the schematics at the top, as only the pin numbers (1 – 40) are used to wire the boards, and PA7, PA8, etc… are used to control the GPIOs from Linux.
I’ve written a shell script that turn on each LED, or group of LEDs, one by one every second, before turning them off every second in an infinite loop. That’s the result.
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.
Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress
Thanks for sharing the GPIO info.
IT is a pitty that the info on the official webpage is so bad.
Good info would help the community build better images and librarles and boost the sales.
But the seller is really crappy.
@Carlos
You get what you pay for. Default distributions are really crappy. Laboris’ distributions seem to be quite good, however he can’t test every function on every board. Ethernet does not work on OPI PC with his distributions. OPI forum did not allow me to register since 30 hours and to ask for help there…
Ethernet work with laboris’ image on OPI PC. Using Debian Wheezy image at least.
@slackstick
slackstick, I used loboris’ debian image on an OPI PC and was able to bring up the ethernet interface, get an IP address via DHCP and ping other servers on the network. I haven’t done any more than that yet. Did you follow the instructions properly and copy over the correct script.bin file before booting the image?
The GPIO header pinouts are documented in the “user manual”. You can find that on the Orange Pi resources page (http://www.orangepi.org/downloadresources/) in the Software Design section. There appears to be a user manual for all the Orange Pi boards.
There is a v1.1 H3 datasheet available linked from the linux-sunxi H3 page (http://linux-sunxi.org/H3). I don’t know the difference to the v1.0 datasheet linked in this article.
@camh
OK I see, yesterday I went there and tried to click the link on Orange Pi 2 mini user’s manual, but nothing happened. Looks like I had to click on Google Drive icon instead…
I can’t find manual for Orange Pi PC, but it should be pretty similar to Orange Pi 2 mini.
@slackstick
I can also confirm Ethernet is working on Debian Jessie image by Loboris.
I think Ethernet might be an issue when people mix up Orange Pi Plus and Orange Pi 2 images or script.bin/uImage. (TBC)
@dvl36
Thanks. You are right. The problem seems to be on the other side of the port.
Has anybody tried using an ftdi connected to the usb? did you get the drivers to work for the ft232rl? FTDI works fine in the usb ports of Cubieboard 2. Any luck with the uart pins in the GPIO? thanks in advance.
I’m trying to make the FTDI work in an Orange Pi Plus, but so far it doesn’t work.
@As1cK
I’m not sure if you want to :
1. Connect a USB to TTL debug to the UART pin of the boards, and to a USB port of a computer
2. Connect that board to the USB port of the board itself to control/monitor something else.
I’ve done (1) by connecting it to the three pin header as shown in in the second photo in that post: http://www.cnx-software.com/2015/09/27/using-usb123-usbee-ax-pro-5-usb-logic-analyzer-with-pulseview-in-linux/
No problem to have it work.
I have not tried (2)
what is up with the orange pi price.
its up 130% from just a week ago.
@emsen
Which Orange Pi? Orange Pi PC?
@emsen Looks the same to me .. http://www.aliexpress.com/item/Orange-Pi-PC-ubuntu-linux-and-android-mini-PC-Beyond-and-Compatible-with-Raspberry-Pi-2/32448079125.html?spm=2114.01020208.3.212.l2OvCc&ws_ab_test=201556_1,201527_2_71_72_73_74_75,0_0
I need to use the same function of GPIO on RPi. How to activate GPIO interrupt function on Orange Pi PC with Allwinner H3? Have to wait for next version kernel? Please help.
@T.Fuse
Check this out: http://linux-sunxi.org/External_interrupts
@Jean-Luc Aufranc (CNXSoft)
H3 GPIO function configured in /sys/class/gpio_sw/P$xx/ files; cfg(for direction, o(in)or 1(out)), data(for read/write data 0 or 1), direction and data only? In same diretion, ‘drv’ and ‘pull’ files. those means ‘drv’ is for drive current configuration, and ‘pull’ is for pull down/up enable/disable configuration? If ‘edge’ function like RPi identified, Orangepi PC will be perfectly compatible with RPi GPIO. EXTINT function will be primary important issue for expanding more Orangepi PC market.
Anyway, I will check more of your suggestion contents. Thanks!
@camh
Nothing for Orange Pi PC
I could be missing something – but on my Orange Pi PC – that command of yours…
modprobe gpio-sunxi – produces…
modprobe: ERROR: could not insert ‘gpio_sunxi’: Exec format error
@Peter Scargill
That’s weird, as if it was compiled for the wrong target (e.g. x86)…
I used Debian 8 image (September 30) by Loboris.
WiringOP has been implemented @ https://github.com/zhaolei/WiringOP
Orange Pi PC schematics (PDF) @ https://drive.google.com/file/d/0BwQT0FP7IkTbRFJQamNwWTAzWms/view
@Peter Scargill
I actually had the same problem. I was using the most recent version of Debian_jessie_mini.img from Google Drive (as of November 14th). But also got the “modprobe: ERROR: could not insert ‘gpio_sunxi’: Exec format error” error.I was able to fix this by downloading the desktop_scripts.tar.gz and running the update_kernel.sh script. (all files I’m referring to are available at https://drive.google.com/folderview?id=0B1hyW7T0dqn6fndnZTRhRm5BaW4zVDVyTGlGMWJES3Z1eXVDQzI5R1lnV21oRHFsWnVwSEU&usp=sharing#list see this thread from more info: http://www.orangepi.org/orangepibbsen/forum.php?mod=viewthread&tid=342&extra=page%3D1)
Hello. I’ve got a problem using interrupt with WiringOP. In my C program I use the method wiringPiISR to catch an Input Change Event. if I run the program I’ve got the following error: wiringPiISR: unable to open /sys/class/gpio/gpio10/value: No such file or directory Actually the folder is missing. I read elsewhere that I’ve got to export the pin before using the method but if I execute gpio export 10 out I’ve the error gpio: Unable to open GPIO export interface: No such file or directory Again, the /sys/class/gpio/export is missing. Actually all the structure /sys/class/gpio/* is missing. Only the… Read more »
@dom
Did you ensure to use latest kernel+script.bin that’s been made for your board? Check http://filez.zoobab.com/allwinner/orangepi/mega/update_kernel.sh and optionally http://www.orangepi.org/orangepibbsen/forum.php?mod=viewthread&tid=785
Thanks for you answer, tkaiser.
I’m sure I’m using lastest kernel and script.bin.
I run the update_kernel.sh, in the past, and I did again fine minute ago (reboot and sudo depmod -a)
Currently I’m using dietPI image (based on Debian loboris image) .
But I had the same problem (missing folder /sys/class/gpio/* ) also with loboris image or raspbian.
I found no solution in internet….
@dom
I used “/sys/class/gpio_sw/” to control the GPIOs in the example above.
Are you sure /sys/class/gpio/ is what you need?
@Jean-Luc Aufranc (CNXSoft)
I also uses gpio_sw for normal pin use. It works very well. The problem is when using wiringop interrupt functionality (method wiringPiISR). It needs sys/class/gpio/ folders because it performs a gpio export that needs that folder.
The error is:
wiringPiISR: unable to open /sys/class/gpio/gpio10/value: No such file or directory
Solution found. Look at this thread.
http://www.orangepi.org/orangepibbsen/forum.php?mod=viewthread&tid=148&extra=&highlight=gpio%2Blibrary&page=7
need to recompile the kernel with sysfs interface enabled and disable Gpio_sunxi
I did so, and finally folder /sys/class/gpio/* appears and Interrupts work
thanks to WereCatf
This should work out of the box with Armbian now. 5.04 has been released yesterday and supports all H3 Orange Pi boards: http://www.armbian.com/download/ (click there on the ‘Questions?’ link to display a short ‘Armbian H3 Mini FAQ’)
We just created a new subsection for H3 right now: http://forum.armbian.com/index.php/forum/13-allwinner-h3/
Hello,
what about reading data from GPIO pins? Why some of pings are named like PA, others is PC, PD or PG? What is the difference?
I mean if i want to connect temperature sensor to some of these pins, which one i have to use?
Thanks in advice
@Kristian Kirilov The GPIOs are usually grouped in blocks, so A, C, P, G are just names. However, most pins usually has multiple uses e.g. GPIO, I2C, UART, one-wire, etc… and some may have different features (e.g. interrupt support), so you need to check the datasheet or at least the pinout diagram to find out which pin(s) is/are more suitable for your sensor. Your temperature sensor is likely using I2C or one-wire interface, so you may want to locate these pins. It’s quite likely somebody has already connect your sensors with other Linux based board, so to get the software… Read more »
@Jean-Luc Aufranc (CNXSoft)
It should be added that regarding 1-Wire all H3 OS images use pin 37 instead of the one normally used on Raspberry Pi. Loboris started with this setting and Armbian/OpenELEC decided to use it too to avoid confusion (even FriendlyARM chose the same pin with their OS images for NanoPi M1 🙂 )
Loading or adding/uncommenting to/in /etc/modules the following is enough to make it work (applies to loboris, OpenELEC or Armbian images): w1-sunxi, w1-gpio and w1-therm.
Thanks guys
@Jean-Luc Aufranc (CNXSoft) have you been able to change the direction of GPIO pins on the Orange Pi PC 2 (H3) – I believe /sys/class/gpio_rw/PA*/cfg is used for that? Even after root permissions, setting the file to either 0 or 1 seemingly goes fine, but it will never actually change the contents (if you verify with cat).
I managed to change it one time ever, now it refuses to work even after many reboots :/
Hi. How to control a servo using PWM? Is it possible? Thank you in advance.
@javier
I can see people use PWM on Orange Pi Allwinner H3 Boards @ https://github.com/vlna/orange-pi-pwm
@Jean-Luc Aufranc (CNXSoft)
Great, thanks for your response. I forget to say: in python… i don’t know c…. Sorry and thank you again.
@javier
I’m not sure. It may be possible to write a Python wrapper around the C code. https://docs.python.org/2/extending/extending.html
While you can do many things with Python, if you are going to play with development boards, it’s always useful to know some C, in case you have to do some optimization (code in Python may be slower), debug some Linux driver code written in C, and so on… You may also end up using Arduino at some points, which uses C API.
Hello!
At a Armbian image put on Orange PI Lite (One) what i have to do to optain list of GPIO pins
like in that commands:
ls /sys/class/gpio_sw/
normal_led PA10 PA13 PA18 PA20 PA6 PA9 PC2 PC7 PG7 PL10
PA0 PA11 PA14 PA19 PA21 PA7 PC0 PC3 PD14 PG8 standby_led
PA1 PA12 PA15 PA2 PA3 PA8 PC1 PC4 PG6 PG9
when i do
modprobe gpio-sunxi
and
ls /sys/class/gpio_sw/
folder gpio_sw appears, but is empty…
Ok, thanks. I will take a look
@Dan
I could list the GPIO in Orange Pi One with an older Armbian image @ http://www.cnx-software.com/2016/03/16/orange-pi-one-board-quick-start-guide-with-armbian-debian-based-linux-distribution/
Are you sure gpio-sunxi module is loaded? You can check with lsmod.
Finally i figure it out how to move a servo with Orange Pi and Arduino. The text is spanish, but easy to understand or translate with google:
http://orangepiweb.es/orange-pi-arduino.php
Best regards.
Javier