Android: Accessing GPIO Pins from an App

Years ago i bought an Odroid X2 from hardkernel. It has been laying aroung quite some time now and i decided to put it into use.  I got a picture frame from IKEA and placed the Board with it’s 7 inch touch panel (from mcpia) into it.

Problem is, that the display is always on. Even turning down the brightness to 0, keeps the display on. I don’t want to add a button to switch it manually ‚on‘ or ‚off‘. I’d rather have an option to switch the display automatically on in the morning and off in the evening. I did not find a way to achieve this with an app like Tasker. As i knew the screen back light is controlled with an gpio pin, I thought it would be a nice challenge to find a way to switch the display on and off with help of an app.

Odroid X2 picture frame front
Odroid X2 picture frame front
X2 picture frame back
X2 picture frame back

How to access the GPIO Port

Usually you’re able to access the gpio pins within linux with help of files and folders under /sys/class/gpio. The gpio pins are organized in gpiochips. You can make a gpiopin accessible by wirting the number of the port in the file /sys/class/gpio/export. Linux will create a new folder for the gpio pin (e.g. gpio12) and in this folder are some more folders like direction or value to manipulate the gpio pin.

The tricky part is, to find out which gpio pin is the one, turning on and off the back light of the screen. A look into the „porting to android howto“ revealed:

[Define GPIO ports]
– LCD Backlight Enable port : GPA1.3

But how does this port GPA1.3 map to an gpio in /sys/class/gpio? The following command retrieves the label of all gpiochips accessible in the /sys/class folder.

for f in `ls -d /sys/class/gpio/gpiochip*`; do echo $f `cat $f/label $f/base $f/ngpio` ; done

This command returns all lables, one per line, and one line is particularly interesting:

/sys/class/gpio/gpiochip0 GPIO A 9 6

We found our gpiochip with the label ‚A‘. The number next to the ‚A‘ tells us, the base number for the gpio pins within this chip. The first gpio pin of gpiochip ‚A‘ is the number 9. We want gpio pin ‚3‘ of gpiochip ‚A‘, so we add 3 to the base and this gives us 9+3 = 12.  No let’s try if we can access the port and test if we can switch on and off the backlight.

First, we’ll tell the kernel to export the gpio pin for us. Therefor we echo the value 12 into the export file:

echo 12 > /sys/class/gpio/export

Now a new directory ‚gpio12‘ is created. Within this directory we have a file ‚value‘ to access the value. When we put ‚0‘ into the file ‚value‘, the screen should turn off.

echo 0 > /sys/class/gpio/gpio12/value

Which it does, success! Now, let’s try if the screen turns back on again?

echo 0 > /sys/class/gpio/gpio12/value

Yes! Awesome!

Writing the App

Next, we need to access the port from an app. As my board is rooted, the Java code to do this is pretty straight forward:

All we need to do, is the same thing we did on the command line:

  1. Get root rights
  2. Export the gpio pin
  3. Put a value to switch the port on and off
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("echo 12 > /sys/class/gpio/export\n");
os.writeBytes("echo 0 > /sys/class/gpio/gpio12/value\n");

The screen is turned off – Yeah!

I created a small android app, that switches the screen off when it is started and displays a big fullscreen button. When I tap the button, the screen is turned back on and the application closes. Now i can turn the screen off when i click on the app icon, and turn the screen back on, when i click the screen.

To automate this process i created a Tasker Script to start my app in the evening and close it in the morning.