Skip to content

esp32: ADC driver update to the new esp_adc. #16630

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

Merged
merged 1 commit into from
Jun 5, 2025

Conversation

purewack
Copy link
Contributor

@purewack purewack commented Jan 23, 2025

Summary

This is a complementary PR to #16521, focusing only on ADC, updating it to fix an issue with the RISCV adc function causing a conflict of drivers mentioned in #16469

There are several errata notes about not being able to change the bit-width of the ADCs certain chips.
The only chip that can switch resolution to a lower one is the normal esp32.
esp32 C2 & S3 are stuck at 12 bits, while S2 is at 13 bits.
On the S2, you can change the resolution, but it has no effect on the resolution, rather, it prevents attenuation from working at all!
I have set the resolution to the be maximum possible for each SoC, with the esp32 being the only one not throwing errors when trying to set the bit-width to 9,10,11 or 12 bits using ADC.width(bits)

Testing

I have built and flashed the following variants: Generic, S2, S3, C3

The ADC behaves correctly as expected, with readings following the position of a 10K potentiometer.
Attenuation works too where values above the maximum range get clipped and readings stay at the maximum attenuated level:

  • ~1v with 0db attenuation
  • ~3.1v using 11db attenuation

@dpgeorge dpgeorge added this to the release-1.26.0 milestone Feb 27, 2025
Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one small note but this looks really nice, thanks for submitting it @purewack!

@purewack purewack force-pushed the esp32/esp_adc branch 2 times, most recently from 53c0c52 to d13d11f Compare May 28, 2025 08:30
@purewack purewack force-pushed the esp32/esp_adc branch 2 times, most recently from 75c404f to c52f5d6 Compare May 29, 2025 13:18
This commit updates the ADC to use the new driver `esp_adc/adc_oneshot.h`.

There are several errata notes about not being able to change the bit-width
of the ADCs certain chips.  The only chip that can switch resolution to a
lower one is the normal ESP32.  ESP32 C2 and S3 are stuck at 12 bits, while
S2 is at 13 bits.

On the S2, you can change the resolution, but it has no effect on the
resolution, rather, it prevents attenuation from working at all!

The resolution is set to the maximum possible for each SoC, with the ESP32
being the only one not throwing errors when trying to set the bit-width to
9, 10, 11 or 12 bits using `ADC.width(bits)`.

Signed-off-by: Damian Nowacki (purewack) bobimaster15@gmail.com
Copy link
Member

@dpgeorge dpgeorge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good now, a nice improvement, thank you!

Tested with ESP32_GENERIC firmware. Deinit'ing and then doing a read now raises EPERM.

@dpgeorge dpgeorge merged commit 5f058e9 into micropython:master Jun 5, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants