RPi GPIO Code Samples

The Raspberry Pi GPIOs can be controlled using many programming languages.

= C =

Examples in different C-Languages.

Direct register access
Gert van Loo & Dom, have provided some tested code which accesses the GPIO pins through direct GPIO register manipulation in C-code. (Thanks to Dom for doing the difficult work of finding and testing the mapping.)

Note: For Raspberry Pi 2 and Pi 3, change BCM2708_PERI_BASE to 0x3F000000 for the code to work.

Example GPIO code:

GPIO Pull Up/Pull Down Register Example

// enable pull-up on GPIO24&25 GPIO_PULL = 2; short_wait; // clock on GPIO 24 & 25 (bit 24 & 25 set) GPIO_PULLCLK0 = 0x03000000; short_wait; GPIO_PULL = 0; GPIO_PULLCLK0 = 0;

WiringPi
Get and install wiringPi: http://wiringpi.com/download-and-install/

Save this, and compile with: gcc -o blink blink.c -lwiringPi and run with: sudo ./blink

sysfs
The following example requires no special libraries, it uses the available sysfs interface.

bcm2835 library
This must be done as root. To change to the root user: sudo -i You must also get and install the bcm2835 library, which supports GPIO and SPI interfaces. Details and downloads from http://www.open.com.au/mikem/bcm2835

pigpio
pigpio provides all the standard GPIO features.

In addition it provides hardware timed PWM suitable for servos, LEDs, and motors and samples/timestamps GPIOs 0-31 up to 1 million times per second (default 200 thousand).

C documentation: http://abyz.co.uk/rpi/pigpio/cif.html

Download and install pigpio: http://abyz.co.uk/rpi/pigpio/download.html

Compile gcc -o pulse pulse.c -lpigpio -lrt -lpthread

Run sudo ./pulse

= C# =

RaspberryGPIOManager is a very basic C# library to control the GPIO pins via the GPIOPinDriver object. See: https://github.com/AlexSartori/RaspberryGPIOManager

RaspberryPiDotNet library is available at https://github.com/cypherkey/RaspberryPi.Net/. This more advanced library includes a GPIOFile and GPIOMem class. The GPIOMem requires compiling Mike McCauley's bcm2835 library above in to a shared object.

= Ruby =

This example uses the WiringPi Ruby Gem: http://pi.gadgetoid.co.uk/post/015-wiringpi-now-with-serial which you can install on your Pi with "gem install wiringpi"

Alternatively the Pi Piper Gem (https://github.com/jwhitehorn/pi_piper) allows for event driven programming:

= Perl = This must be done as root. To change to the root user: sudo su - Supports GPIO and SPI interfaces. You must also get and install the bcm2835 library. Details and downloads from http://www.open.com.au/mikem/bcm2835 You must then get and install the Device::BCM2835 perl library from CPAN http://search.cpan.org/~mikem/Device-BCM2835-1.0/lib/Device/BCM2835.pm

= Python =

RPi.GPIO
The RPi.GPIO module is installed by default in Raspbian. Any RPi.GPIO script must be run as root.

More documentation is available at http://sourceforge.net/p/raspberry-gpio-python/wiki/Home/

pigpio
pigpio Python scripts may be run on Windows, Macs, and Linux machines. Only the pigpio daemon needs to be running on the Pi.

pigpio provides all the standard gpio features.

In addition it provides hardware timed PWM suitable for servos, LEDs, and motors and samples/timestamps gpios 0-31 up to 1 million times per second (default 200 thousand).

Python documentation: http://abyz.co.uk/rpi/pigpio/python.html

Download and install pigpio: http://abyz.co.uk/rpi/pigpio/download.html

pigpio scripts require that the pigpio daemon be running.

sudo pigpiod

They do not need to be run as root. ./pulse.py

RPIO
Also available is RPIO at https://pypi.python.org/pypi/RPIO

RPIO extends RPi.GPIO with TCP socket interrupts, command line tools and more.

WiringPi2-Python
There's a Python-wrapped version of Gordon Henderson's WiringPi. See here. import wiringpi2

wiringpi2.wiringPiSetupGpio # For GPIO pin numbering
 * 1) wiringpi2.wiringPiSetup # For sequential pin numbering, one of these MUST be called before using IO functions
 * OR
 * 1) wiringpi2.wiringPiSetupSys # For /sys/class/gpio with GPIO pin numbering
 * OR

wiringpi2.pinMode(6,1) # Set pin 6 to 1 ( OUTPUT ) wiringpi2.digitalWrite(6,1) # Write 1 ( HIGH ) to pin 6 wiringpi2.digitalRead(6) # Read pin 6

= Scratch =

Scratch using the ScratchGPIO
Scratch can be used to control the GPIO pins using a background Python handler available from http://cymplecy.wordpress.com/2013/04/22/scratch-gpio-version-2-introduction-for-beginners/

Pridopia Scratch Rs-Pi-GPIO driver
Scratch control GPIO (use GPIO number not P1 pin number can support GPIO 28,29,30,31) support I²C 23017 8/16/32/64/128 GPIO, I²C TMP102 Temp sensor, I²C RTC DS1307, I²C ADC ADS1015, I²C PWM, I²C EEPROM 24c32, I²C BMP085 Barometric Pressure/Temperature/Altitude Sensor, GPIO input/output, DC motor, Relay, I²C 16x16 LED matrix, I²C 24x16 Matrix, 84x48 pixels LCD, 16x2 character LCD, 20x4 character LCD, 1-Wire 18B20 Temp Sensor, Ultra Sonic distance sensor, available from

http://www.pridopia.co.uk/rs-pi-set-scratch.html

RpiScratchIO
Generic interface for GPIO or other I/O operations. The package allows user modules to be easily added and loaded, to interface with any I/O device. The code is written in Python and uses the scratchpy Python package to interface with Scratch.



RpiScratchIO - Installation
To install the package on the latest Raspbian installation type, sudo apt-get install -y python-setuptools python-dev sudo easy_install pip sudo pip install RpiScratchIO

RpiScratchIO - Documentation and examples
More information can be found at https://pypi.python.org/pypi/RpiScratchIO/ The package is also documented in Issues 20 and 22 of The MagPi. RpiScratchIO is the basis of a new BrickPi Scratch handler, which is documented in Issue 23 of The MagPi.



= Java =

Java using the Pi4J Library
This uses the Java library available at http://www.pi4j.com/. (Any Java application that controls GPIO must be run as root.)

Please note that the Pi4J library uses the WiringPi GPIO pin numbering scheme. Please see the usage documentation for more details: http://pi4j.com/usage.html

More complete and detailed examples are included on the Pi4J website at http://www.pi4j.com/.

The Pi4J library includes support for:
 * GPIO Control
 * GPIO Listeners
 * Serial Communication
 * I2C Communication
 * SPI Communication

Java
This uses the Java library available at https://github.com/jkransen/framboos. It does not depend on (or use) the wiringPi driver, but uses the same numbering scheme. Instead it uses the default driver under /sys/class/gpio that ships with the distro, so it works out of the box. Any Java application that controls GPIO must be run as root.

Java Webapp GPIO web control via HTTP
This uses the Java Webapp available at https://bitbucket.org/sbub/raspberry-pi-gpio-web-control/overview. You can control your GPIO over the Internet. Any Java application that controls GPIO must be run as root.

host:~ sb$ curl 'http://raspberrypi:8080/handle?g0=1&g1=0' {"g1":0,"g0":1}

= Shell =

sysfs, part of the raspbian operating system
For operating system versions prior to the raspbian Jessie release, the export and unexport of pins must be done as root. Since the raspbian Jessie release the pi user is a member of the group "gpio" and so control of the GPIO no longer requires a change to the root user. With Jessie, if using a script as in the code below, you'll need to put a 'sleep 1' command in between your 'export' and 'direction' commands to allow time for the operating system to set up the GPIO number specific direction file.

