jose4158
Posts: 1
Joined: Fri Feb 13, 2015 3:35 pm

How to control the GPIO pins using c++

Fri Feb 13, 2015 4:05 pm

Sorry if this is a somewhat stupid question.

I have become a real expert with Python and the GPIO pins on my Raspberry Pi B+. But recently I decided that I should start coding in C++. I do know a fair amount of C++ but I do not know how to control the GPIO pins. I have looked at wiringPi, but I did not understand how to install and use it on the Raspberry Pi.

Could someone please explain to me (in the simplest way possible) how to control the GPIO pins using C++?? I don't care if it is not Wiring Pi, plus some simple example code (to light up a led?) would be greatly appreciated.

Thanks in advance to anyone who willingly can show me a way to control the gpio pins using C++. :D :D :D

User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: How to control the GPIO pins using c++

Fri Feb 13, 2015 10:36 pm

There is a website for Wiring Pi. There are detailed instructions on installing the library here on the Wiring Pi website. The classic blink example is here.

User avatar
joan
Posts: 14196
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: How to control the GPIO pins using c++

Fri Feb 13, 2015 10:45 pm

For a lightweight standalone solution have a look at my minimal gpio access at http://abyz.co.uk/rpi/pigpio/examples.html#Misc_code

perimeno
Posts: 2
Joined: Tue Sep 25, 2012 5:21 pm

Re: How to control the GPIO pins using c++

Sun Jun 02, 2019 8:54 pm

Take a look:
https://github.com/perimeno/LIO

Its a high level C++ wrapper for Linux sysfs, so no nasty direct memory writing and other hacky stuff that could mess up your OS.
Hope you can use it.

dl324
Posts: 122
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: How to control the GPIO pins using c++

Sun Jun 02, 2019 11:10 pm

jose4158 wrote:
Fri Feb 13, 2015 4:05 pm
Could someone please explain to me (in the simplest way possible) how to control the GPIO pins using C++?? I don't care if it is not Wiring Pi, plus some simple example code (to light up a led?) would be greatly appreciated.
If you don't need MHz speed, using the sysfs interface is the easiest.

For more speed, you can use the register interface. I only have Pi Zero and Zero W, so can't give you specifics for B+.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7147
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: How to control the GPIO pins using c++

Mon Jun 03, 2019 7:22 am

The Linux sysfs interface is officially deprecated, replaced by gpiolib. I can't see the official docs quickly, but the examples are at https://github.com/raspberrypi/linux/tr ... tools/gpio
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

hippy
Posts: 5790
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to control the GPIO pins using c++

Mon Jun 03, 2019 3:00 pm

perimeno wrote:
Sun Jun 02, 2019 8:54 pm
Take a look:
https://github.com/perimeno/LIO

Its a high level C++ wrapper for Linux sysfs, so no nasty direct memory writing and other hacky stuff that could mess up your OS.
Using sysfs for GPIO has long been deprecated in Linux and, I believe, is scheduled for removal in 2020 so probably not the best direction to send people in. Though the wrapper should stay the same, and be just as usable, once sysfs access has disappeared, assuming the author updates that wrapper.

The new recommended interface is 'gpio-cdev' which uses "/dev/gpiochip*", with 'libgpiod' to provide userland utilities. The latest Raspbian seems to support those but there's not been any discussion on here which I have seen and 'libgpiod' and userland utilities don't seem included by defult or available as an installable package yet -

https://www.kernel.org/doc/Documentatio ... /gpio-cdev

https://www.beyondlogic.org/an-introduc ... spberry-pi

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7147
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: How to control the GPIO pins using c++

Mon Jun 03, 2019 3:38 pm

hippy wrote:
Mon Jun 03, 2019 3:00 pm
The new recommended interface is 'gpio-cdev' which uses "/dev/gpiochip*", with 'libgpiod' to provide userland utilities. The latest Raspbian seems to support those but there's not been any discussion on here which I have seen and 'libgpiod' and userland utilities don't seem included by defult or available as an installable package yet -
libgpiod is available in Buster, but not Stretch - https://packages.debian.org/source/buster/libgpiod
It is certainly the recommended route forward instead of sysfs.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

hippy
Posts: 5790
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to control the GPIO pins using c++

Mon Jun 03, 2019 7:22 pm

6by9 wrote:
Mon Jun 03, 2019 3:38 pm
libgpiod is available in Buster, but not Stretch - https://packages.debian.org/source/buster/libgpiod
It is certainly the recommended route forward instead of sysfs.
Do you happen to know what the future is intended to be for /dev/gpiomem and direct register access to GPIO when Buster arrives ?

I am guessing both will continue to be supported on the Pi because there are so many people currently using those to access GPIO.

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: How to control the GPIO pins using c++

Tue Jun 04, 2019 7:45 pm

