pigpio library
pigpio pigpio C I/F pigpiod pigpiod C I/F Python pigs piscope Misc Examples Download Site Map

Examples

The following examples show various ways pigpio may be used to communicate with sensors via the GPIO.

Although many are complete programs they are intended to be a starting point in producing your own code, not an end point.

Index

Hardware

Shell code

C code

C++ code

pigpiod_if2 code

Python code

Miscellaneous related code

External links

Hardware


IR Receiver
2013-06-09
Reading an infrared remote receiver.

Light Dependent Resistor
2013-06-09
Measuring brightness with a light dependent resistor (LDR). Improved methods of timing the start of the capacitor recharge are given for C and Python.

Motor Shield
2013-12-15
Using an Arduino motor shield.

Rotary Encoder
2013-06-09
Reading a rotary encoder.

Sonar Ranger
2013-06-10
Measuring range with a sonar ranger.

Shell code


GPIO test
2014-08-11
This bash script tests the user GPIO. Video

C code


Frequency Counter 1
2014-08-20
A program showing how to use the gpioSetAlertFunc function to set a callback for GPIO state changes. A frequency count is generated for each monitored GPIO (frequencies up to 500kHz with a sample rate of 1μs).

Frequency Counter 2
2014-08-20
A program showing how to use the gpioSetGetSamplesFunc function to set a callback for accumulated GPIO state changes over the last millisecond. A frequency count is generated for each monitored GPIO (frequencies up to 500kHz with a sample rate of 1μs). Generally the method used is more complicated but more efficient than frequency counter 1.

Hall effect sensor
2014-06-13
Program to show status changes for a Hall effect sensor.

I2C sniffer
2014-06-15
A program to passively sniff I2C transactions (100kHz bus maximum) and display the results. This C program uses pigpio notifications.

IR Receiver
2015-02-25
Function to hash a code from an IR receiver (reading an IR remote control).

PCF8591 YL-40
2014-08-26
A program to display readings from the (I2C) PCF8591.

Pot + Capacitor Recharge timing
2014-03-14
Function to time capacitor charging (through a resistance). The time can be used to estimate the resistance.

Rotary Encoder
2015-10-03
Function to decode a mechanical rotary encoder.

SPI bit bang read MCP3008 (10-bit ADC)
2016-03-20
This program shows how to read multiple MCP3008 ADC simultaneously with accurately timed intervals. One channel of each ADC may be sampled at up to 25k samples per second.

SPI bit bang read MCP3202 (12-bit ADC)
2016-03-20
This program shows how to read multiple MCP3202 ADC simultaneously with accurately timed intervals. One channel of each ADC may be sampled at up to 25k samples per second.

SPI bit bang read MCP3008 and MCP3202
2016-03-20
This program shows how to read multiple MCP3008 and MCP3202 ADC simultaneously with accurately timed intervals. One channel of each ADC may be sampled at up to 25k samples per second. The 10-bit MCP3008 readings are multiplied by 4 so they have the same range (0-4095) as the 12-bit MCP3202.

Wiegand Reader
2013-12-30
Function to read a Wiegand Reader.

C++ code


IR Receiver
2015-02-22
Class to hash a code from an IR receiver (reading an IR remote control).

Rotary Encoder
2013-12-30
Class to decode a mechanical rotary encoder.

Wiegand Reader
2013-12-30
Class to read a Wiegand Reader.

pigpiod_if2 code

The pigpiod_if2 code examples are linked with libpigpiod_if2 and are written in C.

The pigpiod_if2 library may be compiled and run on any Linux machine and allows control of the GPIO on one or more networked Pis.

It should be possible to adapt the library to run on Macs and PCs.

Each Pi needs the pigpio daemon to be running. The pigpio daemon may be started with the command sudo pigpiod.

433MHz keyfob RX/TX
2015-11-17
Code to read and transmit 313 and 434 MHz key fob codes. The codes to be read must use Manchester encoding. The transmitted codes use Manchester encoding.

./_433D -r10 # Print fob keycodes received on GPIO 10.

./_433D -t5 8246184 # Transmit code on GPIO 5.

./_433D -r10 -t5 8246184 # Transmit code on GPIO 5 then listen for codes

./_433D -? for options.

DHT11/21/22/33/44 Sensor
2016-02-16
Code to read the DHT temperature and humidity sensors. The sensor may be auto detected. A DHT11 sensor may be read once per second. The other sensors should not be read more often than once every three seconds.

