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
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. |
GPIO test 2014-08-11 | This bash script tests the user GPIO. Video |
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 MCP3008 2016-03-20 | This program shows how to read multiple MCP3008 ADC simultaneously with accurately timed intervals. One 10-bit channel of each ADC may be sampled at up to 25k samples per second. |
SPI bit bang MCP3202 2016-03-20 | This program shows how to read multiple MCP3202 ADC simultaneously with accurately timed intervals. One 12-bit channel of each ADC may be sampled at up to 25k samples per second. |
SPI bit bang 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. |
Servo Pulse Generator 2016-10-08 | This program generates servo pulses on one or more GPIO. Each connected servo is swept between 1000µs and 2000µs at a different speed. sudo ./servo_demo # Generate pulses on GPIO 4. sudo ./servo_demo 5 9 20 # Generate pulses on GPIO 5, 9, and 20. |
SPI pigpio driver speed test 2016-11-06 | This C code is used to benchmark the pigpio SPI driver on the Pi. The code executes a given number of loops at a given baud rate and bytes per transfer. |
Wiegand Reader 2013-12-30 | Function to read a Wiegand Reader. |
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. |
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. |
Servo Pulse Generator 2016-10-08 | This program generates servo pulses on one or more GPIO. Each connected servo is swept between 1000µs and 2000µs at a different speed. ./servo_demo_D # Generate pulses on GPIO 4. ./servo_demo_D 5 9 20 # Generate pulses on GPIO 5, 9, and 20. |
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. |
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 LED Display Multiplexing 2016-12-12 | Script to multiplex several 7-segment LED 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). |
APA102 LED strip driver 2017-03-28 | Script to drive an APA102 LED strip. Three different methods are demonstrated - using spidev SPI (only works on the local Pi), pigpio SPI, and pigpio waves. The SPI solutions only work with the dedicated SPI GPIO. Waves may use any spare GPIO. Four different examples are given including a LED strip clock. |
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. |
DHT11/21/22/33/44 Sensor 2019-11-07 | Class to read the relative humidity and temperature from a DHT sensor. It can automatically recognize the sensor type. The default script prints the reading from the specified DHT every 2 seconds. E.g. ./DHT.py 22 27 displays the data for DHT connected to GPIO 22 and 27. The following data is printed for each DHT: timestamp, GPIO, status, temperature, and humidity. The timestamp is the number of seconds since the epoch (start of 1970). The status will be one of: 0 - a good reading, 1 - checksum failure, 2 - data had one or more invalid values, 3 - no response from sensor. |
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 2018-03-05 | 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 slave device 2016-10-31 | This script demonstrates how to transfer messages from an Arduino acting as the I2C bus master to the Pi acting as an I2C slave device. |
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 |
Kivy GPIO control 2016-12-11 | This example shows how to use Kivy to control a Pi's GPIO. The GPIO may be configured as inputs, outputs, or to generate Servo or PWM pulses. Kivy is an Open source Python library for rapid development of applications. |
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. |
Monitor GPIO 2016-09-17 | Script to monitor GPIO for level changes. By default all GPIO are monitored. At a level change the GPIO, new level, and microseconds since the last change is printed. |
Morse Code 2015-06-17 | Script to transmit the morse code corresponding to a text string. |
NRF24 radio transceiver 2018-01-06 | Script to transmit and receive messages using the nRF24L01 radio transceiver. |
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. |
PPM (Pulse Position Modulation) generation 2016-02-19 | Script to generate PPM signals on a chosen GPIO. |
PPM (Pulse Position Modulation) to servo pulses 2019-10-09 | Script to read a PPM signal on a GPIO and generate the corresponding servo signals on chosen GPIO. |
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. |
Playback piscope recordings 2016-12-23 | Script to playback GPIO data recorded in piscope format. To playback GPIO 4 to GPIO 4 from file data.piscope ./playback.py data.piscope 4 To playback GPIO 4 to GPIO 7 from file rec.txt ./playback.py rec.txt 7=4 |
Pot + Capacitor Recharge Timing 2016-09-26 | 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. |
Servo Pulse Generator 2016-10-07 | This script generates servo pulses on one or more GPIO. Each connected servo is swept between 1000µs and 2000µs at a different speed. ./servo_demo.py # Generate pulses on GPIO 4. ./servo_demo.py 5 9 20 # Generate pulses on GPIO 5, 9, and 20. |
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 2015-10-31 | Class to send and receive radio messages compatible with the Virtual Wire library for Arduinos. This library is commonly used with 313MHz and 434MHz radio tranceivers. |
Wave create 2019-11-18 | Script to generate waves from a template defined in a text file. You can also specify one of py, c, or pdif - the script output will then be a complete program to generate the wave (py for Python script, c for a C program, pdif for a C program using the pigpio daemon I/F). If none of py, c, or pdif are chosen the waveform will be generated for 30 seconds. Example text file # GPIO levels 23 11000001 11 01110000 12 00011100 4 00000111 To generate a pdif program with a bit time of 100 microseconds ./create_wave.py wave_file 100 pdif >wave_pdif.c To just transmit the wave with a bit time of 50 microseconds ./create_wave.py wave_file 50 |
Wave PWM 1 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. |
Wave PWM 2 2016-10-06 | Class to generate PWM on multiple GPIO. It is more flexible than the Wave PWM 1 example in that the start of the pulse within each cycle may be specified as well as the duty cycle. The start and length of each pulse may be specified on a GPIO by GPIO basis in microseconds or as a fraction of the cycle time. The class includes a __main__ to demostrate its ability to send servo pulses. |
Wiegand Reader 2014-06-12 | Class to read a Wiegand reader. |
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 2019-07-03 | 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 code has been updated for the BCM2711 (Pi4B). The following functions are provided. gpioInitialise gpioSetMode gpioGetMode gpioSetPullUpDown gpioRead gpioWrite gpioTrigger gpioReadBank1 gpioReadBank2 gpioClearBank1 gpioClearBank2 gpioSetBank1 gpioSetBank2 gpioHardwareRevision gpioTick |
Nanosecond 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. |
SPI Linux driver speed test 2016-11-06 | This C code is used to benchmark the Linux SPI driver on the Pi. The code executes a given number of loops at a given baud rate and bytes per transfer. |
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 |
Stepper Motor 2016-08-12 | Stepper motor code. |
Parallax ActivityBot 360 2018-11-03 | Python 3 implementation for programming a Parallax ActivityBot 360 Robot Kit with a Raspberry Pi. |
433MHz Keyfob RX/TX | pdif2 Python |
7-Segment LED Display Multiplexing | Python |
ADXL345 | Misc |
APA102 LED strip driver | Python |
BME280 Sensor | Python |
DHT11/21/22/33/44 Sensor | pdif2 Python |
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 slave device | Python |
I2C Sniffer | C Python |
I2C Sonar | Python |
IR Receiver | Hardware C C++ Python |
IR Record and Playback | Python |
Kivy GPIO control | Python |
Light Dependent Resistor | Hardware |
MAX6675 SPI Temperature Sensor | Python |
Minimal Clock Access | Misc |
Minimal GPIO Access | Misc |
Monitor GPIO | Python |
Morse Code | Python |
Motor Shield | Hardware |
Nanosecond Pulse Generation | Misc |
NRF24 radio transceiver | Python |
Parallax ActivityBot 360 | External |
PCA9685 16 Channel PWM | Python |
PCF8591 YL-40 | C Python Misc |
pigpio Benchmark | Python |
pigpio CGI | Python |
Playback piscope recordings | Python |
Pot + Capacitor Recharge Timing | C Python |
PPM (Pulse Position Modulation) generation | Python |
PPM (Pulse Position Modulation) to servo pulses | Python |
PWM Monitor | Python |
Rotary Encoder | Hardware C C++ pdif2 Python |
RPM Monitor | Python |
Servo Pulse Generator | C pdif2 Python |
Si7021 I2C Temperature and Humidity Sensor | Python |
Sonar Ranger | Hardware pdif2 Python |
SPI bit bang MCP3008 | C |
SPI bit bang MCP3008 and MCP3202 | C |
SPI bit bang MCP3202 | C |
SPI Linux driver speed test | Misc |
SPI Monitor | Python |
SPI pigpio driver speed test | C |
Stepper Motor | External |
TCS3200 Colour Sensor | Python |
Tiny GPIO Access | Misc |
Transmit Rotary Encoder Test Signals | pdif2 |
Transmit Wiegand Test Signals | pdif2 |
Virtual Wire | Python |
Wave create | Python |
Wave PWM 1 | Python |
Wave PWM 2 | Python |
Wiegand Reader | C C++ pdif2 Python |