GPIO

From TinkerBoard Wiki
Jump to: navigation, search

Hardware

GPIO

Gpio-pinout.png

Gpio-pinout-expanded.png

In addition the on board LEDs are at:

GPIO.ASUS

  • PWR_LED 3
  • ACT_LED 48
  • LED1_LED 49

GPIO API

Shell

GPIO can be used direct from the shell:

 # echo 223 > /sys/class/gpio/export 
 # echo out > /sys/class/gpio/gpio223/direction 
 # echo 0 > /sys/class/gpio/gpio223/value 
 # echo 1 > /sys/class/gpio/gpio223/value 
 # echo in > /sys/class/gpio/gpio223/direction 
 # cat /sys/class/gpio/gpio223/value
 # echo 223 > /sys/class/gpio/unexport

Status of all GPIO can be read as follows:

 sudo gpio readall #need GPIO_C_Library

which returns a table:

Gpio-table.png

Python

Python is a programming language that lets you work quickly and integrate systems more effectively.

1. Setup Python GPIO library for tinker board

Python GPIO library

http://dlcdnet.asus.com/pub/ASUS/mb/Linux/Tinker_Board_2GB/GPIO_API_for_Python.zip

sudo apt-get install python-dev
unzip ASUSTinkerBoard.gpio-0.1.zip 
cd ASUSTinkerBoard.gpio-0.1/ 
sudo Python setup.py install

2. Reference code

There is sample code in the folder /home/linaro/ASUSTinkerBoard.gpio-0.1/

  • add_event_callback.py - add_event_detect function for input GPIO
  • btc.py - unit test for all functions of GPIO
  • forloop.py - pull high all GPIO and then pull down all GPIO
  • pwm.py - Software PWM function test
  • pwm_input.py - Software PWM function test by raw_input


More examples here: https://github.com/sabrigultekin/Asus-Tinker-Board

  • Led Blink (Digital Output)
  • Pir and Touch Sensor (Digital Input)
  • Servo Motor Control (PWM)
  • Seven Segment Control with SPI
  • ADC Click Control with SPI
  • MZ80 Distance Sensor
  • Relay Board

Sample LED blink:

import ASUS.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.ASUS)

LED = 164
GPIO.setup(LED,GPIO.OUT)

try:
	while True:
		print "led on"
		GPIO.output(LED,GPIO.HIGH)
                time.sleep(1)
 
                print "led off"
		GPIO.output(LED,GPIO.LOW)
                time.sleep(1) 
  
except KeyboardInterrupt:
	GPIO.cleanup()

To create the script write 'nano led.py' in cli. Copy the above script inside the led.py file. Run the file with command 'sudo python led.py'.

Alternatively, to avoid using python in 'sudo python led.py' command one should insert the following command above the import ASUS.GPIO as GPIO line in led.py file:

#!/usr/bin/env python

To run the latter script first execute the command 'chmod +x led.py' and then 'sudo ./led.py' . To exit the script use Ctrl+c combination.

One trick to be aware of while runnig this script: the endstate in which the led light stays can be on or off depending on the the moment you choose to press Ctrl+c combination.

C

Open a terminal and download C GPIO library and then unzip it.

http://dlcdnet.asus.com/pub/ASUS/mb/Linux/Tinker_Board_2GB/GPIO_API_for_C.ZIP

Install C GPIO library for Tinker Board with

sudo chmod +x build

sudo ./build

To make a simple script create a file with 'nano led.c' and input the following code.

Sample LED blink:

#include <stdio.h>
#include <wiringPi.h>

// #define LED 0 matches with ASUS_GPIO 164! This can be checked with command 'sudo gpio readall'.
#define LED     0

int main (void)
        {
        printf ("TB blink\n");

        wiringPiSetup ();
        pinMode (LED, OUTPUT);

        for (;;)
        {
                printf ("led on\n");
                digitalWrite (LED, HIGH);
                delay (500);
                printf ("led off\n");
                digitalWrite (LED, LOW);
                delay (500);
         }
        return 0;
}

To run the script run the command:

gcc -o led led.c -lwiringPi

To run the newly compiled led run the command 'sudo ./led'.