The code auto detects the DHT model and generally only the GPIO needs to be specified.

./DHTXXD -g17 # Read a DHT connected to GPIO 17.

./DHTXXD -g5 -i3 # Read a DHT connected to GPIO 5 every three seconds.

./DHTXXD -? # for options.

Rotary Encoder
2015-11-18
Code to monitor a rotary encoder and show the position changes. By default the detent changes are shown. There is an option to show the four steps per detent instead.

./RED -a7 -b8 -s30 # Show encoder on 7/8 detent changes for 30 seconds.

./RED -a5 -b6 -m1 # Show encoder on 5/6 step changes forever.

./RED -? # for options.

Sonar Ranger
2015-11-16
Code to read the SRF-04 and SRF-05 type of sonar rangers which use the trigger echo method of operation. A 10 μs trigger pulse initiates a series of high frequency sonar chirps. The echo line then goes high and stays high until an echo from an object is received. The echo high time is used to calculate the distance of the object.

For a one-off reading only the trigger and echo GPIO need to be specified.

./SRTED -t5 -e6 # Read a sonar ranger connected to GPIO 5/6.

./SRTED -t11 -e5 -i0.1 # Read a sonar ranger connected to GPIO 11/5 every 0.1 seconds.

./SRTED -? # for options.

Transmit rotary encoder test signals
2015-11-25
Code to transmit quadrature signals to test rotary encoder software.

tx_RED -aGPIO -bGPIO [options]

tx_RED -? for options

E.g.

tx_RED -a5 -b6 -s20 -r-100

Transmit Wiegand test signals
2015-11-25
Code to transmit Wiegand codes to test Wiegand decoder software.

tx_WD -gGPIO -wGPIO [options] {code}+

tx_WD -? for options

E.g.

tx_WD -g5 -w6 -s37 12345 67890 123 899999

Wiegand Reader
2015-11-25
Code to read a Wiegand Reader.

./WD -g7 -w8 -s30 # Read Wiegand codes from GPIO 7/8 for 30 seconds.

./WD -g5 -w6 # Read Wiegand codes from GPIO 5/6 forever.

./WD -? # for options.

Python code

The Python code may be run on any Python machine and allows control of the GPIO on one or more networked Pis.

The Python machine need not be a Pi, it may run Windows, Mac, Linux, anything as long as it supports Python.

Each Pi needs the pigpio daemon to be running. The pigpio daemon may be started with the command sudo pigpiod.

433MHz keyfob RX/TX
2015-10-30
Classes to send and receive 433MHz wireless keyfob codes. These keyfobs are widely used for remote control of devices.

7-Segment LCD display multiplexing
2015-08-07
Script to multiplex several 7-segment LCD displays. Each display has the segments a-g and the decimal point connected in parallel but has an individual enable GPIO (connected to the common anode or cathode).

BME280 sensor
2016-08-05
Class to read the relative humidity, temperature, and pressure from a BME280 sensor. The sensor has both an I2C and a SPI interface which are both
supported by the class.

DHT22 AM2302 Sensor
2014-07-11
Class to read the relative humidity and temperature from a DHT22/AM2302 sensor.

DS18B20 Temperature Sensor
2016-06-29
Script to read the temperature from any DS18B20 sensors connected to the 1-wire bus.

To enable the 1-wire bus add the following line to /boot/config.txt and reboot.

dtoverlay=w1-gpio

By default you should connect the DS18B20 data line to GPIO 4 (pin 7).

Connect 3V3 or 5V for power, ground to ground, 4k7 pull-up on data line to 3V3, and data line to GPIO 4.

This script uses the file features of pigpio to access the remote file system.

The following entry must be in /opt/pigpio/access.

/sys/bus/w1/devices/28*/w1_slave r

Dust Sensor
2015-11-22
Class to read a Shinyei PPD42NS Dust Sensor, e.g. as used in the Grove dust sensor.

GPIO Status
2014-06-12
Script to display the status of GPIO 0-31.

Hall effect sensor
2014-06-13
Program to show status changes for a Hall effect sensor.

HX711 24-bit ADC
2016-05-01
Class to read the channels of a HX711 24-bit ADC.

I2C ADXL345 Accelerometer
2015-04-01
Script to display the X, Y, and Z values read from an ADXL345 accelerometer.

I2C HMC5883L Magnetometer
2015-04-01
Script to display the X, Y, and Z values read from a HMC5883L Magnetometer (compass).

I2C ITG3205 Gyroscope
2015-04-01
Script to display the X, Y, Z, and temperature values read from an ITG3205 gyroscope.

I2C LCD Display
2016-04-20
Class to display text on a LCD character display. The class supports the PCF8574T 8-bit I2C port expander connected to a HD44780 based LCD display. These displays are commonly available in 16x2 and 20x4 character formats.

I2C sniffer
2015-06-15
A program to passively sniff I2C transactions (100kHz bus maximum) and display the results.

I2C Sonar
2016-03-24
A class to read up to 8 HC-SR04 sonar rangers connected to an MCP23017 port expander.

IR Receiver
2014-06-12
Class to hash a code from an IR receiver (reading an IR remote control).

IR Record and Playback
2015-12-21
This script may be used to record and play back arbitrary IR codes.

To record the GPIO connected to the IR receiver, a file for the recorded codes, and the codes to be recorded are given.

E.g. ./irrp.py -r -g4 -fir-codes vol+ vol- 1 2 3 4 5 6 7 8 9 0

To playback the GPIO connected to the IR transmitter, the file containing the recorded codes, and the codes to be played back are given.

E.g. ./irrp.py -p -g18 -fir-codes 2 3 4

./irrp.py -h # for options

MAX6675 SPI Temperature sensor
2016-05-02
A script to read the temperature from a MAX6675 connected to a K-type thermocouple. The MAX6675 supports readings in the range 0 - 1023.75 C. Up to 4 readings may be made per second.

Morse Code
2015-06-17
Script to transmit the morse code corresponding to a text string.

PCA9685 16 channel PWM
2016-01-31
Class to control the 16 PWM channels of the I2C PCA9685. All channels use the same frequency. The duty cycle or pulse width may be set independently for each channel.

PCF8591 YL-40
2014-08-26
Script to display readings from the (I2C) PCF8591.

pigpio Benchmark
2014-06-12
Script to benchmark the pigpio Python module's performance.

pigpio CGI
2015-05-04
Script demonstrating how to access the pigpio daemon using CGI from a browser. Instructions on how to use with Apache2 on the Pi are given in the comments.

Pot + Capacitor Recharge timing
2016-02-06
Class to time capacitor charging (through a resistance). The time can be used to estimate the resistance.

PWM monitor
2015-12-08
Class to monitor a PWM signal and calculate the frequency, pulse width, and duty cycle.

Rotary Encoder
2014-06-12
Class to decode a mechanical rotary encoder.

RPM monitor
2016-01-20
Class to monitor speedometer pulses and calculate the RPM (Revolutions Per Minute).

Si7021 I2C Temperature and Humidity sensor
2016-05-07
Class to read the temperature and relative humidity from a Si7021.

SPI monitor
2016-09-21
A program to passively sniff SPI transactions and display the results. The SPI rate should be limited to about 70kbps if using the default pigpio 5┬Ás sampling rate.

Sonar Ranger
2014-06-12
Class to read sonar rangers with separate trigger and echo pins.

TCS3200 Colour Sensor
2015-07-03
Class to read the TCS3200 colour sensor

Virtual Wire (313MHz and 434MHz radio interface)
2015-10-31
Class to send and receive radio messages compatible with the Virtual Wire library for Arduinos.

Wave PWM
2016-03-19
Script to show how waves may be used to generate PWM at (one) arbitrary frequency on multiple GPIO. For instance PWM at 10kHz may be generated with 100 steps between off and fully on.

Wiegand Reader
2014-06-12
Class to read a Wiegand reader.

Miscellaneous related code


ADXL345
2014-03-12
This C program reads x, y, and z accelerations from the ADXL345 via I2C address 0x53.

DS18B20 temperature sensor
2016-04-25
This Python script reads the temperature from any DS18B20 sensors connected to the 1-wire bus.

To enable the 1-wire bus add the following line to /boot/config.txt and reboot.

dtoverlay=w1-gpio

By default you should connect the DS18B20 data line to GPIO 4 (pin 7).

Connect 3V3 or 5V for power, ground to ground, 4k7 pull-up on data line to 3V3, and data line to GPIO 4.

Easy as Pi Server
2014-09-15
This Python class implements a simple server which allows broswer commands to be executed on the Pi.

