Howto: Controlling Pine64 GPIO via the filesystem (sysfs) on linux
#1
Information 
So, you received your Pine64 board, and now you want to do the first thing that almost all electronic hobbyists do as their first project... blink an LED.

You might have seen a guide like this one, and gone... "that looks easy" or "I'm confused!". If you have used a Raspberry Pi, you're probably going "it's easy"... but there is a catch. Whilst the pine64 does have a 40 pin connector that is mostly compatible with the Raspberry Pi (ie. important bits like I2C, SPI, power are all in the same places), some bits don't work 100% like the Raspberry Pi. The gremlins we know about so far aren't a big problem as long as you know about them, as they are easily remedied. One or two others are annoying enough that you might just go "I wanted to use pin 7 for my project, but on the pine64 it controls the backlight for the LCD board, and turning it off isn't a one line command, so I'll just use another pin". That sort of thing. There are plenty of other I/O pins to choose from, so not being able to use one without some fiddling isn't a drama for most setups. 

The other thing to be aware of is that if you are using accessing the GPIO (General Purpose Input/Output) pins like I'm about to demonstrate, and are using a nice Raspberry Pi Pinout guide like the nice one that Raspberry Pi Spy does, the GPIO numbers are all different! So if you wanted to connect your LED to pin 15, which is GPIO22 on the Raspberry Pi, and told gpio22 to turn on and off, nothing would happen! The reason for this comes from the fact that the pins are grouped differently on the CPU used on the pine64. The detail isn't important, unless you wanted to work out how to calculate it yourself. If so, have at look at this post, and you'll be able to work out the formula used. Needless to say, if you've done it once, you've done it 100 times, so a nice lookup table is always handy. Joey was kind enough to write up a nice reference table showing the Pine64 GPIO pin number / Raspberry Pi GPIO pin numbering  / physical pin numbers. So keep that handy when you want to work out which pin is which. 

longsleep also wrote a very handy script which converts the pin names (the ones like PC7 and PH5) to sysfs GPIO numbers. All you need to do is download it, make it executable (chmod +x pine64-gpio-number.py) and then run it specifying the pin name as an argument... 


Code:
./pine64-gpio-number.py PC7


... to which the script will then print out "71" and volia... you know what GPIO number PC7 is! btw, you need python3 to run that script, so if it doesn't work properly when you run, you can add python3 by running "sudo apt-get -y install python3", and you can then try the script again.

Now, you have yourself a pine64, you're logged into the terminal either via a serial link, via a network (wired or wireless link), or by with a monitor or keyboard connected to it. If not...  you might want to back up a few steps, and get your system up to a state where you have a running linux distro, and can log into it! Smile

First thing you'll want to do is connect an LED to your pine64 so you can blink it. You have two options. There is actually a place on the board you can add an LED (a smaller 3mm led is best, as standard 5mm leds are a bit to big for where this LED goes). If you have a look near where the headphones socket is on the board, you'll see that there is a place for an IR receiver, a power and reset button, but more importantly for us, an LED (not to be confused with the Chg LED pads next to it - which is for usage with the pine64 battery charge circuitry). You can probably get away with poking the LED into the hole, and bending the leds to keep them under tension, whilst making sure they don't make contact with any contacts on the board, but I would strongly recomment you solder it on, so it makes a good connection. Plus it looks a lot neater! Smile If you take this option, make sure you put the LED in the right way around (cathode - short leg - goes to negative-, and anode - long leg - goes to positive +), otherwise you'll have to take it off and turn it around!! Also, the GPIO number you'll need to use to control this LED is 359, so remember that for later!

The second option is the easier one. You need an LED and a resistor (anything between 220ohm to 1K will do), a breadboard and some connector wires. If you don't have some handy, I strongly recommend you get yourself a set of male to male, male to female, and female to female dupont connector breadboard wires.... you can get a set of 120 for a couple of $$ on eBay, and you'll find them indispensable in future electronics projects. Wire up the breadboard something like the below example. You then connect the end of the resistor (wire on the left side) to a GND pin on the pine64 (pin 39 will do just nicely). For this example, connect the LED positive (wire on the right side) to pin 40 of the pine64. Now you have the LED connected, ready to make it blink. 

   

Now, at the pine64's console prompt, pressing enter at the end of each line. I will explain the purpose of each command in a moment.

