Page 1 of 1

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

Posted: Fri Jan 03, 2025 10:35 pm
by egionet
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.

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

Posted: Sat Jan 04, 2025 2:52 am
by boarchuz
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.

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

Posted: Sat Jan 18, 2025 2:18 pm
by mbratch
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`

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

Posted: Sat Jan 18, 2025 10:48 pm
by mikemoy
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 );
}

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

Posted: Sat Jan 25, 2025 10:44 am
by egionet
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.

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

Posted: Sat Jan 25, 2025 12:30 pm
by egionet
After further testing, this issue is resolved.