Read input voltage via code?

  • Hi! I have a GPy with the Makr board and a battery connected on the battery +/- 2-pin connector on the board. I want to be able to read how much input voltage I am getting from the battery on the GPy so that I can notify myself in advance before the battery runs out of power.

    So, I am wondering if there is there any way I can read how much power the PyCom are receiving from the battery connector in my code? If not, is there anything else I can do?

  • @robert-hh Thanks! :)

  • @techykermit If the BAT jumper of the expansion board (pymakr, as you call it) is on place, the battery is connected by a voltage divider to P16. You can get it e.g. with this little script:

    import machine
    adc = machine.ADC()             # create an ADC object
    apin ='P16', attn=adc.ATTN_11DB)   # create an analog pin on P16
    val = apin.voltage()                    # read the value from ADC
    print(val * 2)    # compensate for the voltage divider

    Please note, that the voltage only allows to estimate the charge. For a LiPo battery, you can assume the battery as almost empty when the voltage is below 3.3V.

Log in to reply

Pycom on Twitter