wiringPi - gpio utility
You need the wiringPi library from https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/. Once installed, there is a new command gpio which can be used as a non-root user to control the GPIO pins.

The man page man gpio has full details, but briefly:

gpio -g mode 17 out gpio -g mode 18 pwm

gpio -g write 17 1 gpio -g pwm 18 512

The -g flag tells the gpio program to use the BCM GPIO pin numbering scheme (otherwise it will use the wiringPi numbering scheme by default).

The gpio command can also control the internal pull-up and pull-down resistors:

gpio -g mode 17 up

This sets the pull-up resistor - however any change of mode, even setting a pin that's already set as an input to an input will remove the pull-up/pull-down resistors, so they may need to be reset.

Additionally, it can export/un-export the GPIO devices for use by other non-root programms - e.g. Python scripts. (Although you may need to drop the calls to GPIO.Setup in the Python scripts, and do the setup separately in a little shell script, or call the gpio program from inside Python).

gpio export 17 out gpio export 18 in These exports GPIO-17 and sets it to output, and exports GPIO-18 and sets it to input.

And when done:

gpio unexport 17 The export/unexport commands always use the BCM GPIO pin numbers regardless of the presence of the -g flag or not.

If you want to use the internal pull-up/down's with the /sys/class/gpio mechanisms, then you can set them after exporting them. So:

gpio -g export 4 in gpio -g mode 4 up

You can then use GPIO-4 as an input in your Python, Shell, Java, etc. programs without the use of an external resistor to pull the pin high. (If that's what you were after - for example, a simple push button switch taking the pin to ground.)

A fully working example of a shell script using the GPIO pins can be found at http://project-downloads.drogon.net/files/gpioExamples/tuxx.sh.

pigpio - pigs utility
pigpio provides a command line utility pigs.

pigs provides all the standard gpio features.

In addition it provides hardware timed PWM suitable for servos, LEDs, and motors.

Use man pigs or view the pigs documentation at http://abyz.co.uk/rpi/pigpio/pigs.html

Download and install pigpio: http://abyz.co.uk/rpi/pigpio/download.html

Additional error information may be reported to /dev/pigerr. The following command will display any errors. cat /dev/pigerr & Examples pigs modes 4 w # set gpio4 as output (write) pigs modes 17 w # set gpio17 as output (write) pigs modes 23 r # set gpio23 as input (read) pigs modes 24 0 # set gpio24 as ALT0

pigs servo 4 1500 # start 1500 us servo pulses on gpio4

pigs pwm 17 192 # start 75% dutycycle PWM on gpio17 (192/255 = 75%)

pigs r 23 # read gpio23

pigs w 24 1 # write 1 to gpio 24


 * 1) servo may be abbreviated to s
 * 2) pwm may be abbreviated to p
 * 3) modes may be abbreviated to m

pigs s 4 0 # stop servo pulses

pigs p 17 0 # stop pwm

pigs requires that the pigpio daemon be running.

sudo pigpiod

For an example pigs script see x_pigs in the pigpio archive.

pigpio - /dev/pigpio interface
pigpio provides command line access via the /dev/pigpio pipe.

/dev/pigpio provides all the standard gpio features.

In addition it provides hardware timed PWM suitable for servos, LEDs, and motors.

The command set is identical to that used by pigs. Use man pigs or view the pigs documentation at http://abyz.co.uk/rpi/pigpio/pigs.html

Download and install pigpio: http://abyz.co.uk/rpi/pigpio/download.html

The result of /dev/pigpio commands are written to /dev/pigout. The following command will display the results. cat /dev/pigout & Errors are reported to /dev/pigerr. The following command will display any errors. cat /dev/pigerr & Examples echo modes 4 w >/dev/pigpio # set gpio4 as output (write) echo modes 17 w >/dev/pigpio # set gpio17 as output (write) echo modes 23 r >/dev/pigpio # set gpio23 as input (read) echo modes 24 0 >/dev/pigpio # set gpio24 as ALT0

echo servo 4 1500 >/dev/pigpio # start 1500 us servo pulses on gpio4

echo pwm 17 192 >/dev/pigpio # start 75% dutycycle PWM on gpio17 (192/255 = 75%)

echo r 23 >/dev/pigpio # read gpio23

echo w 24 1 >/dev/pigpio # write 1 to gpio 24


 * 1) servo may be abbreviated to s
 * 2) pwm may be abbreviated to p
 * 3) modes may be abbreviated to m

echo s 4 0 >/dev/pigpio # stop servo pulses

echo p 17 0 >/dev/pigpio # stop pwm

The /dev/pigpio pipe interface requires that the pigpio daemon be running.

sudo pigpiod

For an example /dev/pigpio pipe script see x_pipe in the pigpio archive.

= Lazarus / Free Pascal =

The GPIO pins are accessible from Lazarus without any third-party software. This is performed by means of the BaseUnix unit that is part of every distribution of Lazarus and Free Pascal or by invoking Unix shell commands with fpsystem. The following example uses GPIO pin 17 as output port. It is assumed that you created a form named GPIO17ToggleBox with a TToggleBox and a TMemo named LogMemo (optional, for logging purposes). The program has to be executed with root privileges.

Unit for controlling the GPIO port: unit Unit1;

{Demo application for GPIO on Raspberry Pi} {Inspired by the Python input/output demo application by Gareth Halfacree} {written for the Raspberry Pi User Guide, ISBN 978-1-118-46446-5}

{$mode objfpc}{$H+}

interface

uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Unix, BaseUnix;

type

{ TForm1 }

TForm1 = class(TForm) LogMemo: TMemo; GPIO17ToggleBox: TToggleBox; procedure FormActivate(Sender: TObject); procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); procedure GPIO17ToggleBoxChange(Sender: TObject); private { private declarations } public { public declarations } end;

const PIN_17: PChar = '17'; PIN_ON: PChar = '1'; PIN_OFF: PChar = '0'; OUT_DIRECTION: PChar = 'out';

var Form1: TForm1; gReturnCode: longint; {stores the result of the IO operation}

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormActivate(Sender: TObject); var fileDesc: integer; begin { Prepare SoC pin 17 (pin 11 on GPIO port) for access: } try fileDesc := fpopen('/sys/class/gpio/export', O_WrOnly); gReturnCode := fpwrite(fileDesc, PIN_17[0], 2); LogMemo.Lines.Add(IntToStr(gReturnCode)); finally gReturnCode := fpclose(fileDesc); LogMemo.Lines.Add(IntToStr(gReturnCode)); end; { Set SoC pin 17 as output: } try fileDesc := fpopen('/sys/class/gpio/gpio17/direction', O_WrOnly); gReturnCode := fpwrite(fileDesc, OUT_DIRECTION[0], 3); LogMemo.Lines.Add(IntToStr(gReturnCode)); finally gReturnCode := fpclose(fileDesc); LogMemo.Lines.Add(IntToStr(gReturnCode)); end; end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); var fileDesc: integer; begin { Free SoC pin 17: } try fileDesc := fpopen('/sys/class/gpio/unexport', O_WrOnly); gReturnCode := fpwrite(fileDesc, PIN_17[0], 2); LogMemo.Lines.Add(IntToStr(gReturnCode)); finally gReturnCode := fpclose(fileDesc); LogMemo.Lines.Add(IntToStr(gReturnCode)); end; end;

procedure TForm1.GPIO17ToggleBoxChange(Sender: TObject); var fileDesc: integer; begin if GPIO17ToggleBox.Checked then begin { Swith SoC pin 17 on: } try fileDesc := fpopen('/sys/class/gpio/gpio17/value', O_WrOnly); gReturnCode := fpwrite(fileDesc, PIN_ON[0], 1); LogMemo.Lines.Add(IntToStr(gReturnCode)); finally gReturnCode := fpclose(fileDesc); LogMemo.Lines.Add(IntToStr(gReturnCode)); end; end else begin { Switch SoC pin 17 off: } try fileDesc := fpopen('/sys/class/gpio/gpio17/value', O_WrOnly); gReturnCode := fpwrite(fileDesc, PIN_OFF[0], 1); LogMemo.Lines.Add(IntToStr(gReturnCode)); finally gReturnCode := fpclose(fileDesc); LogMemo.Lines.Add(IntToStr(gReturnCode)); end; end; end;

