Skip to main content
This doc relates to the below revision of hardware
Mecha Logo

Comet (rev5)

For Pilot users

The all new Mecha Comet

Better in all ways, the next revision of Mecha Comet, coming soon.

Learn more

General-Purpose Input/Output (GPIO)

What is GPIO?

General-Purpose Input/Output (GPIO) refers to the configurable digital pins available on the Mecha Comet. These pins can be individually programmed to function as either inputs or outputs, providing a flexible interface to interact with external electronic components. Unlike dedicated communication interfaces (such as UART, I2C, and SPI) with fixed protocols, GPIO pins are versatile and their behavior is determined by user configuration.

Why Use GPIO?

GPIOs are fundamental in embedded systems for a wide range of tasks, including:

  • Controlling Peripherals: Driving external devices like LEDs, motors, relays, and actuators.
  • Reading Sensor Input: Receiving digital signals from sensors, switches, and buttons.
  • Low-Level Hardware Interaction: Enabling direct digital communication with external circuits.
  • Interrupt Handling: Configuring pins to detect external events and trigger interrupts.
  • Software-Defined Protocols: Implementing custom communication protocols through bit-banging.

GPIO vs. Dedicated Interfaces

Dedicated interfaces like I2C and UART adhere to specific communication protocols, simplifying interaction with devices designed for those protocols. GPIOs, however, offer greater flexibility. They can be configured to act as simple digital controls or be manipulated at the bit level to emulate other communication protocols when dedicated hardware is not available or suitable.

GPIO Usage on Mecha Comet

The Mecha Comet provides a number of general-purpose GPIO pins for flexible hardware interfacing. The specific mapping of these GPIOs to physical pin numbers and their associated names is crucial for proper configuration.

Pin NumberNameDescription
7GPIO2_IO12General-purpose GPIO (active low)
16GPIO2_IO15General-purpose GPIO (active low)
17GPIO2_IO16General-purpose GPIO (active low)
18GPIO2_IO17General-purpose GPIO (active low)
19GPIO2_IO18General-purpose GPIO (active low)
20GPIO4_IO28General-purpose GPIO (active low)
24GPIO2_IO20General-purpose GPIO (active low)
31GPIO1_IO13General-purpose GPIO (active low)
36GPIO2_IO14General-purpose GPIO (active low)
37GPIO2_IO13General-purpose GPIO (active low)

Accessing GPIO on Linux with libgpiod

On Linux-based systems, including the Mecha Comet, the libgpiod library is the recommended and modern way to interact with GPIO pins. It provides a stable and efficient user-space interface to the kernel's GPIO subsystem.

Listing Available GPIOs

To view the available GPIO lines, their current state, and their consumer (if any), use the gpioinfo command:

$ sudo gpioinfo

This command provides a comprehensive list of GPIO chips and the individual lines they manage.

Controlling GPIOs from the Command Line

The gpioset and gpioget utilities (part of the libgpiod-utils package) allow basic control of GPIOs from the command line.

Setting a GPIO as output and controlling its state:

$ gpioset gpiochip0 12=1  # Set GPIO line 12 on gpiochip0 to high (1)
$ gpioset gpiochip0 12=0 # Set GPIO line 12 on gpiochip0 to low (0)

Reading the state of a GPIO configured as input:

gpioget gpiochip0 12  # Read GPIO12 state

Configuring GPIO as Output Using gpiod

  1. Finding a GPIO line by name

    $ gpiofind "GPIO2_IO12"
  2. Monitoring GPIO events

    $ gpiomon gpiochip0 12

Example Use Case: LED Control (libgpiod)

Using Python

import gpiod
import time

def toggle_gpio(chip_name, line_num, state):
chip = gpiod.Chip(chip_name)
line = chip.get_line(line_num)
config = gpiod.LineSettings()
config.direction = gpiod.LINE_DIR_OUT
line.request(config)
line.set_value(state)
line.release()

toggle_gpio("gpiochip0", 12, 1) # Turn on LED
time.sleep(1)
toggle_gpio("gpiochip0", 12, 0) # Turn off LED

To Run the Python Script on the Mecha Comet:

  1. Save the script as toggle_gpio.py.
  2. Make sure you have gpiod installed:
    $ sudo apt install gpiod python3-gpiod
  3. Run the script:
    $ python3 toggle_gpio.py
  4. Ensure you have the necessary permissions to access GPIO lines. You may need to run the script with sudo or add your user to the gpio group.
  5. Check the LED connected to GPIO line 12 to see if it toggled on and off.

Use Cases

GPIOs are fundamental building blocks for Internet of Things (IoT) devices. They enable direct interaction with the physical world by:

  • Interfacing with Sensors: Reading data from a wide variety of sensors (temperature, humidity, motion, light, pressure, etc.).
  • Controlling Actuators: Driving motors, solenoids, valves, and other output devices.
  • Implementing Low-Power Features: Utilizing GPIOs for wake-up signals to conserve power.
  • Enabling Event-Driven Processing: Configuring GPIOs for edge-triggered interrupts to respond efficiently to external events.