Minimal clock access
2015-05-20
This C code sets GPIO 4 to a specified clock frequency. The frequency can be set between 4.6875 kHz and 500 MHz (untested). The clock can be preferentially set from one of the sources OSC (19.2MHz), HDMI (216MHz), PLLD (500MHz), or PLLC (1000MHz). MASH can be set between 0 and 3. MASH may not work properly for clock dividers less than 5.

Minimal GPIO access
2016-04-30
This C code has a minimal set of functions needed to control the GPIO and other Broadcom peripherals. The program requires root privileges to run. See Tiny GPIO access for an alternative which controls the GPIO (but not the other peripherals) and does not require root access.

The following functions are provided.

gpioInitialise
gpioSetMode
gpioGetMode
gpioSetPullUpDown
gpioRead
gpioWrite
gpioTrigger
gpioReadBank1
gpioReadBank2
gpioClearBank1
gpioClearBank2
gpioSetBank1
gpioSetBank2
gpioHardwareRevision
gpioTick

Nano second pulse generation
2014-01-29
This C program uses the PWM peripheral to generate precisely timed pulses of very short duration. Pulses as short as 4 nano seconds can be generated.

PCF8591 YL-40
2014-08-26
C and Python code to read the (I2C) PCF8591.

Tiny GPIO access
2016-04-30
This C code has a minimal set of functions needed to control the GPIO without needing root privileges (it uses /dev/gpiomem to access the GPIO).

You may need to change the permissions and ownership of /dev/gpiomem if they have not been correctly set up.

sudo chown root:gpio /dev/gpiomem
sudo chmod g+rw /dev/gpiomem

The user (default pi) needs to be in the gpio group.

sudo adduser pi gpio

The following functions are provided.

gpioInitialise
gpioSetMode
gpioGetMode
gpioSetPullUpDown
gpioRead
gpioWrite
gpioTrigger
gpioReadBank1
gpioReadBank2
gpioClearBank1
gpioClearBank2
gpioSetBank1
gpioSetBank2
gpioHardwareRevision

External links


stepper motor
2016-08-12
Stepper motor code.

Index


433MHz keyfob RX/TX pdif2 Python
7-Segment LCD display multiplexing Python
ADXL345 Misc
BME280 sensor Python
DHT11/21/22/33/44 Sensor pdif2
DHT22 AM2302 Sensor Python
DS18B20 Temperature Sensor Python Misc
Dust Sensor Python
Easy as Pi Server Misc
Frequency Counter 1 C
Frequency Counter 2 C
GPIO Status Python
GPIO test Shell
Hall effect sensor C Python
HX711 24-bit ADC Python
I2C ADXL345 Accelerometer Python
I2C HMC5883L Magnetometer Python
I2C ITG3205 Gyroscope Python
I2C LCD Display Python
I2C sniffer Python C
I2C Sonar Python
IR Receiver C Hardware C++ Python
IR Record and Playback Python
Light Dependent Resistor Hardware
MAX6675 SPI Temperature sensor Python
Minimal clock access Misc
Minimal GPIO access Misc
Morse Code Python
Motor Shield Hardware
Nano second pulse generation Misc
PCA9685 16 channel PWM Python
PCF8591 YL-40 C Python Misc
pigpio Benchmark Python
pigpio CGI Python
Pot + Capacitor Recharge timing Python C
PWM monitor Python
Rotary Encoder Hardware C++ C pdif2 Python
RPM monitor Python
Si7021 I2C Temperature and Humidity sensor Python
Sonar Ranger Hardware pdif2 Python
SPI bit bang read MCP3008 (10-bit ADC) C
SPI bit bang read MCP3008 and MCP3202 C
SPI bit bang read MCP3202 (12-bit ADC) C
SPI monitor Python
stepper motor External
TCS3200 Colour Sensor Python
Tiny GPIO access Misc
Transmit rotary encoder test signals pdif2
Transmit Wiegand test signals pdif2
Virtual Wire (313MHz and 434MHz radio interface) Python
Wave PWM Python
Wiegand Reader pdif2 C Python C++
|pigpio| |pigpio C I/F| |pigpiod| |pigpiod C I/F| |Python| |pigs| |piscope| |Misc| |Examples| |Download| |Site Map|
© 2012-2016
e-mail: pigpio @ abyz.co.uk
Updated: 22/09/2016