Skip to content

Add GPIO beginners guide and fix incorrect API references in libgpiod docs#2493

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/extract-gpio-documentation
Draft

Add GPIO beginners guide and fix incorrect API references in libgpiod docs#2493
Copilot wants to merge 6 commits intomainfrom
copilot/extract-gpio-documentation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

Extracts GPIO documentation from #2438, rewritten for newcomers to IoT. Preserves the directory structure from the original PR (protocols/, fundamentals/). Removes irrelevant topics (Docker, deployment, systemd). Also fixes incorrect API references in the existing gpio-linux-libgpiod.md.

New: Documentation/protocols/gpio.md

Main GPIO protocol guide covering:

  • Table of contents for easy navigation
  • Quick-start LED blink with wiring diagram
  • Pin numbering (BCM vs physical) demystified, with pinout.xyz reference
  • Digital input introduction with button wiring and pull-up explanation
  • Pin modes, reading/writing, and interrupt-driven events
  • Complete button-controlled LED example
  • GpioController and driver overview with auto-detection table
  • Driver descriptions: RaspberryPi3Driver (fast direct memory access, requires root), SysFsDriver (slow but universal, deprecated)
  • Troubleshooting section including Pi 5 GPIO chip detection with bash snippet and kernel update guidance
  • Best practices

New: Documentation/fundamentals/gpio-basics.md

Deep dive on GPIO fundamentals:

  • Digital output and input concepts
  • Pull-up/pull-down resistors and why floating pins are bad
  • Interrupt-driven input with event types
  • Current and voltage limitations (3.3 V safety, ~15 mA with 220 Ω resistor, standard 20 mA indicator LEDs only — high-brightness LEDs should not be attached to GPIO directly)
  • Pin mode summary table
  • Common GPIO pins on Raspberry Pi

New: Documentation/fundamentals/choosing-drivers.md

Driver selection guide:

  • Quick comparison table (LibGpiodDriver vs SysFsDriver)
  • Auto-detection behavior per board
  • Explicit driver selection with code examples
  • Installing libgpiod and permission configuration
  • Summary decision table

Fixed: Documentation/gpio-linux-libgpiod.md

The existing doc had three references to API surface that doesn't exist:

// ❌ These don't exist
new GpioController(chipNumber);
new LibGpiodDriver(chipNumber, LibGpiodDriverVersion.V1);
// env var DOTNET_IOT_LIBGPIOD_DRIVER_VERSION also doesn't exist

// ✅ Actual API
new GpioController();
new GpioController(new LibGpiodDriver(gpioChip: 0));
new GpioController(new LibGpiodV2Driver(chipNumber: 0));

Also fixed variable name mismatch (gpioController vs controller) in the blink example, and documented that LibGpiodDriver and LibGpiodV2Driver are separate classes (not selected via an enum).

Updated: Documentation/README.md

Added links to all new GPIO guides in the GPIO section.

@krwq
Copy link
Copy Markdown
Member

krwq commented Apr 9, 2026

@copilot please preserve paths from the original PR

….md, fundamentals/gpio-basics.md, fundamentals/choosing-drivers.md)

Agent-Logs-Url: https://github.com/dotnet/iot/sessions/c24b48b9-932c-4ad1-a39d-b2fca64a84a2

Co-authored-by: krwq <660048+krwq@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 9, 2026

@copilot please preserve paths from the original PR

Restructured to match the original PR #2438 paths in commit 49cc9bf:

  • Documentation/protocols/gpio.md
  • Documentation/fundamentals/gpio-basics.md
  • Documentation/fundamentals/choosing-drivers.md

The flat gpio-beginners-guide.md has been removed.

Copilot AI requested a review from krwq April 9, 2026 15:57
Copilot finished work on behalf of krwq April 9, 2026 15:57
Comment thread Documentation/protocols/gpio.md Outdated

## What You Need

- A **Raspberry Pi** (3, 4, or 5) running Raspberry Pi OS
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any gpio board should be ok as long as it has unix and libgpiod installed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the Raspberry Pis can be mentioned as an example though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 7f680f7 — "What You Need" now says "A Linux single-board computer with GPIO headers and libgpiod installed" with Raspberry Pi mentioned as an example.

Comment thread Documentation/protocols/gpio.md Outdated
using System.Device.Gpio;
using System.Threading;