end.

Main program: program io_test;

{$mode objfpc}{$H+}

uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Interfaces, // this includes the LCL widgetset Forms, Unit1 { you can add units after this };

{$R *.res}

begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.

Alternative way to access the GPIO port with Lazarus / Free Pascal is by using Lazarus wrapper unit for Gordon Henderson's wiringPi C library or encapsulated shell calls.

The Lazarus wiki describes a demo program that can read the status of a GPIO pin.

= BASIC =

BASIC - Return to BASIC
'Return to Basic' (RTB) can be found here.

It is a new BASIC featuring modern looping constructs, switch statements, named procedures and functions as well as graphics (Cartesian and turtle), file handling and more. It also supports the Pi's on-board GPIO without needing to be run as root. (You do not need any special setup routines either)

Sample blink program: // blink.rtb: //   Blink program in Return to Basic //   Gordon Henderson, projects@drogon.net // PinMode (0, 1) // Output CYCLE DigitalWrite (0, 1) // Pin 0 ON WAIT (0.5) // 0.5 seconds DigitalWrite (0, 0) WAIT (0.5) REPEAT END

Bywater BASIC Interpreter
The Bywater BASIC Interpreter (bwBASIC) implements a large superset of the ANSI Standard for Minimal BASIC (X3.60-1978) and a significant subset of the ANSI Standard for Full BASIC (X3.113-1987) in C. It also offers shell programming facilities as an extension of BASIC. bwBASIC seeks to be as portable as possible and is downloadable.

BASIC programming of the I/O
Setting up a GPIO pin to be used for inputs or for outputs.

The control words cannot be loaded directly into the 32 bit ARM registers with 32 bit addresses, as bwBASIC has no POKE and PEEK commands and other versions of BASIC only handle 8 bit registers with 16 bit addresses with these commands. So the GPIO pins need to be exported so that they exist in a file structure which can be accessed from basic with the OPEN command (ref 2).

This must be done in Linux root. BASIC must be run in the root too. sudo -1 sudo bwbasic

Now to export GPIO pin 4 for example, using a Shell command. echo "4" > /sys/class/gpio/export

Whilst bwbasic can accommodate shell commands, and we can store a set of these commands (eg. to export a number of GPIO pins at the outset) as numbered statements in a file that can be loaded with the basic command LOAD "filename" and RUN (ref 2), the shell commands have to run as a separate file, as they cannot be run from within, as part of a basic program.

Now the file containing the pin direction setting from BASIC can be accessed.

GPIO pin 4 can be set to input or output by OPENing its pin direction file for output and writing "in" or "out" with a PRINT# command.

This closes the open direction file, whereupon the system performs the action of setting the direction to "out". The system only carries out the action as the file is closed.

Now the output of the gpio 4 pin can be controlled from BASIC.

GPIO 4 pin can be set to 1 or to 0 by OPENing its pin value file for output and writing "1" or "0" with a PRINT# command. Similarly the output of GPIO pin 4 can be turned off.

Example of an (unstructured) BASIC program
To read the state of a switch and control the power to two LEDs connected to GPIO pins 8,7 and 4 respectively.

Program to set 2 pins as outputs and 1 pin as input and to read the input turning on two different combinations of the two outputs (ie output 0,1 or 1,0) depending on the state of the input (1 or 0).

NEW clears the export.bas program from memory

When all is done, the GPIO pins should be unexported, to leave the R-Pi as we found it. A simple circuit to provide the switched input and the two LED outputs.

For the two original documents this example has been copied from, see:
 * 1) [[Media:GPIO_Driving_Example_(BASIC)_.doc |GPIO_Driving_Example_(BASIC)_.doc]]
 * 2) [[Media:Raspberry_Pi_I-O_viii.doc | Raspberry_Pi_I-O_viii.doc]]

