Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to utilize the GPIO pins on my Raspberry Pi 2, but I'd rather not use Python since I personally dislike the language. However, I cannot seem to find any information regarding the use of C, only Python.

Is there an official C API? If so, where can I find tutorials on it? If not, what alternatives do I have?

share|improve this question
    
This is not C specific but for general GPIO programming language support see the list at elinux.org/RPi_GPIO_Code_Samples – joan Jan 15 at 11:06

There is no official library

There are several options. I usually use http://wiringpi.com. This is used for the gpio utility included in recent Raspbian releases.

Joan's pigpio library is at https://github.com/joan2937/pigpio and she also has Python wrappers. If you want to use sockets or a daemon this is recommended and has good support for hardware and bulk access to banks of pins.

Both have extensive documentation.

There are a couple of others, but some seem a bit dated.

share|improve this answer

You don't mention an operating system, like Milliways I will presume you mean Raspbian or some other GNU/Linux. Windows IoT would be seperate.

All of the python libraries are wrappers on C libraries, which is not unusual. Creating the libraries from scratch in something other than C would be. There are three:

These have been around at least a few years and are actively maintained (e.g., the last version of libbcm2835 was 3-4 months ago).

However, you do not need any library depending on what you want to do. As mentioned there, in addition to the sysfs interface for basic things, there are portable userland C level interfaces for I2C and SPI that use kernel drivers. This just leaves PWM -- to access and use the hardware channels you need to use one of those three libraries. Or, as joan mentions in a comment on that other question, if you are trying to count very fast pulses (I would experiment with a few methods in that case).

I have not tried every combination, but presuming the pi specific libraries are coded sanely, you should be able to mix and match them with the portable interfaces (e.g, use the regular I2C API and wiringPi for PWM) as long as you use them for separate pins. This may seem an odd approach, but it makes sense if you are working with modular code and want to make individual parts as portable as possible. For example, if you want to write an interface for an I2C sensor, base it around the kernel driver API. Then if you want to use that in a program with PWM, use one of the three libraries for PWM. Code written with pigpio/wiringPi/libbcm2835 will only work on a Raspberry Pi; code written using the kernel driver APIs will work on any linux system.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.