Reading output GPIO pin logic state

x-8973
Posts: 20
Joined: Tue Apr 03, 2018 4:49 pm

Reading output GPIO pin logic state

Postby x-8973 » Mon May 21, 2018 4:06 pm

I needed to know the logical level on the pin, which is set to output. The gpio_get_level() function always returns 0, even after a gpio_set_level(GPIO_PIN, 1). Is there any way to find out the logical level on such a pin?

Code example:

Code: Select all

gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1 << GPIO_OUTPUT_IO_LED);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);

while (1)
{
	if (gpio_get_level(GPIO_OUTPUT_IO_LED)) // If pin is switched on
	{
		ESP_LOGI(__func__, "LED on");
		gpio_set_level(GPIO_OUTPUT_IO_LED, 0); // Switch pin off
	}
	else
	{
		ESP_LOGI(__func__, "LED off");
		gpio_set_level(GPIO_OUTPUT_IO_LED, 1); // Switch pin on
	}
}

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: Reading output GPIO pin logic state

Postby loboris » Mon May 21, 2018 6:31 pm

The question was asked several times already, you must set the gpio mode to GPIO_MODE_INPUT_OUTPUT (or GPIO_MODE_INPUT_OUTPUT_OD) if you want to use the gpio as output and read its state back. Or you can simply save the state in a variable after gpio_set_level().

x-8973
Posts: 20
Joined: Tue Apr 03, 2018 4:49 pm

Re: Reading output GPIO pin logic state

Postby x-8973 » Mon May 21, 2018 7:24 pm

I did a search on the forum, but did not find anything suitable. Thank you, now I will know)

flodis
Posts: 12
Joined: Mon Feb 26, 2018 5:09 am

Re: Reading output GPIO pin logic state

Postby flodis » Thu Feb 14, 2019 4:07 pm

If you are interested in the last output state of a pin you can read from either GPIO_OUT_REG (31..0) or GPIO_OUT1_REG (32..39).
To detect if port is input or output you can read the GPIO_ENABLE_REG (31..0) or GPIO_ENABLE1_REG(32..39)

If you are sure the direction is not changed outside your control a non atomic direct port access for pin 0..31 like this may work:

Code: Select all

gpio_num_t pin = (gpio_num_t)(your_pin_number_0to31 & 0x1F);
int state=0;
if (GPIO_REG_READ(GPIO_ENABLE_REG) & BIT(pin)){
	//pin is output - read the GPIO_OUT_REG register
	state = (GPIO_REG_READ(GPIO_OUT_REG)  >> pin) & 1U;
}
else
{
	//pin is input - read the GPIO_IN_REG register
	state = (GPIO_REG_READ(GPIO_IN_REG)  >> pin) & 1U;
}
Details here:
https://github.com/espressif/esp-idf/bl ... gpio_reg.h
https://docs.espressif.com/projects/esp ... /gpio.html
https://www.espressif.com/sites/default ... #section.4
Last edited by flodis on Sun Aug 29, 2021 10:45 am, edited 1 time in total.

JL1946
Posts: 11
Joined: Mon Feb 25, 2019 3:46 pm

Re: Reading output GPIO pin logic state

Postby JL1946 » Tue Aug 10, 2021 5:17 am

Many thanks to @flodis for your code snippet. It worked for me...

I implement a timer to toggle the Blue Led on a Esp32 Dev Board...
My Code is as follows:

Code: Select all

#define BLINK_GPIO 	2

void blink_blue_led() {

	gpio_num_t pin = (gpio_num_t)(BLINK_GPIO & 0x1F);
	int state = 0;

	if (GPIO_REG_READ(GPIO_ENABLE_REG) & BIT(pin))
	{
		state = (GPIO_REG_READ(GPIO_OUT_REG)  >> pin) & 1U;
		if (state == 0) {
	        	gpio_set_level(BLINK_GPIO, 1);
		} else {
			gpio_set_level(BLINK_GPIO, 0);
		}
	}
}

ayham24
Posts: 1
Joined: Tue Sep 07, 2021 11:23 pm

Re: Reading output GPIO pin logic state

Postby ayham24 » Tue Sep 07, 2021 11:26 pm

Hey Could you please somehow send me your whole package of the program if it's ok? cuz i'm trying since days to do it but i don't know what's wrong here :( i would be really thankful

davidhbrown
Posts: 1
Joined: Mon Jan 31, 2022 4:42 am

Re: Reading output GPIO pin logic state

Postby davidhbrown » Sat Oct 22, 2022 3:19 am

flodis wrote:
Thu Feb 14, 2019 4:07 pm
... read from either GPIO_OUT_REG (31..0) or GPIO_OUT1_REG (32..39)....
Emphasis added as I'd missed that on first reading and it turned out I was using GPIOs 32 and 33 (hidden behind #defined constants so I'd forgotten) :oops: Thank you for the example; it's working great.

Who is online

Users browsing this forum: No registered users and 136 guests