How to configure a GPIO on TX2?


Can you please help me with a C++ code including headers to toggle pin 23 - SPI_SCLK continously ?


I am experiencing the same issues: The GPIOs on J21 did work but now they don’t anymore. I have gpiochip488 under /sys/class/gpio. How did you fix that?

One issue I found is that the level shifter may change reliablability. If you look at J24 this optionally selects between 3.3V TTL levels and 1.8V. In terms of software commands doing an echo into “/sys” gpio files this will not matter…but output levels may show correct behavior at 3.3V even when not correct at 1.8V. Check this jumper and test the output reliability at both 1.8V and 3.3V…see if one is reliable even when the other is not. I’m not positive (it was quite some time back), but I think it was 3.3V which was working for me.

Hi linuxdev,

I have configued the GPIO Pin37( gpio388) on TX2 ,with the following step:

cd /sys/class/gpio

echo 388 > export

cd gpio388

echo out > direction

echo 1   > value 

cat value


My target is that ,Driving a small steper motor & led, but why , nothing happened .

what I should to do ???

to make gpio388 out with high voltage(3.3v)

Check with a voltmeter and see if manually changing 1/0 works with essentially no load. Monitor “dmesg --follow” while doing so.

It may be that if this succeeds you need to add a buffer to your stepper.

hi linuxdev,
i check pin37 voltage with voltmeter, but whatever value be setted (1or0) ,the output voltage is 3.19v

if i changed to connect the motor with pin39 and pin1,the motor work fine.the voltage is 3.3v

what i should to do,more

This may be a bug or issue, but you’ll want to first state if you have any device tree modifications. After that give the exact pin you are checking voltage on, the L4T release you are using (“head -n 1 /etc/nv_tegra_release”), and the commands you are using in “/sys”. With that combination of information something could be verified.

1 I have changed nothing about device tree。
root@tegra-ubuntu:/sys/class/gpio# echo 388 > export
root@tegra-ubuntu:/sys/class/gpio# cd gpio388
root@tegra-ubuntu:/sys/class/gpio/gpio388# ls
active_low device direction edge power subsystem uevent value
root@tegra-ubuntu:/sys/class/gpio/gpio388# cat direction
root@tegra-ubuntu:/sys/class/gpio/gpio388# echo 1 > value
root@tegra-ubuntu:/sys/class/gpio/gpio388# cat value
root@tegra-ubuntu:/sys/class/gpio/gpio388# head -n 1 /etc/nv_tegra_release

R28 (release), REVISION: 1.0, GCID: 9379712, BOARD: t186ref, EABI: aarch64, DATE: Thu Jul 20 07:59:31 UTC 2017


Which pin are you measuring voltage on to see if it reflects the state change?

Refer to the following maping table

the PIN37/port388/GPIO26 is used by me!

thanks alot!

My TX2 dev board (R28.2) works correctly with the above. My J24 jumper is set for 3.3V level shifter (pins furthest from the board corner are jumpered). Are your J24 jumpers the same? If you measure the voltage on the jumper itself in that position is it 3.3V?

Just something to cross check…this will verify what you think the state of gpio-388 is:

sudo -s
watch -n 1 egrep 388 /sys/kernel/debug/gpio

If 3.3V does not work try shutting down, remove power, discharge a few seconds with the power button, change J24 to 1.8V position, and try again…then back to 3.3V. Does this finally work at 3.3V? I have had trouble in the past on 1.8V.

if I change J24 to 1.8V position, and measure the voltage on the jumper24 itself in that position. it is 0.8V.
if I change J24 to 3.3V position, and measure the voltage on the jumper24 itself in that position. it is 2.35V.
after I change J24 to 1.8v position ,and then change J24 to 3.3V position, I can measure the change of gpio388 voltage .

so , I think , the root reason is gpio388 have no enough power to driver my steper_motor.

now , instead of gpio388, I used the GPIO466 , the stepper_motor work fine !

thanks a lot!

Ahh! I did not know there was a stepper directly connected. Yes, put a buffer on that…GPIO wasn’t meant to drive a motor (it might drive it, it depends on the specific case).

The Jetson GPIO pins are rated at only one or two milliamps of current. Some of the pins use voltage level translators that may be rated for slightly more, but I wouldn’t expect a drive capability of anything above 6 mA or so.
If you try to drive a heavy load, you will burn out parts of the motherboard, or even parts of the Jetson.

There’s another risk with motors (and relays, and other inductive devices): back EMF. For example, if the motor is rotated when not powered, it will work as a generator, and will generate a voltage into the Jetson motherboard that is potentially a lot higher than what the pins are rated for.

When driving electromechanical parts, ALWAYS use a separate driver! Else you will destroy your expensive computer hardware. Examples of affordable drivers include the various Texas and LM chips (LM298, TI8825, and so forth.) You can buy these reasonably inexpensively from places like Pololu or Sparkfun, or even cheaper from places like ebay and banggood.

thanks linuxdev and sharky
from now on,i will use gpio388 as a on/off signal only .