BBC BASIC
BBC BASIC for the RPi 2 or RPi 3 (running Raspbian) can be found here.

BBC BASIC supports the RPi's GPIO via the supplied gpiolib library, it does not need to be run as root.

Here is a simple 'blink' program: REM blink.bbc REM Simple blink program in BBC BASIC REM by Richard Russell, http://www.rtrussell.co.uk/

INSTALL @lib$+"gpiolib"

REM Initialise GPIO: gpio% = FN_gpio_setup

REM Set GPIO pin 7 to output mode: PROC_gpio_inp(gpio%, 7) : REM Must use PROC_gpio_inp before PROC_gpio_out PROC_gpio_out(gpio%, 7)

pin7% = %10000000 : REM Bit mask REPEAT PROC_gpio_set(gpio%, pin7%) WAIT 50 : REM 0.5 seconds PROC_gpio_clr(gpio%, pin7%) WAIT 50 UNTIL FALSE END

= Gambas =

Gambas is a visual programming language for Linux and is similar in some ways to Visual Basic.

Gambas can make use of C libraries such as wiringPi and pigpio.

Here is some minimal code to get started, which assumes the required library (either wiringPi or pigpio, as applicable) is installed.

Gambas with wiringPi
A few Declarations:- Library "/usr/local/lib/libwiringPi"

Public Extern wiringPiSetup As Integer    'initialises wiringPi (assumes calling program uses virtual Pin numbers) Public Extern pinMode(pin As Integer, pud As Integer)  'sets Pin mode to either INPUT or OUTPUT Public Extern digitalRead(pin As Integer) As Integer   'returns the input state (low=0, high=1) Public Extern digitalWrite(pin As Integer, value As Integer)   'sets the output state

Some code:- Public Sub Form_Open wiringPiSetup Catch Me.text = "WiringPi setup failed: must be run as root" End If we want to set GPIO pin 2 to be an output and set low:- pinMode(2,1) digitalWrite(2,0) If we want to read GPIO pin 4:- pinMode(4,0) Me.Text = "pin 4 logic level: " & digitalRead(4)

More details here: http://captainbodgit.blogspot.co.uk/2013/11/raspberrypi-gpio-using-gambas.html

Gambas with pigpio
A few Declarations:-

Library "/usr/local/lib/libpigpio"

Public Extern gpioInitialise As Integer  'call at start of program to initialise Public Extern gpioTerminate As Integer   'call at end of program to release resources and terminate threads

Public Extern gpioSetMode(iPin As Integer, iMode As Integer) As Integer Public Extern gpioRead(iPin As Integer) As Integer Public Extern gpioWrite(iPin As Integer, iLevel As Integer) As Integer

Some code:- Public Sub Form_Open If gpioInitialise < 0 Then Me.Text = "Failed: must be run as root" Else Me.Text = "Initialised: pigpio" Endif End

Public Sub Form_Close gpioTerminate End If we want to set Pin 6 to be an output and set low:- gpioSetMode(6, 1) gpioWrite(6, 0) If we want to read GPIO pin 7:- gpioSetMode(7,0) Me.Text = "pin 7 logic level: " & gpioRead(7)

More details here: http://captainbodgit.blogspot.co.uk/2016/08/raspberry-pi-gpio-gambas-pigpio-library.html

=Graphical User Interfaces=

WebIOPi
WebIOPi allows to control each GPIO with a simple web interface that can be used with any browser. Available in PHP and Python, they both require root access, but the Python version serves HTTP itself. Each GPIO pin can be set up as input or output and its LOW/HIGH stae can be changed. WebIOPi is fully customizable, so it can be used for home remote control. It also works over Internet. UART/SPI/I²C support will be added later. See code examples above.

=Benchmarks=

Toggling gpios
For information about the relative performance at toggling GPIOs for each approach, and a comparison of original Pi vs. Raspberry Pi 2, see:


 * http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/
 * http://codeandlife.com/2015/03/25/raspberry-pi-2-vs-1-gpio-benchmark/

=Citations=