Difference between revisions of "GPIO"

From Tinker Board Wiki
Jump to: navigation, search
(Python)
 
Line 44: Line 44:
 
''Python GPIO library''
 
''Python GPIO library''
 
  <nowiki>
 
  <nowiki>
 +
sudo apt-get install python-dev
 +
 
git clone http://github.com/TinkerBoard/gpio_lib_python --depth 1 GPIO_API_for_Python
 
git clone http://github.com/TinkerBoard/gpio_lib_python --depth 1 GPIO_API_for_Python
  
sudo apt-get install python-dev
 
unzip GPIO_API_for_Python.zip
 
 
cd GPIO_API_for_Python/
 
cd GPIO_API_for_Python/
 
sudo python setup.py install</nowiki>
 
sudo python setup.py install</nowiki>
Line 112: Line 112:
  
 
=== C ===
 
=== C ===
Open a terminal and download C GPIO library and then unzip it.
+
Open a terminal and download C GPIO library.
 
  <nowiki>git clone http://github.com/TinkerBoard/gpio_lib_c --depth 1 GPIO_API_for_C</nowiki>
 
  <nowiki>git clone http://github.com/TinkerBoard/gpio_lib_c --depth 1 GPIO_API_for_C</nowiki>
  
 
Install C GPIO library for Tinker Board with  
 
Install C GPIO library for Tinker Board with  
  <nowiki>unzip GPIO_API_for_C.zip
+
  <nowiki>
 
cd GPIO_API_for_C/
 
cd GPIO_API_for_C/
 
sudo chmod +x build
 
sudo chmod +x build

Latest revision as of 05:06, 7 November 2018

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

sudo apt-get install python-dev

git clone http://github.com/TinkerBoard/gpio_lib_python --depth 1 GPIO_API_for_Python

cd GPIO_API_for_Python/
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.

git clone http://github.com/TinkerBoard/gpio_lib_c --depth 1 GPIO_API_for_C

Install C GPIO library for Tinker Board with

cd GPIO_API_for_C/
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'.