<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.koansoftware.com/index.php?action=history&amp;feed=atom&amp;title=Manage_the_GPIO_lines_in_C_with_libgpiod</id>
	<title>Manage the GPIO lines in C with libgpiod - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.koansoftware.com/index.php?action=history&amp;feed=atom&amp;title=Manage_the_GPIO_lines_in_C_with_libgpiod"/>
	<link rel="alternate" type="text/html" href="https://wiki.koansoftware.com/index.php?title=Manage_the_GPIO_lines_in_C_with_libgpiod&amp;action=history"/>
	<updated>2026-04-17T19:30:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.15</generator>
	<entry>
		<id>https://wiki.koansoftware.com/index.php?title=Manage_the_GPIO_lines_in_C_with_libgpiod&amp;diff=369&amp;oldid=prev</id>
		<title>Koan: Created page with &quot;== Manage the GPIO lines in C with libgpiod ==  Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use the character device instead.   &#039;&#039;&#039;libgpiod&#039;&#039;&#039; en...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.koansoftware.com/index.php?title=Manage_the_GPIO_lines_in_C_with_libgpiod&amp;diff=369&amp;oldid=prev"/>
		<updated>2024-09-12T09:07:49Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Manage the GPIO lines in C with libgpiod ==  Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use the character device instead.   &amp;#039;&amp;#039;&amp;#039;libgpiod&amp;#039;&amp;#039;&amp;#039; en...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Manage the GPIO lines in C with libgpiod ==&lt;br /&gt;
&lt;br /&gt;
Since linux 4.8 the GPIO sysfs interface is deprecated. User space should use the character device instead. &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;libgpiod&amp;#039;&amp;#039;&amp;#039; encapsulates the ioctl calls and data structures behind a straightforward API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Manage the GPIO using the C language ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
- [https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/README libgpiod documentation]&lt;br /&gt;
&lt;br /&gt;
- [https://github.com/starnight/libgpiod-example libgpiod-example git repo]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;gpiod.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
  #include &amp;lt;unistd.h&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
  #ifndef	CONSUMER&lt;br /&gt;
  #define	CONSUMER	&amp;quot;Consumer&amp;quot;&lt;br /&gt;
  #endif&lt;br /&gt;
  &lt;br /&gt;
  int main(int argc, char **argv)&lt;br /&gt;
  {&lt;br /&gt;
    char *chipname = &amp;quot;gpiochip0&amp;quot;;&lt;br /&gt;
    unsigned int line_num = 23;	// GPIO Pin #23&lt;br /&gt;
    unsigned int val;&lt;br /&gt;
    struct gpiod_chip *chip;&lt;br /&gt;
    struct gpiod_line *line;&lt;br /&gt;
    int i, ret;&lt;br /&gt;
  &lt;br /&gt;
    chip = gpiod_chip_open_by_name(chipname);&lt;br /&gt;
    if (!chip) {&lt;br /&gt;
      perror(&amp;quot;Open chip failed\n&amp;quot;);&lt;br /&gt;
      goto end;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    line = gpiod_chip_get_line(chip, line_num);&lt;br /&gt;
    if (!line) {&lt;br /&gt;
      perror(&amp;quot;Get line failed\n&amp;quot;);&lt;br /&gt;
      goto close_chip;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    ret = gpiod_line_request_output(line, CONSUMER, 0);&lt;br /&gt;
    if (ret &amp;lt; 0) {&lt;br /&gt;
      perror(&amp;quot;Request line as output failed\n&amp;quot;);&lt;br /&gt;
      goto release_line;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
    /* Blink 20 times */&lt;br /&gt;
    val = 0;&lt;br /&gt;
    for (i = 20; i &amp;gt; 0; i--) {&lt;br /&gt;
      ret = gpiod_line_set_value(line, val);&lt;br /&gt;
      if (ret &amp;lt; 0) {&lt;br /&gt;
        perror(&amp;quot;Set line output failed\n&amp;quot;);&lt;br /&gt;
        goto release_line;&lt;br /&gt;
      }&lt;br /&gt;
      printf(&amp;quot;Output %u on line #%u\n&amp;quot;, val, line_num);&lt;br /&gt;
      sleep(1);&lt;br /&gt;
      val = !val;&lt;br /&gt;
    }&lt;br /&gt;
  &lt;br /&gt;
  release_line:&lt;br /&gt;
    gpiod_line_release(line);&lt;br /&gt;
  close_chip:&lt;br /&gt;
    gpiod_chip_close(chip);&lt;br /&gt;
  end:&lt;br /&gt;
    return 0;&lt;br /&gt;
  }&lt;/div&gt;</summary>
		<author><name>Koan</name></author>
	</entry>
</feed>