8000 ESP32 taking pin back from adc to gpio requires machine.reset() · Issue #5771 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content
ESP32 taking pin back from adc to gpio requires machine.reset() #5771
Closed
@gmos

Description

@gmos

When you use a Pin for ADC it can never be a gpio pin until a hard reset is done.
Example:

p = Pin(36, Pin.IN)
p()  # Yields 1 since it is conncted to 3.3V via external pull-up
a = ADC(Pin(36))
a.read()  # Yields 4095 on standard attenuation
del a
p = Pin(36, Pin.IN)
p()  # Yields 0 but should be 1

Cause: When the pin is taken for the ADC the primary mux is set to connect to the RTC_ADC. Subsequent re-initialisation with Pin() or Pin.init does not switch the mux back. This normally works because after reboot, by default the primary mux connects to the GPIO mux.
As a solution we could reset the pin before selecting in machine_pin_obj_init_helper() in machine_pin.c. This seems to work well:

    // configure the pin for gpio
    gpio_reset_pin(self->id);   # restores defaults, similar to reboot
    gpio_pad_select_gpio(self->id);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0