Documentation/Allspark2-Orin NX WikiEnglish · NX
Browse documentation
Additional documentation

Controlling GPIO in C

Note GPIO control must be run in root mode.

2 min read · English documentation

Note

GPIO control must be run in root mode.

The following example sets PQ.05 (453) to output a high level.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define GPIO_PATH "/sys/class/gpio/"
#define GPIO_NAME "PQ.05"

int gpio_exists(const char *gpio) {
    char path[256];
    snprintf(path, sizeof(path), "%s%s", GPIO_PATH, gpio);
    if (access(path, F_OK) == 0) {
        return 1;
    } else {
        return 0;      }
}

void write_to_file(const char *path, const char *value) {
    int fd = open(path, O_WRONLY);
    if (fd == -1) {
        perror("Error opening file");
        exit(1);
    }

    if (write(fd, value, strlen(value)) == -1) {
        perror("Error writing to file");
        close(fd);
        exit(1);
    }

    close(fd);
}

int main() {
    if (!gpio_exists(GPIO_NAME)) {
        write_to_file(GPIO_PATH "export", "453");
    } else {
        printf("GPIO %s already exists. Skipping export.\n", GPIO_NAME);
    }

    write_to_file(GPIO_PATH "PQ.05/direction", "out");

    write_to_file(GPIO_PATH "PQ.05/value", "1");

    printf("GPIO %s (PQ.05) set to output and value 1.\n",GPIO_PIN);

    return 0;
}

Function Descriptions

1. gpio_exists(const char *gpio)

Function: This function checks whether the specified GPIO pin has already been exported (whether it exists in the /sys/class/gpio/ directory). It returns 1 if the specified GPIO pin exists; otherwise, it returns 0.

Parameter: gpio: A string containing the GPIO pin name or number (such as 453 or PQ.05).

Return value:

Returns 1 if the GPIO pin exists.

Returns 0 if the GPIO pin does not exist.

Example:

Export GPIO 453:

echo 453 > /sys/class/gpio/export 

Then go to /sys/class/gpio/:

cd /sys/class/gpio/

View the GPIO name corresponding to 453:

ls 

image.png

You can also refer to the table below.

Pin Parameter PIN 1 PIN 2 PIN 3 PIN4 PIN 5 PIN6
I/O Name 3.3V IO9 IO11 IO1 IO13 GND
Internal Software Name / PAC.06 PQ.06 PQ.05 PH.00 /
Internal Software Number / 492 454 453 391 /
int exists = gpio_exists("PQ.05");     // Enter the GPIO name obtained above
if (exists) {
    printf("GPIO exists\n");
} else {
    printf("GPIO does not exist\n");
}

2. write_to_file(const char *path, const char *value)

Function: This function writes the specified value to the file at path. It opens the file, writes the content, and closes the file.

Parameters:

path: The path of the file to which data will be written.

value: The string to write to the file.

Return value: This function does not return a value. If the write operation fails, it prints an error message and terminates the program.

Example:

write_to_file("/sys/class/gpio/export", "453")   // Export GPIO 453
write_to_file("/sys/class/gpio/PQ.05/direction", "out");// Set the direction of GPIO 453 to output
write_to_file("/sys/class/gpio/PQ.05/value", "1"); // Set the value of GPIO 453 to 1 (1 is high level; 0 is low level) 

Compiling

gcc -o gpio_test gpio_test.c

Running

sudo su
./gpio_test