Query battery status without board

  • How can I query the battery status without board during Wipy/Lopy?
    With Board I am running like this.

        # takes battery voltage readings
        from machine import ADC #Batteriestatus
        def adc_battery():
        	adc = ADC(0)                                        # initialise adc hardware
        	adc_c = adc.channel(attn=3, pin='P16')              # create an object to sample ADC on pin 16 with attenuation of 11db (config 3)
        	adc_samples = []                                    # initialise the list
        	for count in range(100):                            # take 100 samples and append them into the list
        	adc_samples = sorted(adc_samples)                   # sort the list
        	adc_median = adc_samples[int(len(adc_samples)/2)]   # take the center list row value (median average)
        # apply the function to scale to volts
        	adc_median = adc_median * 2 / 4095 / 0.3275
        	return adc_median
        lipo_voltage = adc_battery() #Batteriewerte zuordnen
        print('Batterie = ' + str(lipo_voltage) + ' Volt') #Wert anzeigen
        # takes battery voltage readings

  • @Wolli01 This is not rebuildung everything, It's just two resistors and one capacitator. Pretty simply.

  • @robert-hh
    Thank you for your detailed description.
    But I thought it would be easier to do that without rebuilding anything.

  • @Wolli01 Assuming that with "board" you mean Expansion Board, you can rebuild the same principle with just a few external components, namely a voltage divider consisting of two resistors. The one on the expansion board uses 56k and 116k, but you can as well use identical values like 470k and 470k. Then you have to divide by 0.5 instead of 0.3275. Higher values are better, since this voltage divider will always consume power without an additional benefit. In case of 2*470k ~ 3.5µA. If you do not want that, you have add an external switch for the voltage divider (e.g. an p-channel mosfet transistor). Assuming that the output voltage of a GPIO port with small load is almost the same as the supply voltage, you can also drive the voltage divider from an GPIO output pin.
    Add a small capacitor (e.g. 10 nF) between the ADC input and GND. The noise of the ADC is lower and you might need fewer than 100 samples for your median.

    A few notes about your code:

    • list append is slow. Allocating the list once and then filling it is much faster.
      . you can use adc_samples.sort() for an in-place sort.

    P.S.: Sorry if I confused you.

Pycom on Twitter