GPIO Problems - Functions don't seem to work (ESP-IDF v5.3.1)

egionet
Posts: 7
Joined: Thu Sep 26, 2024 4:34 am

GPIO Problems - Functions don't seem to work (ESP-IDF v5.3.1)

Postby egionet » Fri Jan 03, 2025 10:35 pm

Hello,

I've been scratching my head over the past few hours and stumped. I didn't think it would be this trivial and wondered if anyone else was encountering the same problem. I am down to the basics and still can't get it to work.

Code: Select all

static void i2c_0_task( void *pvParameters ) {
    // initialize the xLastWakeTime variable with the current time.
    TickType_t xLastWakeTime = xTaskGetTickCount ();
    //
    //
    uint8_t gpio_pin = GPIO_NUM_13;
    gpio_reset_pin(gpio_pin);
    esp_rom_gpio_pad_select_gpio(gpio_pin);
    gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
    //
    // task loop entry point
    for ( ;; ) {

        if(gpio_get_level(gpio_pin) == 1) {
            gpio_set_level(gpio_pin, 0);
        } else {
            gpio_set_level(gpio_pin, 1);
        }

        ESP_LOGI(APP_TAG, "GPIO %d: %d", gpio_pin, gpio_get_level(gpio_pin));
        
        //
        //
        // pause the task per defined wait period
        vTaskDelaySecUntil( &xLastWakeTime, 5 );
    }
    //
    // free up task resources and remove task from stack
    vTaskDelete( NULL );
}
The above code toggles the gpio from high to low but in my case the gpio stays low.

esp32 pinout reference: https://randomnerdtutorials.com/esp32-p ... nce-gpios/

Any suggestions would be greatly appreciated.

boarchuz
Posts: 626
Joined: Tue Aug 21, 2018 5:28 am

Re: GPIO Problems - Functions don't seem to work (ESP-IDF v5.3.1)

Postby boarchuz » Sat Jan 04, 2025 2:52 am

Try using

Code: Select all

gpio_config
to initialise the pin

Oh also gpio_get_level reads the pin's input level, it does not read the pin's configured output level. Since you have not enabled the input function on the pin, the returned value will never change.Use INPUT_OUTPUT mode or a local variable.

User avatar
mbratch
Posts: 305
Joined: Fri Jun 11, 2021 1:51 pm

Re: GPIO Problems - Functions don't seem to work (ESP-IDF v5.3.1)

Postby mbratch » Sat Jan 18, 2025 2:18 pm

Did you initialize the pin? As was mentioned, you can use `gpio_config`, or you can do a simple init with:

Code: Select all

    gpio_reset_pin(gpio_num);
    gpio_set_direction(gpio_num, mode);
Where `mode` is `GPIO_MODE_INPUT` or `GPIO_MODE_OUTPUT`

mikemoy
Posts: 634
Joined: Fri Jan 12, 2018 9:10 pm

Re: GPIO Problems - Functions don't seem to work (ESP-IDF v5.3.1)

Postby mikemoy » Sat Jan 18, 2025 10:48 pm

If memory serves when you have a GPIO pin configured as an output, when you try to read the pin it does not read the actual I/O pin state.

Try this.

Code: Select all

static void i2c_0_task( void *pvParameters ) {
    // initialize the xLastWakeTime variable with the current time.
    TickType_t xLastWakeTime = xTaskGetTickCount ();
    //
    //
    BaseType_t io_state = pdFALSE;
    uint8_t gpio_pin = GPIO_NUM_13;
    gpio_reset_pin(gpio_pin);
    esp_rom_gpio_pad_select_gpio(gpio_pin);
    gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
    //
    // task loop entry point
    for ( ;; ) {

        if(io_state == pdTRUE) {
            gpio_set_level(gpio_pin, 0);
	    io_state = pdFALSE;
        } else {
            gpio_set_level(gpio_pin, 1);
	    io_state = pdTRUE;
        }

        ESP_LOGI(APP_TAG, "GPIO %d: %d", gpio_pin, io_state);
        
        //
        //
        // pause the task per defined wait period
        vTaskDelaySecUntil( &xLastWakeTime, 5 );
    }
    //
    // free up task resources and remove task from stack
    vTaskDelete( NULL );
}

egionet
Posts: 7
Joined: Thu Sep 26, 2024 4:34 am

Re: GPIO Problems - Functions don't seem to work (ESP-IDF v5.3.1)

Postby egionet » Sat Jan 25, 2025 10:44 am

mbratch wrote:
Sat Jan 18, 2025 2:18 pm
Did you initialize the pin? As was mentioned, you can use `gpio_config`, or you can do a simple init with:

Code: Select all

    gpio_reset_pin(gpio_num);
    gpio_set_direction(gpio_num, mode);
Where `mode` is `GPIO_MODE_INPUT` or `GPIO_MODE_OUTPUT`
Yes, I tried both implementation methods as documented here https://docs.espressif.com/projects/esp ... /gpio.html. However, I see what you mean given it is configured as an output, so reading the level won't work. I'll try toggling the pin while analyzing it with a logic analyzer.

egionet
Posts: 7
Joined: Thu Sep 26, 2024 4:34 am

Re: GPIO Problems - Functions don't seem to work (ESP-IDF v5.3.1)

Postby egionet » Sat Jan 25, 2025 12:30 pm

After further testing, this issue is resolved.

Who is online

Users browsing this forum: Bing [Bot] and 79 guests