WiPy ADC resolution



  • It seems I'm unable to vary the ADC resolution from 12 bits. The doco says 9, 10, 11 and 12 bits are valid.

    import pycom
    import machine
    
    pycom.heartbeat(False)
    
    adc = machine.ADC()
    apin = adc.channel(pin='P13',attn=adc.ATTN_11DB)
    adc.init(bits=11)
    
    print ("Count:",apin(),"  Voltage:",apin.voltage())
    

    The above code gives:

    Traceback (most recent call last):
      File "<stdin>", line 9, in <module>
    ValueError: invalid argument(s) value
    >
    

    The only value that doesn't give the error is "bits=12".

    Is my syntax correct?



  • @robert-hh Ok thanks for that. Glad to perhaps make a small contribution to an improvement to the code :-)



  • @Jonno I raised an issue about adc.init() which also contains the fixed function code. https://github.com/pycom/pycom-micropython-sigfox/issues/324
    I could also make a PR, starting the fight with the git dragon.



  • @robert-hh thanks for that. machine.ADC(bits=11) is a better way of defining the resolution anyway.



  • @Jonno: There is indeed an inconsistency in the code. While the adc instance creating call accepts all values for bits between 9 and 12, the adc.init() call only accepts 12. So you can use for instance:
    adc = machine.ADC(bits=11)

    Edit: Looking at the implementation of adc.init(), it looks like the assignment of the bits value to pyb_adc_obj_t->width is missing. Then, the default value 12 is used. So it may be wrong anyhow.


Log in to reply
 

Pycom on Twitter