-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
digitalRead gets stuck on 0 once analogRead is called on the pin #10927
Comments
АAnalog and digital modes are a very different thing. Different hardware inside the chip even. If you want to digital read that pin, you need to call |
Yes, that's what I did. It does not work. I am certain this is a bug. I had a typo in my code that was supposed to demonstrate the issue. I updated it. Now it's correct, the behavior described persists. Reopen the issue, please. Another example ` void loop() { Serial.println(digitalRead(3)); ` Prints: 17:29:02.002 -> 4095 |
I'm not sure you understood me void loop(){
Serial.println(analogRead(3));
delay(100);
pinMode(3, INPUT); // You need to do this after analogRead and before digitalRead to switch the pin to digital
Serial.println(digitalRead(3));
delay(100);
} |
I now understand why this happens, but this isn't a solution, it's a workaround—one that users wouldn't expect to be necessary. You mentioned that analog and digital functions are handled by different hardware. That’s true, but this is also the case for other microcontrollers, such as the Arduino Uno. However, the following code works correctly on an Uno without any extra pinMode calls:
Output on Arduino Uno (as expected):
I understand that the ESP32 is very different hardware, but once a user calls pinMode(pin, INPUT), they are fully justified in expecting that both digitalRead and analogRead will "just work" from that point onward. The fact that analogRead permanently prevents subsequent digitalReads from working correctly is:
This difference will break cross-platform code portability, which is one of the main goals of a HAL. Arduino HALs often take steps to ensure correct pin behavior. For example, the Uno’s digitalRead implementation explicitly ensures that PWM is not active before reading a pin:
The only logical conclusion is that this behavior is a bug. I can probably fix it and submit a pull request, but I need to know that you’re willing to accept it. |
I beg to differ. The fact that AVR's hardware and Arduino's original code allows such use, does not make it correct. Those are two very different IO modes. There are more things that the original Arduino allows and are not allowed on ESP32. Also things that are not correct. So to argue that one platform does not allow invalid use and that is a bug is invalid IMHO. Even if we make it possible (which we can) the result will be unpredictable, because the pin in ADC mode is connected to different hardware inside the chip and the digital value of it will be just wrong and unpredictable. |
Yes, but arguably, the end user does not care about the hardware differences. This is what the HAL should abstract away. I propose the following behavior: When you call digitalRead, it should check if the pin was previously connected to the ADC. If so, it disconnects it from the ADC and uses it as as a digital pin, reading the correct value. This makes the behavior consistent with user expectations. I should get less busy by the end of the week. If I fix it, will you accept a pull request? |
yes we will |
Board
ESP32-S3 custom board
Device Description
I bumped into this issue with my custom ESP32S3 board. The board was not detecting a button press after I mistakingly left analogRead in the loop.
Hardware Configuration
A button to pin 3, but can be replicated by having a pullup from 3 to vcc.
Version
latest master (checkout manually)
IDE Name
Arduino IDE
Operating System
Windows 10
Flash frequency
N/A
PSRAM enabled
yes
Upload speed
921600
Description
Once analogRead is called on a pin, subsequent calls to digitalWrite return zero, even if the pin is pulled high.
Sketch
Debug Message
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: