GPIO Set up with C++ for Motor Control

Hi,

I am new to the Jetson AGX Orin Dev Kit, so forgive me if I am a novice.

I am trying to control a couple of motors and solenoids with the GPIO output of the AGX Orin however I cannot seem to figure out what the pinout of the GPIO pins are? I am coding with C++ so I know that for example GPIO Pin32 is not actually named pin 32 in the code, but something else. How do I find this and control it with the code.
This is the code I am using right now, you can see I am trying to assign pin 456 which from this pinout link i thought was GPIO Pin32…

#include <fstream>
#include <string>
#include <unistd.h>

void export_gpio(int gpio_num) {
	std::ofstream export_file("/sys/class/gpio/export");
	export_file << gpio_num;
	usleep(100000);
}

void set_direction(int gpio_num, const std::string& dir) {
	std::ofstream dir_file("/sys/class/gpio/gpio" + std::to_string(gpio_num) + "/direction");
	dir_file << dir;
}

void write_gpio(int gpio_num, int value) {
	std::ofstream val_file("/sys/class/gpio/gpio" + std::to_string(gpio_num) + "/value");
	val_file << value;
}

int main() {
	int gpio_solenoid = 456;
	export_gpio(gpio_solenoid);
	set_direction(gpio_solenoid, "out");
	write_gpio(gpio_solenoid, 1);
	sleep(2);
	write_gpio(gpio_solenoid, 0);
	return 0;
}

I could imagine that this method is very slow and might not work for motor control.
There is an official GPIO library from Nvidia, but it’s only available for Python. You may try this library for example, which is an unofficial port for C++.

Hi Patrick,

Thanks for the reply. I installed the unofficial version of GPIO for C++ and went through setting up an example test to see if I could turn off and on (real pin 13) GPIO32, but there was no change of output. The multimeter I put across pin 13 and the GND on pin 9 just reads a constant 0V.

So I tried to use (real pin 16) GPIO08 and that worked when I used 16 as the pin number in the code.. Is PIN32 being used for something for something else and that is why I cannot control it?

hello gnehma2020,

please double check the pin with.. GitHub - NVIDIA/jetson-gpio: A Python library that enables the use of Jetson's GPIOs
you may see-also developer guide, Get GPIO Number by Name for reference.

Hi Jerry,

I was able to get this working, but I realised for another application I needed Ubuntu 22.04. So I flashed the AGX Orin DevKit with the SDK, following the instructions from the SDK Manager and when I tried to rerun the previously working code, I got this error:

sudo ./gpio_test
terminate called after throwing an instance of 'std::runtime_error'
  what():  [Exception] [Exception] could not open directory: /sys/bus/platform/devices/2200000.gpio/gpio (catched from: GPIO::get_data())
 (catched from: setmode())

Aborted

It seems like the GPIO pins aren’t ‘seen’ anymore right? Is there a way to fix this or can I reset back to the original factory flash with Ubuntu 20.04?

Maybe the path changed in jetpack 6.2 36.4.3 ?

/sys/bus/platform/devices/2200000.gpio/
/sys/bus/platform/devices/2200000.gpio/gpiochip0/

You could run this to see where to edit the paths in the source code. JetsonGPIO.git

sudo find /sys/ -iname “gpio


Or

sudo apt install gpiod

then these to get names and other gpio information.

gpiodetect gpiofind gpioget gpioinfo gpiomon gpioset

hello gnehma2020,

as you can see.. Using the Pins in GPIO Mode.
JP-6 is now using gpiod to control pins. you cannot use debugfs to toggle the pin anymore.

Hi Jerry,

So I attempted to follow the tutorial in the link you sent. I ran

gpioset gpiofind "PDD.02"=1

Then ran

gpioget gpiofind "PDD.02"

and got an output of 1 as expected. I then used gpioset to set the value to 0, but when I ran the gpioget command again it still said ‘1’ as the output.

I also tried to create a simple python script based on the Jetson.GPIO library linked, but that also did not work. Any ideas? I just need to set 8 solenoids to on/off on command. If there is an easier way to get this done I am open to that as well. Thanks!

I found a solution that works well now with GPIO pins and C++.

I downloaded this repo but made sure only to download the standalone zip and then extract it into a folder. I then followed the README running

sudo make
sudo make install

Then I simply compiled and ran the EXAMPLE_C++ files and they worked straight out of the box.

Hopefully someone else finds this useful and simple to use.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.