You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
c-gpio-intro/Readme.md

3.1 KiB

GPIO Command-Line Tool in C

A simple command-line tool written in C to control Raspberry Pi GPIO pins using the Linux GPIO sysfs interface.

The program supports configuring a GPIO as an input or output, writing a digital value, and reading the state of an input.

Compile

Compile the program using GCC:

gcc main.c -o gpio

Usage

The general command syntax is:

./gpio <gpio> <direction> [value]

GPIO as Output

Configure GPIO 17 as an output and set it HIGH:

./gpio 17 out 1

Set GPIO 17 LOW:

./gpio 17 out 0

When out is selected, the value argument is required:

./gpio <gpio> out <0|1>

For example, the following command is invalid:

./gpio 17 out

because the output value is missing.

GPIO as Input

Configure GPIO 17 as an input and read its current digital state:

./gpio 17 in

The program reads the GPIO value file and displays either:

0

or:

1

No additional value argument is required for an input.

Program Flow

The program follows this sequence:

Command-line arguments
        │
        ▼
Validate direction
        │
        ├── out → verify value argument
        │
        └── in  → no value required
        │
        ▼
Export GPIO
        │
        ▼
Set GPIO direction
        │
        ├── out → gpio_write()
        │
        └── in  → gpio_read()
        │
        ▼
Unexport GPIO

Functions

The program is divided into the following functions:

int gpio_export(const char *gpio);
int gpio_direction(const char *gpio, const char *direction);
int gpio_write(const char *gpio, const char *value);
int gpio_read(const char *gpio);
int gpio_unexport(const char *gpio);

Their responsibilities are:

  • gpio_export() — exports the selected GPIO.
  • gpio_direction() — configures the GPIO as in or out.
  • gpio_write() — writes 0 or 1 to an output.
  • gpio_read() — reads the digital state of an input.
  • gpio_unexport() — releases the GPIO after the operation.

GPIO sysfs Files

For GPIO 17, the program accesses files such as:

/sys/class/gpio/chip0/gpio17/direction
/sys/class/gpio/chip0/gpio17/value

The direction file determines whether the GPIO is configured as an input or output.

The value file is used to read or write the digital state of the GPIO.

Example

Output:

./gpio 17 out 1

Input:

./gpio 17 in

Invalid output command:

./gpio 17 out

Expected error:

Error: GPIO out option requires a value.
Usage: ./gpio <gpio> out <0|1>

Current Features

  • GPIO export and unexport
  • Input/output direction configuration
  • Digital output (0 or 1)
  • Digital input reading
  • Command-line argument validation
  • Invalid direction detection

Future Work

A future version will include a blink operation:

./gpio 17 blink 500

where 500 represents the blinking interval in milliseconds.

This version now reflects the structure of your current C implementation, including the separation between argument validation and GPIO operations.