How to correctely use Analog input

  • Helloo
    I am new with Pycom and I need to use an analog pin to get the value of 3 identical moisture sensors and one capacitive sensor.

    I would like to measure a range of 0-1023, so to make sure to have this range, I should select bit=10.

    I do not want to be dependent of the pin outpt voltage then I think, it would be good to use a voltage reference with

    adc.vref(*, vref)

    I am not sure about several thinks. First, I found this exemple:

    adc = ADC(0)

    but I also found this

    adc = ADC(0)
    adc = ADC(bits=10)
    adc = ADC()

    then I am confused how to correctly froce the bit range from 0-1023

    I suppose all does the same if bits=10 is the default. I would prefere use


    but is it really correct, or should I consider

    adc = ADC()

    I would like to set an external voltage reference.
    Here, calibration it state, we need to configure by measuring P22. But P22 is SDA pin as well and I need to use I2C.

    So if I do that (considering, I measure 1.1V at P21)

    adc = ADC()

    The above code will not disturb the SDA pin, when I will connect an I2C sensor?
    Does my above code is correct, regarding vref?

    With Arduino , there is 3V pin, which is always at 3.3V. I can solder a wire from the 3V pin to the aref pin to have 3.3V as a reference.
    With LoPy4, is there a such pin, that we can trust the output voltage and use it a vref?

    I do not understand what does exately attenuation level

    # ADC.ATTN_11DB
    wm_1 ='P13', attn = machine.ADC.ATTN_11DB)

    IS depend of the output voltage? I heard ATTN_11DB is from 0-3V and it look like what I need...

    Thank for your clarification

  • administrators


    To answer your first question, the following two blocks of code are functionally, identical.

    adc = ADC(bits=10)
    adc = ADC()

    If you require 10-bit resolution then please specify this rather than relying on a default behaviour. This way if anything changes in the future your code will still work as expected.

    Second, with regards to the ADC calibration, this is something you only need to do once, you can output Vref onto P6 P21 or P22, take a measurement with a multimeter, then record the value in mV. After this you never need to output the reference voltage unless you want to use it for something.

    Finally, about antennuation. the ADC on the ESP32 ranges from 0 to 1v, therefore attenuation of the input signal is required to read voltages above this. The following levels are supported
    0dB = 0-1V
    2.5dB = 0-1.33V
    6db = 0-2v
    11db = 0 - 3.55V

Log in to reply

Pycom on Twitter

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.