# How can you set and read the GPIO pins using the C SDK?

**URL:** <https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808>\
**Category:** PicoCalc\
**Created:** [September 21, 2025, 2:45am UTC](https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808 "2025-09-21T02:45:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![llee454](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/llee454/32/13380_2.png) [@llee454](https://forum.clockworkpi.com/u/llee454)\
**Post date:** [September 21, 2025, 2:45am UTC](https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808/1 "2025-09-21T02:45:23Z")

</div>

Hey all,

I’ve been trying to figure out how to set and read the GPIO pins using the C SDK. So far, I haven’t been able to get any of them to show any output when I attach a voltimeter. Can anyone tell me what I’m doing wrong? Here’s what I’ve written so far:

```auto
/*
  Build by running the following commands:

  PICO_SDK_PATH=/home/larry/Projects/pico-sdk/ cmake -S . -B build -DPICO_BOARD=pico
  cmake --build build --target hello_world
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/clocks.h"
#include "i2ckbd.h"
#include "lcdspi.h"
#include "psram_spi.h"
#include "pwm_sound.h"

int main() {
  set_sys_clock_khz(133000, true);
  stdio_init_all();
  init_i2c_kbd();
  lcd_init();
  lcd_clear();
  lcd_print_string("Testing GPIO pins\n");

  uint gpio = 1;
  bool value = 1;
  char* message = malloc (100 * sizeof (char));

  gpio_set_dir (1, GPIO_OUT);
  gpio_set_dir (2, GPIO_OUT);
  gpio_set_dir (3, GPIO_OUT);
  gpio_set_dir (4, GPIO_OUT);
  while (1) {
    sleep_ms(2000);
    sprintf (message, "Testing GPIO pins sending value out %u\n", (uint) value);
    lcd_print_string(message);
    gpio_put (1, value);
    gpio_put (2, value);
    gpio_put (3, value);
    gpio_put (4, value);
    value = !value; 
  }
  return 0;
}

```

The program displays the Testing message and appears to toggle between sending a 1 and a 0 signal to the various IO pins once every two seconds, but my voltimeter does not display any output.

The rest of the program (the build environment etc can be found in my demo repo in Codeberg - [llee454/picocalc-test: A demonstration project showing how to compile, build, and flash C programs for the PicoCalc. - Codeberg.org](https://codeberg.org/llee454/picocalc-test).

Thanks!

- L

---

<div class="post-metadata">

**Author:** ![pelrun](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/pelrun/32/12323_2.png) [@pelrun](https://forum.clockworkpi.com/u/pelrun)\
**Post date:** [September 21, 2025, 3:35am UTC](https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808/2 "2025-09-21T03:35:12Z")

</div>

The pins that go to the outside connector are not “1,2,3,4”.

---

<div class="post-metadata">

**Author:** ![pelrun](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/pelrun/32/12323_2.png) [@pelrun](https://forum.clockworkpi.com/u/pelrun)\
**Post date:** [September 21, 2025, 5:42am UTC](https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808/3 "2025-09-21T05:42:25Z")

</div>

Now that I’m actually awake and could check, the external pins are 2,3,4,5,21,28. So at least a couple of them should toggle.

But you’re missing calls to `gpio_init(<pin>)` for each pin you’re using as gpio. Every pin has a mux that selects the peripheral it’s connected to, and unless you explicitly select it for gpio (which gpio\_init does), chances are high that it’s actually defaulting to some other peripheral.

---

<div class="post-metadata">

**Author:** ![pelrun](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/pelrun/32/12323_2.png) [@pelrun](https://forum.clockworkpi.com/u/pelrun)\
**Post date:** [September 21, 2025, 5:52am UTC](https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808/4 "2025-09-21T05:52:02Z")

</div>

![image](https://canada1.discourse-cdn.com/flex029/uploads/clockworkpi/original/2X/2/2f9fd35245a979e824147c526eb6dbce0e2c8b70.png)

Yup, the pins all default to disconnected (function NULL) on reset.

---

<div class="post-metadata">

**Author:** ![llee454](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/llee454/32/13380_2.png) [@llee454](https://forum.clockworkpi.com/u/llee454)\
**Post date:** [September 21, 2025, 1:49pm UTC](https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808/5 "2025-09-21T13:49:41Z")

</div>

@pelrun - you are awesome! Thanks for the quick reply. I will fix this and test immediately!

---

<div class="post-metadata">

**Author:** ![llee454](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/llee454/32/13380_2.png) [@llee454](https://forum.clockworkpi.com/u/llee454)\
**Post date:** [September 21, 2025, 4:06pm UTC](https://forum.clockworkpi.com/t/how-can-you-set-and-read-the-gpio-pins-using-the-c-sdk/19808/6 "2025-09-21T16:06:22Z")

</div>

I added the gpio\_init function calls to my code and successfully observed the pins toggling ± 3V on the selected pins! Success. I’m going to try reading values from these pins next. Please let me know if there’s anything I should do to read values.

Also, It appears that these pins can be used to drive various other protocols. Can anyone drop in a description about how we can uses these other protocols through the C SDK?

I’ll keep posting my demo/test code here: [Making sure you're not a bot!](https://codeberg.org/llee454/picocalc-test/src/branch/main/picocalc-test/gpio_example.c)

Cheers!
