Using pigpio

The pigpio library, which source code can be found in github, is another C library specifically built to control the GPIO pins of the Raspberry Pi.

Installation

As shown on pigpio’s installation instructions, after making sure that pigpiod is not running, you can download it, compile it, and install it by running the following instructions. Please note that at the time of writing, the latest release is v79:

ubuntu@rpi:~$ wget https://github.com/joan2937/pigpio/archive/refs/tags/v79.zip
ubuntu@rpi:~$ unzip v79.zip
ubuntu@rpi:~$ cd pigpio-79
ubuntu@rpi:~/pigpio-79$ make
ubuntu@rpi:~/pigpio-79$ sudo make install

You can test the installation by running, in the pigpio-79 directory, the following commands:

ubuntu@rpi:~/pigpio-79$ pigpiod -v
79

ubuntu@rpi:~/pigpio-79$ sudo ./x_pigpio
Testing pigpio C I/F
[...]
TEST  1.1  PASS (set mode, get mode: 0)
TEST  1.2  PASS (set pull up down, read: 1)
[...]
TEST  9.3  PASS (run/stop script/script status: 110)
TEST  9.4  PASS (delete script: 0)

Example 2: Using inputs

By substituting lines 75-83 with the following snippet, you can see an example of how to read an input using gpioRead().

   int count = 20;
   int state = gpioRead(Input_1);
   
   while(count) {
	while(state == gpioRead(Input_1));
	state = gpioRead(Input_1);
  	gpioWrite(Output_1, state);
   	sleep(1);
   	count--;
   }

   gpioWrite(Output_1, 0);

Example 3: Callback functions

The following example shows how pigpio allows the registration of an alert function that will be called when a GPIO pin changes state.

 1#include <unistd.h>
 2#include <stdio.h>
 3
 4#include <pigpio.h>
 5
 6#define Output_1 4
 7#define Output_2 5
 8#define Output_3 6
 9#define Output_4 7
10#define Output_5 8
11#define Output_6 9
12#define Output_7 10
13#define Output_8 11
14#define Output_9 12
15#define Output_10 13
16#define Output_11 14
17#define Output_12 15
18
19#define Input_1 16
20#define Input_2 17
21#define Input_3 18
22#define Input_4 19
23#define Input_5 20
24#define Input_6 21
25#define Input_7 22
26#define Input_8 23
27#define Input_9 24
28#define Input_10 25
29#define Input_11 26
30#define Input_12 27
31
32
33void alert(int gpio, int level, uint32_t tick)
34{
35   gpioWrite(Output_1, level);
36}
37
38int main(int argc, char *argv[])
39{
40   if (gpioInitialise()<0) return 1;
41
42   gpioSetMode(Output_1, PI_OUTPUT);
43   gpioSetMode(Output_2, PI_OUTPUT);
44   gpioSetMode(Output_3, PI_OUTPUT);
45   gpioSetMode(Output_4, PI_OUTPUT);
46   gpioSetMode(Output_5, PI_OUTPUT);
47   gpioSetMode(Output_6, PI_OUTPUT);
48   gpioSetMode(Output_7, PI_OUTPUT);
49   gpioSetMode(Output_8, PI_OUTPUT);
50   gpioSetMode(Output_9, PI_OUTPUT);
51   gpioSetMode(Output_10, PI_OUTPUT);
52   gpioSetMode(Output_11, PI_OUTPUT);
53   gpioSetMode(Output_12, PI_OUTPUT);
54
55   gpioSetMode(Input_1, PI_INPUT);
56   gpioSetMode(Input_2, PI_INPUT);
57   gpioSetMode(Input_3, PI_INPUT);
58   gpioSetMode(Input_4, PI_INPUT);
59   gpioSetMode(Input_5, PI_INPUT);
60   gpioSetMode(Input_6, PI_INPUT);
61   gpioSetMode(Input_7, PI_INPUT);
62   gpioSetMode(Input_8, PI_INPUT);
63   gpioSetMode(Input_9, PI_INPUT);
64   gpioSetMode(Input_10, PI_INPUT);
65   gpioSetMode(Input_11, PI_INPUT);
66   gpioSetMode(Input_12, PI_INPUT);
67
68   gpioSetPullUpDown(Input_1, PI_PUD_OFF);
69   gpioSetPullUpDown(Input_2, PI_PUD_OFF);
70   gpioSetPullUpDown(Input_3, PI_PUD_OFF);
71   gpioSetPullUpDown(Input_4, PI_PUD_OFF);
72   gpioSetPullUpDown(Input_5, PI_PUD_OFF);
73   gpioSetPullUpDown(Input_6, PI_PUD_OFF);
74   gpioSetPullUpDown(Input_7, PI_PUD_OFF);
75   gpioSetPullUpDown(Input_8, PI_PUD_OFF);
76   gpioSetPullUpDown(Input_9, PI_PUD_OFF);
77   gpioSetPullUpDown(Input_10, PI_PUD_OFF);
78   gpioSetPullUpDown(Input_11, PI_PUD_OFF);
79   gpioSetPullUpDown(Input_12, PI_PUD_OFF);
80
81   int secs=60;
82
83   gpioSetAlertFunc(Input_1, alert);
84
85   sleep(secs);
86
87   gpioWrite(Output_1, 0);
88   gpioTerminate();
89}