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 1: Blink
This first example sets up io1212’s inputs and outputs appropriately, and then turns the first output on and off repeatedly. If an LED is connected on the first output, you can expect to see it blink.
1#include <unistd.h>
2
3#include <pigpio.h>
4
5#define Output_1 4
6#define Output_2 5
7#define Output_3 6
8#define Output_4 7
9#define Output_5 8
10#define Output_6 9
11#define Output_7 10
12#define Output_8 11
13#define Output_9 12
14#define Output_10 13
15#define Output_11 14
16#define Output_12 15
17
18#define Input_1 16
19#define Input_2 17
20#define Input_3 18
21#define Input_4 19
22#define Input_5 20
23#define Input_6 21
24#define Input_7 22
25#define Input_8 23
26#define Input_9 24
27#define Input_10 25
28#define Input_11 26
29#define Input_12 27
30
31
32int main(int argc, char *argv[])
33{
34 if (gpioInitialise()<0) return 1;
35
36 gpioSetMode(Output_1, PI_OUTPUT);
37 gpioSetMode(Output_2, PI_OUTPUT);
38 gpioSetMode(Output_3, PI_OUTPUT);
39 gpioSetMode(Output_4, PI_OUTPUT);
40 gpioSetMode(Output_5, PI_OUTPUT);
41 gpioSetMode(Output_6, PI_OUTPUT);
42 gpioSetMode(Output_7, PI_OUTPUT);
43 gpioSetMode(Output_8, PI_OUTPUT);
44 gpioSetMode(Output_9, PI_OUTPUT);
45 gpioSetMode(Output_10, PI_OUTPUT);
46 gpioSetMode(Output_11, PI_OUTPUT);
47 gpioSetMode(Output_12, PI_OUTPUT);
48
49 gpioSetMode(Input_1, PI_INPUT);
50 gpioSetMode(Input_2, PI_INPUT);
51 gpioSetMode(Input_3, PI_INPUT);
52 gpioSetMode(Input_4, PI_INPUT);
53 gpioSetMode(Input_5, PI_INPUT);
54 gpioSetMode(Input_6, PI_INPUT);
55 gpioSetMode(Input_7, PI_INPUT);
56 gpioSetMode(Input_8, PI_INPUT);
57 gpioSetMode(Input_9, PI_INPUT);
58 gpioSetMode(Input_10, PI_INPUT);
59 gpioSetMode(Input_11, PI_INPUT);
60 gpioSetMode(Input_12, PI_INPUT);
61
62 gpioSetPullUpDown(Input_1, PI_PUD_OFF);
63 gpioSetPullUpDown(Input_2, PI_PUD_OFF);
64 gpioSetPullUpDown(Input_3, PI_PUD_OFF);
65 gpioSetPullUpDown(Input_4, PI_PUD_OFF);
66 gpioSetPullUpDown(Input_5, PI_PUD_OFF);
67 gpioSetPullUpDown(Input_6, PI_PUD_OFF);
68 gpioSetPullUpDown(Input_7, PI_PUD_OFF);
69 gpioSetPullUpDown(Input_8, PI_PUD_OFF);
70 gpioSetPullUpDown(Input_9, PI_PUD_OFF);
71 gpioSetPullUpDown(Input_10, PI_PUD_OFF);
72 gpioSetPullUpDown(Input_11, PI_PUD_OFF);
73 gpioSetPullUpDown(Input_12, PI_PUD_OFF);
74
75 int count = 30;
76
77 while(count) {
78 gpioWrite(Output_1, 1);
79 sleep(1);
80 gpioWrite(Output_1, 0);
81 sleep(1);
82 count--;
83 }
84
85 gpioTerminate();
86}
After saving the previous code into a file named io1212-1.c
, you can compile and link it using:
gcc -Wall -pthread -o io1212-1 io1212-1.c -lpigpio -lrt
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}