-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESP32 C3 digitalRead() returns always 0, should return the pin level #5552
Comments
This is expected behavior as the pin is configured as output only. Reading an output pin will always return zero. The pin must be configured as INPUT_OUTPUT for the read to work. Unfortunately there is no Arduino API to configure a pin as INPUT_OUTPUT, you can however call:
where PIN is the target GPIO number. |
Hi Mike, thank you for that advise, your workaround works. Again, thank you for your advise. |
There is simple explanation. ESP32 C3 just does not support it.
Like you said, it works on S2 and does not work on C3, and it cant be changed or fixed in arduino as long as it wont be "fixed" in esp-idf. This also may be C3 hardware design and cant be fixed. |
Again, I understand that it is not working. Ff it can be fixed in the IDF, I would recommend to do so. |
Then you should report it in esp-idf, not here, if you understand it. |
But here is the place for the ESP32-C3-Arduino, and it is an Arduino behaviour. I will have a look into the IDF source to check out what is behind. |
I did show you example code with plain esp-idf API, it is not arduino, which shows the same output: |
This CAN work in arduino-esp32 but the support code for it does not exist yet. A new pin mode will be necessary (INPUT_OUTPUT or similar name) which is a combination of the current INPUT and OUTPUT modes. The underlying ESP-IDF code does not support reading the state of an OUTPUT only pin and that is why the current arduino code does not work as you might expect. As for why this works for ESP32 and ESP32-S2 but not ESP32-C3 that is due to the ESP32-C3 code using ESP-IDF APIs whereas the others are using a local implementation which does not work for the ESP32-C3 (and long term should be removed entirely in favor of ESP-IDF APIs). |
@atanisoft this is esp-idf API, which works on esp32 with pin OUTPUT only: |
@chegewara it shouldn't work even in ESP-IDF unless the pin mode is INPUT_OUTPUT since the HAL code references the hardware IN register which is not updated as part of OUTPUT only pin manipulation. |
Maybe it should not work, but it works. Either way it may be consider bug and should be reported in esp-idf, not here. To be honest until now ive been thinking the same way, that reading OUTPUT pin should not be possible and i never before even did try it, since there is INPUT_OUTPUT option. |
A common technic ist digitalWrite(!digitalWrite()) to invert the current output. |
I think the expected arduino behavior here is that you can digitalRead an output pin. That probably means the implementation is INPUT_OUPUT once the core is fully refactored on the idf hal. |
The ESP32-C3 uses ESP-IDF APIs but ESP32 and ESP32-S2 use direct register access. This issue should be used to standardize on ESP-IDF APIs AND default OUTPUT mode to be INPUT_OUTPUT. |
@atanisoft are you sure? As far as i understand C3 is not using esp-idf, but i may be wrong: |
@chegewara Certain... https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-gpio.c#L41 This if/elif block enters this line when using the C3. |
ok, i did some more testing there is something odd with |
I did some testing once again, it is only a C3 problem, the S2 or the regular ESP32 and other MCUs don't show this behaviour.
|
Hello @VojtechBartoska the problem still exists with the 2.0.3-RC1 release |
Thanks for testing, adding it to our Roadmap and we will investigate it more. |
@helmut64 That is expected behavior for a GPIO that has been configured as @VojtechBartoska |
Thanks @atanisoft for your extensive explanation. @P-R-O-C-H-Y Can you please take a look as you refactored GPIO? |
@P-R-O-C-H-Y Change
0x3 which maps to https://github.com/espressif/esp-idf/blob/e3c0229d2ec9bfd0203a3cab18d6642798457578/components/hal/include/hal/gpio_types.h#L376. Perhaps it would make more sense to use the IDF type values directly?
|
Dear @atanisoft I understand, however in Arduino ESP32, Atmel D21 and all others Arduino's the GPIO OUTPUT reading is working. I watched that this compatibility problem exists only with the C3 |
That is due to C3 being the only ESP32 variant that uses the IDF APIs so far. It is not a bug in so far as how the pin is configured, it is strictly OUTPUT only and IDF does not track the state for OUTPUT only pins. It must be configured as INPUT_OUTPUT for IDF APIs to track and return the state. |
@atanisoft Do you think its best to change OUTPUT to 0x03 (to be input_output) or add INPUT_OUTPUT option separately? |
As Arduino pinMode has no INPUT_OUTPUT, I believe when pinMode OUTPUT is specified it should handle it as GPIO_MODE_INPUT_OUTPUT. This would be compatible with other existing Arduino MCUs. @P-R-O-C-H-Y |
@helmut64 I will make the change, it makes sense to be as close as we can to Arduino official API. Will do some tests before placing PR to make sure, changing that doesn't break anything else :) |
Hi, I recently updated from 2.0.2 to 2.0.3, and a |
When I do a digitalWrite() to set the GPIO pin (e.g. 2) to HIGH, a digitalRead(2) returns always 0
It should return the pin level as it does not the regular ESP32 or the S2
Arduino-esp32 version: 2.0.0-alpha1
Regards Helmut
The text was updated successfully, but these errors were encountered: