# GPIO usage

## Available GPIO's

The following output provides a list of all GPIO's that are available to the Jetson. This includes Tegra GPIO's as well as external driven GPIO extender.

{% hint style="info" %}
If you want to do this Linux interface on a external connected GPIO extender, requires changes to the DeviceTree. A more detailed Instruction on how to implement such a device can be found in the [Linux kernel Documentation](https://www.kernel.org/doc/Documentation/devicetree/bindings/gpio/gpio.txt). &#x20;
{% endhint %}

Syntax: `sudo cat /sys/kernel/debug/gpio`

{% code title="Debug gpio output" lineNumbers="true" %}

```bash
test@test-desktop:~$ sudo cat /sys/kernel/debug/gpio
[sudo] password for test: 
gpiochip1: GPIOs 316-347, parent: platform/c2f0000.gpio, tegra234-gpio-aon:
 gpio-316 (PAA.00              )
 gpio-317 (PAA.01              )
 gpio-318 (PAA.02              )
 gpio-319 (PAA.03              )
 gpio-320 (PAA.04              )
 gpio-321 (PAA.05              |fixed-regulators:reg) out hi 
 gpio-322 (PAA.06              )
 gpio-323 (PAA.07              )
 gpio-324 (PBB.00              )
 gpio-325 (PBB.01              )
 gpio-326 (PBB.02              )
 gpio-327 (PBB.03              )
 gpio-328 (PCC.00              )
 gpio-329 (PCC.01              )
 gpio-330 (PCC.02              )
 gpio-331 (PCC.03              |mux                 ) out hi 
 gpio-332 (PCC.04              )
 gpio-333 (PCC.05              )
 gpio-334 (PCC.06              )
 gpio-335 (PCC.07              )
 gpio-336 (PDD.00              )
 gpio-337 (PDD.01              )
 gpio-338 (PDD.02              )
 gpio-339 (PEE.00              )
 gpio-340 (PEE.01              )
 gpio-341 (PEE.02              )
 gpio-342 (PEE.03              )
 gpio-343 (PEE.04              |power-key           ) in  hi IRQ ACTIVE LOW
 gpio-344 (PEE.05              )
 gpio-345 (PEE.06              )
 gpio-346 (PEE.07              )
 gpio-347 (PGG.00              )
gpiochip0: GPIOs 348-511, parent: platform/2200000.gpio, tegra234-gpio:
 gpio-348 (PA.00               |fixed-regulators:reg) out lo 
 gpio-349 (PA.01               )
 gpio-350 (PA.02               )
 gpio-351 (PA.03               )
# And so on...
```

{% endcode %}

{% hint style="warning" %}
The GPIO numbers listed in the Linux operating system do not match with the numbers of the Jetson socket pins.&#x20;
{% endhint %}

## Export GPIO

At first, you have to export the GPIO that you want to use in the Linux file system in order to configure and read or write data from it.

Syntax: `echo <GPIO_NR> > /sys/class/gpio/export`

{% code title="export gpio" lineNumbers="true" %}

```bash
nvidia@nvidia-desktop:~$ echo 414 > /sys/class/gpio/export
nvidia@nvidia-desktop:~$
```

{% endcode %}

## Change GPIO direction

The GPIO direction decides if the GPIO should be used as Input or Output&#x20;

{% hint style="info" %}
Keep in mind that some Carrierboards are using unidirectional level shifters -> In this case only one direction will work properly.&#x20;

More Information about such implementations are documented in the corresponding manual of your Carrierboard.
{% endhint %}

### Syntax

{% tabs %}
{% tab title="jetpack 4.6 <=" %}
{% code title="change direction" overflow="wrap" lineNumbers="true" %}

```bash
echo <in|out> > /sys/class/gpio/gpio<GPIO_NR>/direction
```

{% endcode %}
{% endtab %}

{% tab title="Jetpack 5.0 >=" %}
{% code title="Change direction" overflow="wrap" lineNumbers="true" %}

```bash
echo <in|out> > /sys/class/gpio/gpio<GPIO_NAME>/direction
```

{% endcode %}
{% endtab %}

{% tab title="External driven extender" %}
{% code title="Change direction" overflow="wrap" lineNumbers="true" %}

```bash
echo <in|out> > /sys/class/gpio/gpio<GPIO_NR>/direction
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Configure as Input

{% code title="set GPIO direction as input" lineNumbers="true" %}

```bash
nvidia@nvidia-desktop:~$ echo in > /sys/class/gpio/gpio414/direction
nvidia@nvidia-desktop:~$
```

{% endcode %}

### Configure as Output

{% code title="set GPIO direction as output" lineNumbers="true" %}

```bash
nvidia@nvidia-desktop:~$ echo out > /sys/class/gpio/gpio414/direction
nvidia@nvidia-desktop:~$
```

{% endcode %}

## Set GPIO Value

The logical GPIO level can be configure in the following ways. Keep in mind that its required to configure the GPIO as output before changing its output value.&#x20;

### Syntax

{% tabs %}
{% tab title="jetpack 4.6 <=" %}
{% code title="change direction" overflow="wrap" lineNumbers="true" %}

```bash
echo <0|1> > /sys/class/gpio/gpio<GPIO_NR>/value
```

{% endcode %}
{% endtab %}

{% tab title="Jetpack 5.0 >=" %}
{% code title="Change direction" overflow="wrap" lineNumbers="true" %}

```bash
echo <0|1> > /sys/class/gpio/gpio<GPIO_NAME>/value
```

{% endcode %}
{% endtab %}

{% tab title="External driven extender" %}
{% code title="Change direction" overflow="wrap" lineNumbers="true" %}

```bash
echo <0|1> > /sys/class/gpio/gpio<GPIO_NR>/value
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Set low

{% code title="Set output as logical low" lineNumbers="true" %}

```bash
nvidia@nvidia-desktop:~$ echo 0 > /sys/class/gpio/gpio414/value
nvidia@nvidia-desktop:~$
```

{% endcode %}

### Set high

{% code title="Set output as logical high" lineNumbers="true" %}

```bash
nvidia@nvidia-desktop:~$ echo 1 > /sys/class/gpio/gpio414/value
nvidia@nvidia-desktop:~$
```

{% endcode %}

## Read GPIO state

### Syntax

{% tabs %}
{% tab title="jetpack 4.6 <=" %}
{% code title="change direction" overflow="wrap" lineNumbers="true" %}

```bash
cat /sys/class/gpio/gpio<GPIO_NR>/value
```

{% endcode %}
{% endtab %}

{% tab title="Jetpack 5.0 >=" %}
{% code title="Change direction" overflow="wrap" lineNumbers="true" %}

```bash
cat /sys/class/gpio/gpio<GPIO_NAME>/value
```

{% endcode %}
{% endtab %}

{% tab title="External driven extender" %}
{% code title="Change direction" overflow="wrap" lineNumbers="true" %}

```bash
cat /sys/class/gpio/gpio<GPIO_NR>/value
```

{% endcode %}
{% endtab %}
{% endtabs %}

The following example reads the current state of the GPIO. This operation works in output as well as in input mode.&#x20;

{% code title="read GPIO state " lineNumbers="true" %}

```bash
nvidia@nvidia-desktop:~$ cat /sys/class/gpio/gpio414/value
0
nvidia@nvidia-desktop:~$ cat /sys/class/gpio/gpio414/value
1
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://auvidea.gitbook.io/gpio/guide/gpio-usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
