How to use GPIO in C language

Hello all,

I develop a C program and want to control something using nano GPIO, but here (Jetson.GPIO · PyPI) is the python library.
So how should i use the GPIO in C language on nano board? Do you have C library for GPIO?
Thanks a lot! Look forward to your reply.


Hi, for accessing GPIO from userspace, you can use the sysfs file nodes under /sys/class/gpio/

Reading and writing to these files is how languages like C/C++, Python, and other libraries implement it under the covers. You can see the documentation for it here: [url][/url]

Also see slide #35 from this presentation for a high level overview.

And here is a code example in C of using these files for GPIO: [url][/url]

1 Like

I wrote a library for working with GPIO pins and I2C sensors for the Raspberry Pi 3.
The library is here . If you want, I can modify it for Jetson Nano. But I will need help, I need to test it on Jetson Nano.
I do not have my own board. If you want to contact me, write In the subject line of the letter, indicate on what issue.

1 Like

If you want, I can make a description of the library (github) in English.

1 Like

Hi dusty_nv,
I am trying to access the GPIO’s on the Jetson Nano with C++. The first link you have provided ( says that the ABI was deprecated and that userspace consumers are supposed to use the character device ABI. Any documentation on the character device thing they mention?

Also I tried to implement the same code from the example you provided ( [url][/url]) and I was not able to write to /sys/class/gpio/export . I received a “Permission Denied” error. Any advice on getting GPIO’s to work with C++? I am using nsight for my development by the way

Did you try running the program with sudo permissions? Unless your user has write access to /sys/, you would need to elevate permissions with sudo.

I got it working when I log into terminal as root. All I am doing is calling

system("echo 15 > /sys/class/gpio/export");
system("echo out > /sys/class/gpio/gpio15/direction");
system("echo 1 > /sys/class/gpio/gpio15/value");

from within my program.
How can I make sure that my program runs as root or with elevated permissions when I am ready to push the final production code to my device?

Since the files under /sys are dynamic and get reset after every reboot, the modified permissions would need to be set for your user in a boot service. The easier way would just be to run your application with sudo.

Once I finish with my code, I intend to be able to power up the device and have the program running with no user input. My program needs to be able to write to GPIO pins and so I need a way to have all these permissions set at boot. Running as sudo is fine for now but it is definitely not a long term solution for me. Any chance you could point me in the right direction to get a boot service set up?

Hi nadir, see this post for an example systemd boot service that sets write permissions to a set of files under /sys. You would want to modify the chmod command there to reflect the /sys/class/gpio/* path you are using instead of the path from that example.

I created the service but I notice that when booting up the device that service always fails. Once it is logged in I can manually start the service but my code still outputs an error when attempting to initialize the GPIOs. Any advice?

Is there any error output that may indicate why it fails, either the start-up service or with your application?

You might also want to try this sudo NOPASSWD method from another post in that same thread, which limits the sudo access to the files/directories that you specify. Perhaps that may be easier to get going with.

$ sudo groupadd -f -r gpio
$ sudo usermod -a -G gpio your_user_name
$ sudo cp /opt/nvidia/jetson-gpio/etc/99-gpio.rules /etc/udev/rules.d/
$ sudo udevadm control --reload-rules
$ reboot

1 Like

I am writing a deep-stream script in C++ for object detection bounding box location and moving motors using GPIO pins. Does writing in C++ increase efficiency and speed of the script tremendously ? Since most computing is done in deep-stream i am unsure.

Ps: My goal is to make it fast, but is difference in speed between C++ and python significant as development and bug testing in C++ is a pain.