6by9 wrote:
Mon Jun 03, 2019 3:38 pm
hippy wrote:
Mon Jun 03, 2019 3:00 pm
The new recommended interface is 'gpio-cdev' which uses "/dev/gpiochip*", with 'libgpiod' to provide userland utilities. The latest Raspbian seems to support those but there's not been any discussion on here which I have seen and 'libgpiod' and userland utilities don't seem included by defult or available as an installable package yet -
libgpiod is available in Buster, but not Stretch - https://packages.debian.org/source/buster/libgpiod
It is certainly the recommended route forward instead of sysfs.
libgpiod seems to be pretty available to me on my Pi with Raspbian Stretch:

Code: Select all

apt search libgpio
Sorting... Done
Full Text Search... Done
gpiod/stable,now 1.0.1-1~bpo9+1 armhf [installed]
  Tools for interacting with Linux GPIO character device - binary

libgpiod-dev/stable,now 1.0.1-1~bpo9+1 armhf [installed]
  C library for interacting with Linux GPIO device - static libraries and headers

libgpiod-doc/stable,now 1.0.1-1~bpo9+1 all [installed]
  C library for interacting with Linux GPIO device - library documentation

libgpiod1/stable,now 1.0.1-1~bpo9+1 armhf [installed,automatic]
  C library for interacting with Linux GPIO device - shared libraries

libgpiod1-dbgsym/stable 1.0.1-1~bpo9+1 armhf
  Debug symbols for libgpiod1


hippy
Posts: 5790
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to control the GPIO pins using c++

Tue Jun 04, 2019 8:24 pm

buja wrote:
Tue Jun 04, 2019 7:45 pm
libgpiod seems to be pretty available to me on my Pi with Raspbian Stretch:
I see the same here, and /dev/gpiochip* exist.

But there's something missing. The userland utilities don't seem to be there, eg "gpiodetect", and when I tried building those gcc gives file not found for "#include <gpiod.h>".

User avatar
buja
Posts: 498
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: How to control the GPIO pins using c++

Wed Jun 05, 2019 5:43 pm

This is what I have:

Code: Select all

 $ ls -l /usr/bin/gpio*
-rwsr-xr-x 1 root root 33348 Mar 13 14:41 /usr/bin/gpio
-rwxr-xr-x 1 root root  9728 Jun 29  2018 /usr/bin/gpiodetect
-rwxr-xr-x 1 root root  9728 Jun 29  2018 /usr/bin/gpiofind
-rwxr-xr-x 1 root root  9728 Jun 29  2018 /usr/bin/gpioget
-rwxr-xr-x 1 root root  9728 Jun 29  2018 /usr/bin/gpioinfo
-rwxr-xr-x 1 root root  9728 Jun 29  2018 /usr/bin/gpiomon
-rwxr-xr-x 1 root root 13824 Jun 29  2018 /usr/bin/gpioset
These tools are in the package gpiod, except gpio, which is part of wiringPi.
Header file gpiod.h is in /usr/include.

I don't remember doing anything special to make it work.

Example program, just a matter of gcc -o test test.c -lgpiod

Code: Select all

/* test.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gpiod.h>

int main(void)
{
    printf("gpiod version: %d\n", gpiod_version_string());
}

hippy
Posts: 5790
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How to control the GPIO pins using c++

Wed Jun 05, 2019 7:23 pm

Very odd, Pi 3B (non-plus), Raspbian Stretch Full with Desktop ...

Code: Select all

[email protected]:~/tmp $ uname -a
Linux Pi3B 4.19.42-v7+ #1219 SMP Tue May 14 21:20:58 BST 2019 armv7l GNU/Linux
[email protected]:~/tmp $ ls -l /usr/bin/gpio*
-rwsr-xr-x 1 root root 33348 Mar 13 13:41 /usr/bin/gpio
None of the others.

Code: Select all

[email protected]:~/tmp $ cat test.c
/* test.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gpiod.h>

int main(void)
{
    printf("gpiod version: %d\n", gpiod_version_string());
}
[email protected]:~/tmp $ gcc -o test test.c -lgpiod
test.c:6:19: fatal error: gpiod.h: No such file or directory
 #include <gpiod.h>
                   ^
compilation terminated.
Running "sudo apt-get install gpiod" does seem to have partially fixed things, bringing in "gpiod" and "libgpiod1" -- I had been trying to install "libgpiod" which doesn't exist !

That creates the previously missing /usr/bin/gpio* files, and this at least works and seems correct (3B) ...

Code: Select all

[email protected]:~/tmp $ gpiodetect
gpiochip0 [pinctrl-bcm2835] (54 lines)
gpiochip1 [raspberrypi-exp-gpio] (8 lines)
gpiochip2 [brcmvirt-gpio] (2 lines)
But the compile still fails with gpiod.h not found. It's not in /usr/include.

"sudo apt-get install libgpiod-dev" fixed that ...

Code: Select all

[email protected]:~/tmp $ ./test
gpiod version: 1995806036
Thanks for the pointers which got me there.

Return to “C/C++”