Having Fun with GPIO on Android

The ODROID-C1/C1+, ODROID-C2, and ODROID-XU4 have on-board GPIO (General Purpose Input/Output) pins that allow the control of external devices through software. In order to access the GPIO port properly, you must install the Android Marshmallow version 2.8 image or higher on the ODROID-C2, the Android KitKat version 3.2 image or higher on the ODROID-C1/C1+, and either the Android KitKat version 6.0 image or higher, or the Android Nougat version 1.3 20171214 image or higher on the ODROID-XU4.

This WiKi explains how to make an Android app which can access GPIO ports. You need to install Google Android Studio on your host PC. Add NDK and tools first before starting below steps. We tested the following steps on Android Studio 2.3 and NDK R14.

Ubuntu/Linux

You can find the Android SDK location on this menu (File → Settings → Appearance & Behavior → System Settings → Android SDK)

Figure 1 - Android SDK location
Figure 1 – Android SDK location

After editing the bashrc file, you have to login again or type “source ~/.bashrc” on the command line. Then, download the WiringPi NDK library and App source code Project from GitHub.

ODROID-C1+/C2

$ sudo apt install git
$ git clone https://github.com/codewalkerster/example-wiringPi -b odroid-c

ODROID-XU4

$ sudo apt install git
$ git clone https://github.com/codewalkerster/example-wiringPi -b odroid-xu
Run Android Studio and open the downloaded project.

Figure 2 - Opening the wiringPi project
Figure 2 – Opening the wiringPi project

Build the project to make an apk package

Select Build -> Make Project from the menu. You will see a couple of error messages, and will need to click the following options to complete the build process, which will produce an APK file to run on your ODROID:

  • Install missing platform(s) and sync project
  • Install Build Tools 25.0.2 and sync project

Windows

In Window, set the environment PATH to point to the NDK folder path, then reboot Windows.

Figure 3 - Setting the environment path to point to the NDK folder path
Figure 3 – Setting the environment path to point to the NDK folder path

Figure 4 - Setting the environment path to point to the NDK folder path
Figure 4 – Setting the environment path to point to the NDK folder path

Figure 5 - Setting the environment path to point to the NDK folder path
Figure 5 – Setting the environment path to point to the NDK folder path

Next, install the Git client program from https://git-for-windows.github.io/, and clone the wiringPi project, as shown in Figure 6.

Figure 6 - Cloning the project
Figure 6 – Cloning the project

The project is available at https://github.com/codewalkerster/example-wiringPi. Select origin/odroid-c or origin/odroid-xu.

Figure 7 - Checking out the GitHub branch
Figure 7 – Checking out the GitHub branch

Then, install the NDK by selecting Tools -> Android -> SDK Manager from the menu.

Figure 8 - Installing the SDK
Figure 8 – Installing the SDK

Figure 9 - Installing the SDK
Figure 9 – Installing the SDK

Features of the example project

You can read the analog-to-digital converter (ADC) value and show the voltage level with 19 LEDs on the GPIO output. You can view a demo video at https://youtu.be/lGqyhvd3q9U and https://youtu.be/lGqyhvd3q9U.

Figure 10 - Reading the ADC value on the ODROID-C1+/C2
Figure 10 – Reading the ADC value on the ODROID-C1+/C2

Figure 11 - Reading the ADC value on the ODROID-XU4
Figure 11 – Reading the ADC value on the ODROID-XU4

PWM

Figure 12 shows a basic PWM control example. You can choose the number of PWM outputs (1 or 2), as well as control the frequency and duty ratio.

Figure 12 - Basic PWM control example
Figure 12 – Basic PWM control example

Gmail Notifier example

This is a fun and useful project using the PWM port. When you are watching videos or playing games, you may miss notification of an important email or message. The flag is moved by servo motor which is connected to a PWM pin on 40pin GPIO port. The code may be downloaded from https://github.com/codewalkerster/GMailNotifier. A demo of the project may be viewed at https://youtu.be/Vvq77w87RWQ.

I2C

Figure 15 shows example code for accessing our Weather Board is available in order to measure the temperature, humidity, atmospheric pressure, altitude and visible/invisible light intensities via I2C interface.

Figure 13 - Hardkernel’s Weather Board measures environmental statistics
Figure 13 – Hardkernel’s Weather Board measures environmental statistics

Figure 14 - Hardkernel’s Weather Board measures environmental statistics
Figure 14 – Hardkernel’s Weather Board measures environmental statistics

Figure 15 - A demo software for Sending and Receiving characters via UART interface
Figure 15 – A demo software for Sending and Receiving characters via UART interface

Figure 16 - Demo software to access 1-wire protocol interfaced DS18S20 temperature sensor
Figure 16 – Demo software to access 1-wire protocol interfaced DS18S20 temperature sensor

Kernel for I2C

Open the File Manager app, and edit the file /storage/internal/boot.ini as shown below:

Original

movi read dtb 0 ${dtbaddr}
# load kernel from vat or boot partition.
movi read boot 0 ${loadaddr}
#fatload mmc 0:1 ${loadaddr} Image
booti ${loadaddr} - ${dtbaddr}
After edit
movi read dtb 0 ${dtbaddr}
# load kernel from vat or boot partition.
#movi read boot 0 ${loadaddr}
fatload mmc 0:1 ${loadaddr} Image
booti ${loadaddr} - ${dtbaddr}
Load the kernel image from the vfat partition built i2c. If you cannot find the “fatload” command, remove /storage/internal/boot.ini file and reboot the system. For comments, questions and suggestions, please visit the original Wiki article at https://wiki.odroid.com/odroid-c2/application_note/gpio/enhancement_40pins_on_android.

Be the first to comment

Leave a Reply