I tried exporting pin 35(gpio395), pin 37(gpio388), pin 40(gpio393), and pin 29(gpio398) and setting the value to 1 and direction to out, and cat shows the correct values. But only pin29 works with giving me the digital output I need. Using a voltmeter on pin37 shows 0.01V, which is really puzzling because it should work according to the above comments. Any ideas?

Also, I need 12 digital outputs. Is J21 able to provide that many, or should I try some other header ports? If so, please advise on how!


Some pins may already be in use and unless removed from that other function they won’t be available. I assume you are talking about J21 on the dev board? Under R28.2.1?

I haven’t checked if those are actually the correct pins for those particular gpio. How did you determine/map which pin belongs to which gpio number for export?

Before you try to export a GPIO be sure it isn’t already in “/sys/kernel/debug/gpio”, and then after export check again for both existence and direction.

I have seen cases where the level shifter does not always seem to perform as it should. Some I/O is selectable between 1.8V and 3.3V via J24 and I found 3.3V worked when 1.8V did not, but I never investigated it further.

Yes I’m talking about J21, but I think my jetpack version is 28.2.0?
“# R28 (release), REVISION: 2.0, GCID: 10136452, BOARD: t186ref, EABI: aarch64, DATE: Fri Dec 1 14:20:33 UTC 2017”

I used the GPIO mapping in

And now not even pin 29 is working for me!

I did what you said and checked “/sys/kernel/debug/gpio”, and everything was as you said:
before exporting 396 (pin 7) and 398 (pin 29) for example, I get
GPIOs 320-511, platform/2200000.gpio, tegra-gpio: gpio-381 ( |reset_gpio ) out lo gpio-412 ( |vdd-usb0-5v ) out lo gpio-413 (
after exporting, I get
GPIOs 320-511, platform/2200000.gpio, tegra-gpio: gpio-381 ( |reset_gpio ) out lo gpio-396 ( |sysfs ) out hi gpio-398 ( |sysfs ) out hi gpio-412 ( |vdd-usb0-5v ) out lo gpio-413 (
but neither of them turn on my test LED and when testing with a voltmeter I get a very low voltage like 0.06V.

I also tried echoing the value and direction in “/sys/devices/2200000.gpio/gpio/gpio398” for example, and it didn’t work.

Also I tried shifting the jumper on J24 in both positions, pins 1-2 (default) which is supposed to set the I/O to 3.3V and pins 2-3 for 1.8V, and nothing changed on my voltmeter.

I’m completely stumped?

For reference, I am using this for my pinout:

Looking at these pins of J21:

pin 31: GPIO298
pin 33: GPIO389
pin 37: GPIO388

First, make sure the voltage to pins 1 and 3 of J24 are working. This is the power to the level select jumper, so pin 1 is 3.3V and pin 3 is 1.8V. If these are not up, then nothing will work. I am using 3.3V.

Next, know that an LED probably exceeds the load capacity of GPIO. You shouldn’t use an LED directly on this pin (even with a resistor). Though not likely, it might be possible you could even damage the level shifter when drawing too much current. Use a buffer or driver.

For reference, I am using “sudo -s” to drop into a root shell for all of this. It’s easier than using “sudo” on every line.

Each time you want to see the current status of one of these run this command:

egrep '(298|388|389)' /sys/kernel/debug/gpio

Pin 31, GPIO298:

echo '298' > /sys/class/gpio/export
echo 'out' > /sys/class/gpio/gpio298/direction
echo '1' > /sys/class/gpio/gpio298/value
egrep '(298|388|389)' /sys/kernel/debug/gpio
# implies gpio-298 (                    |sysfs               ) out hi 
# Pin 31 is 3.3V
echo '0' > /sys/class/gpio/gpio298/value
egrep '(298|388|389)' /sys/kernel/debug/gpio
# implies  gpio-298 (                    |sysfs               ) out lo 
# Pin 31 is about 90mV

Same for pin 33 (GPIO389)…

echo '389' > /sys/class/gpio/export
echo 'out' > /sys/class/gpio/gpio389/direction
echo '1' > /sys/class/gpio/gpio389/value
egrep '(298|388|389)' /sys/kernel/debug/gpio
# implies gpio-389 (                    |sysfs               ) out hi 
# Pin 31 is 3.3V
echo '0' > /sys/class/gpio/gpio389/value
egrep '(298|388|389)' /sys/kernel/debug/gpio
# implies  gpio-389 (                    |sysfs               ) out lo 
# Pin 31 is about 90mV

And pin 37 (GPIO388)…

echo '388' > /sys/class/gpio/export
echo 'out' > /sys/class/gpio/gpio388/direction
echo '1' > /sys/class/gpio/gpio388/value
egrep '(298|388|389)' /sys/kernel/debug/gpio
# implies gpio-388 (                    |sysfs               ) out hi 
# Pin 31 is 3.3V
echo '0' > /sys/class/gpio/gpio388/value
egrep '(298|388|389)' /sys/kernel/debug/gpio
# implies  gpio-388 (                    |sysfs               ) out lo 
# Pin 31 is about 90mV

Earlier I did not know you had an LED on it. If you have issues without the LED it might be a problem, but I would expect any significant load to cause voltage measurements to be off. I’m not sure how software would react under too much load.