Code:
sudo -i
echo "75" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio75/direction
echo 1 > /sys/class/gpio/gpio75/value
echo 0 > /sys/class/gpio/gpio75/value

Line 1 logs you into the root user account on your pine64, as due to how the permissions are configured by default, only the root user can access the GPIO stuff we're about to change. I believe it is possible to change permissions, or use different methods of accessing the GPIO, but that is beyond this scope of this guide.

Line 2 is where the magic starts. Before we can use a pin, we need to make sure it has been initialised. The only bit you would normally change on this line is the number. If you look at Joey's GPIO pinout reference, you'll see that the reason I specified 75 is because that is the pine64 GPIO port number that corresponds with pin40! 

Line 3 tells the pine64 if the GPIO pin is to be an input or an output (the I/O bit in GPIO). Since we want to turn an LED on and off... we want it to be an output. If we had wanted to monitor a swtich or button, we would have specified it to be an "input" instead. 

Lines 4 & 5 are the ones that will be the most exciting, and have been what we've been working towards... Line 4 will make the led turn on ("1"), and Line 5 will make the led turn off ("0"). Think of "1" being "POWER!" and "0" being "NO POWER!", as that is essentially what those lines are doing... turning the pin output on and off. You can run those last two lines as many times as you like... , and if the pin is on and you try to turn it on again... unsurprisingly nothing will happen. 

Now see if you can attach LEDs other GPIO pins, and make them light up! you should be use most of the pins (highlighted in blue on Joey's table). I would avoid using pins 7,8 & 10 however, as they have other functions that could cause unexpected results. 

I hope you have found this guide helpful, and happy blinking!

TODO: Do a better picture, maybe a more entertaining blink script, maybe a video... showing the wiring?
  Reply
#2
I've made this thread sticky. Excellent write-up... very nice job ! ... thanks pfeerick !
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#3
What about the internal pull-up and pull-down resistors? I'm able to them on the Pi2 port using the RPi.GPIO-PineA64 Python module. Is it possible to set these via sysfs?
  Reply
#4
Very good guide! It has given me an idea for a project - a switchable backlight for the Pinebook keyboard. This facility is available on some more expensive laptops but would be nice to see on the PB.

What would we need:

Physical fitting of LED(s) within the keyboard mechanism
Facility to switch an LED on as descibed above
Allocation of a function key to control this

Of course the effect achieved will depend on the transparency of the keys, we might just get a glow round the edges. An alternative, which I have also seen on some laptops, is an LED placed near the webcam shining down on the keyboard. Some ideas anyway!
  Reply
#5
Thank you for the clear explanation! However, I was wondering how you can control the SPI and I2C bus pins.

I am currently busy with the readout of a temperature sensor. The sensor works on an Arduino and on a Raspberry Pi. But the Pine64 seems to have some trouble with it. I can't "see" the device with the i2cdetect tool either.
Does anyone have some experience with that?
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Controlling a Servo Motor casmiguefl 5 5,860 10-23-2023, 02:48 AM
Last Post: Villetta
  RPi.GPIO python module for Pine A64/A64+ aquilegia 98 129,295 12-15-2022, 08:40 PM
Last Post: Fadazo
  fm transmitter with gpio weasel18 2 4,730 09-10-2019, 04:28 AM
Last Post: desai_amogh
  How to use dts or other setup to declare gpio pin Interrupt (e.g. a button)? dkebler 1 3,531 06-12-2019, 10:37 AM
Last Post: dkebler
Lightbulb Sample GPIO codes highlighting RPi.GPIO-PineA64 and the PI bus MarkHaysHarris777 6 10,918 06-07-2019, 12:37 AM
Last Post: tllim
  Pine64 as jtag programmer desai_amogh 0 1,523 05-25-2019, 06:13 AM
Last Post: desai_amogh
Star GPIO, SPI and I2C C++ Lib databit 7 11,012 02-04-2019, 05:45 AM
Last Post: Jeff R
  GPIO and SPI SamR1 20 31,078 03-15-2018, 10:32 AM
Last Post: jomoengineer
Question GPIO shockr 7 14,524 03-11-2018, 01:52 AM
Last Post: jomoengineer
  Will EPOLL C or PIGPIO C work on pine64? dkebler 0 2,162 02-15-2018, 10:43 AM
Last Post: dkebler

Forum Jump:


Users browsing this thread: 1 Guest(s)