const int ledPin = 18; // GPIO 18 = physical pin 12 on Raspberry Pi
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to point to https://pinout.xyz/ for reference

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 7f680f7 — the quick-start code comment now includes a link to pinout.xyz: // GPIO 18 = physical pin 12 on Raspberry Pi (see https://pinout.xyz/)

controller.OpenPin(18, PinMode.Output);

// Input with pull-up resistor (default HIGH, button press = LOW)
controller.OpenPin(17, PinMode.InputPullUp);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no background on what input is and how it works or connect but we're going straight from output tutorial to pull downs/ups

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new "Digital Input" section in 7f680f7 that introduces what input is, shows a basic read example, illustrates button wiring, and explains why InputPullUp is needed — all before presenting the pin modes table.

4. **Use resistors with LEDs** — Calculate the value or use 220 Ω as a safe default
5. **Check pin assignments** — Some GPIO pins are shared with I2C, SPI, or UART; using them for general GPIO disables those interfaces
6. **Document your pin usage** — Especially in projects using multiple pins

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot you should mention that this will lead to 15mA current. Therefore a normal 20mA led is expected in this example and that high-brightness leds should not be attached to GPIO directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 7f680f7 — both the "Driving an LED" section and the best practices list now mention the ~15 mA current with a 220 Ω resistor, that this is fine for standard 20 mA indicator LEDs, and that high-brightness/power LEDs should not be connected to GPIO directly.

…uction, add LED current notes

Agent-Logs-Url: https://github.com/dotnet/iot/sessions/c7565b37-f74a-44ad-8f38-5993f685f665

Co-authored-by: raffaeler <5183152+raffaeler@users.noreply.github.com>
Copilot AI requested a review from raffaeler April 16, 2026 15:50
Comment thread Documentation/protocols/gpio.md Outdated
| `LibGpiodDriver` | Modern Linux with libgpiod v1 — the recommended default |
| `LibGpiodV2Driver` | Modern Linux with libgpiod v2 |
| `RaspberryPi3Driver` | Raspberry Pi 3/4 — auto-selected, legacy direct memory access |
| `SysFsDriver` | Very old Linux kernels (pre-4.8) only — **deprecated** |
Copy link
Copy Markdown
Member

@krwq krwq Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably also mention that it's very slow but also supported on any linux version

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in b1791c7SysFsDriver now reads: "Very slow but works on any Linux version — deprecated".

Comment thread Documentation/protocols/gpio.md Outdated
| --- | --- |
| `LibGpiodDriver` | Modern Linux with libgpiod v1 — the recommended default |
| `LibGpiodV2Driver` | Modern Linux with libgpiod v2 |
| `RaspberryPi3Driver` | Raspberry Pi 3/4 — auto-selected, legacy direct memory access |
Copy link
Copy Markdown
Member

@krwq krwq Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should focus on "very fast because of direct memory access" but also requires root permissions and it's not recommended way. For certain cases it's necessary to use it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in b1791c7RaspberryPi3Driver now reads: "very fast (direct memory access) but requires root permissions; not recommended unless you specifically need the performance".

Comment thread Documentation/protocols/gpio.md Outdated

## Raspberry Pi 5 Note

Raspberry Pi 5 moved its GPIO to a different chip number. If you are explicitly constructing a driver, use chip **4** instead of 0:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed in the recent versions of the kernel, no need to mention that really or mention that if 0 doesn't work and you're on pi 5 then occasionally it may be 4 but it's recommended to update kernel instead. You may also add tiny bash snippet how to detect it by looking at number of pins on each chip

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe troubleshooting section for this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked in b1791c7 — removed the dedicated Pi 5 section and moved it to a "Wrong GPIO Chip on Raspberry Pi 5" troubleshooting entry. It now recommends updating the kernel first, and includes a bash snippet to detect the correct chip by counting lines on each /dev/gpiochip*.

@@ -0,0 +1,292 @@
# GPIO (General-Purpose Input/Output)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also add table of content here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in b1791c7 — table of contents with links to all sections.

@krwq
Copy link
Copy Markdown
Member

krwq commented Apr 16, 2026

@copilot please fix the comments

Copilot AI and others added 2 commits April 16, 2026 16:02
Copilot finished work on behalf of krwq April 16, 2026 16:05
Copilot AI requested a review from krwq April 16, 2026 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants