Difference between revisions of "Basic GPIO with PICO APL3"

From AAEON Community Wiki
Jump to: navigation, search
(Created page with "== Basic GPIO Control from a Shell Script == The AAEON PICO-APL3 board features 4 GPIO pins that are available for the user. These signals are accessible on the 6-pin header...")
(No difference)

Revision as of 09:48, 12 December 2018

Basic GPIO Control from a Shell Script

The AAEON PICO-APL3 board features 4 GPIO pins that are available for the user. These signals are accessible on the 6-pin header CN60:


The pins in the header are mapped as shown in the following table:


There are different ways in which these pins can be controlled from a user application (e.g. in Python) but the goal of this tutorial is to describe the steps required to perform this control in a very simple way from a Linux terminal using the command line, or maybe a shell script. The proposed method uses the sysfs interface to control the digital signals. The signals are mapped to the directory


The user can control these signals by reading or writing different virtual files that are present in this directory. It should be noted that these operations require root access.

GPIO Setup

In order to make available the GPIO signals the first thing to do is to ‘export’ the desired signal numbers. This means writing the signal number to the /sys/class/gpio/export file. For example, if we want to use GPIO3:

# echo 3 > /sys/class/gpio/export

After running this command a new directory will show up, /sys/class/gpio/gpio3. The next step is to establish the type of digital signal the we want, input or output. This setup is achieved by writing to the /sys/class/gpio/gpioNN/direction file (where NN is the signal number). In our case, for GPIO3:

•	Input:		# echo “in” > /sys/class/gpio/gpio3/direction
•	Output:		# echo “out” > /sys/class/gpio/gpio3/direction

Input and Output Operations

Once the GPIO signal is setup the interaction with the physical signals boil down to accessing the file /sys/class/gpio/gpioNN/value. Depending on the type of signal we need to read from this file or write to it. Continuing with the GPIO3 example:

•	Read input:	# cat /sys/class/gpio/gpio3/value
 		# 1 (or ‘0’, depending on the logical value present at GPIO3)
•	Write output:	# echo 1 > /sys/class/gpio/gpio3/value (to set GPIO3 high)
		# echo 0 > /sys/class/gpio/gpio3/value (to set GPIO3 low)

GPIO Release

When the use of the GPIO signals is no longer necessary, it is good practice to release them. For GPIO3 the command is:

# echo 3 > /sys/class/gpio/unexport

Example Script: blink.sh

As a way of illustrating the concepts exposed so far, a shell script is provided that configures one of the GPIO signals as output and toggles its value with a 1-second period (0.3s ON, 0.7s OFF), until the user presses Ctrl-C. Please remember that this script must be executed as root. Also, the GPIO used must be passed as a parameter when the script is called. For example, to toggle GPIO2 the script should be called as follows:

# ./blink 2

# blink.sh – Turn GPIO on and off indefinitely.


	if [ ! -e $GPIO_PATH/gpio$1 ]; then
		echo "$1" > $GPIO_PATH/export

	if [ -e $GPIO_PATH/gpio$1 ]; then
		echo "$1" > $GPIO_PATH/unexport

	echo $2 > $GPIO_PATH/gpio$1/direction

	echo $2 > $GPIO_PATH/gpio$1/value

	setOutState $PIN $OFF
	unexportPin $PIN
	echo "Shutting down $0..."
	exit 0

trap shutDown SIGINT

# Setup output pin
exportPin $PIN
setDirection $PIN $OUT
setOutState $PIN $OFF

# Loop until the user hits Ctrl-C
while [ 1 ]
	# Turn on LED
	setOutState $PIN $ON
	# Pause
	sleep 0.3
	# Turn off LED
	setOutState $PIN $OFF
	# Pause
	sleep 0.7

Demo Circuit

The real utility of the GPIO signals is to allow the PICO board to interact with external devices. Some very simple examples that come to mind are push-buttons (digital inputs) and light indicators (digital outputs). The circuit presented here allows to use the output managed by the script blink.sh to switch on and off an LED. Due to the limitations of the GPIO pins in the PICO board, we cannot connect an LED directly to a GPIO output: the current supplied by the GPIO is not enough to drive an LED properly. For this reason a transistor is used as current amplifier (a general purpose NPN bipolar transistor is well suited, e.g. 2N3904). The 3.3V power present in connector CN60 is used to supply the circuit. The following picture shows how to wire the circuit to GPIO header on the PICO board:

//image 2

The GPIO0 used in this case is GPIO0 (green wire), so the script should be run as follows:

# ./blink 0