Can you get the supply voltage?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
mischko
Posts: 18
Joined: Tue Feb 03, 2015 12:32 am

Can you get the supply voltage?

Post by mischko » Thu Feb 05, 2015 10:04 pm

How do you get the PyBoard supply voltage so you can watch battery level?

Damien
Site Admin
Posts: 634
Joined: Mon Dec 09, 2013 5:02 pm

Post by Damien » Fri Feb 06, 2015 12:28 am

You could take the VIN pin and wire it into a simple voltage divider, with the output going to an ADC pin. This voltage level will drop as the battery is drained, but the 3.3v reference will stay the same, so you can measure the battery level. For more accuracy I think there is an internal voltage reference on one if the ADC ports.

mischko
Posts: 18
Joined: Tue Feb 03, 2015 12:32 am

Re: Can you get the supply voltage?

Post by mischko » Fri Feb 06, 2015 12:35 am

Cool! Is there documentation on this reference voltage via ADC somewhere?

User avatar
dhylands
Posts: 3382
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Can you get the supply voltage?

Post by dhylands » Fri Feb 06, 2015 5:56 am

mischko wrote:Cool! Is there documentation on this reference voltage via ADC somewhere?
There is VREFINT, mentioned in Chapter 13 of the datasheet:
http://www.st.com/web/en/resource/techn ... 031020.pdf (Page 389)

mischko
Posts: 18
Joined: Tue Feb 03, 2015 12:32 am

Re: Can you get the supply voltage?

Post by mischko » Fri Feb 06, 2015 5:37 pm

How do I get at that from Python?
I'm new to microcontrollers and hardware in general, having been a Python/Javascript/Database developer for years.
This is all swimming in the deep end for me.

Thanks,
Scott

User avatar
dhylands
Posts: 3382
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Can you get the supply voltage?

Post by dhylands » Fri Feb 06, 2015 5:58 pm

mischko wrote:How do I get at that from Python?
I'm new to microcontrollers and hardware in general, having been a Python/Javascript/Database developer for years.
This is all swimming in the deep end for me.

Thanks,
Scott
Unfortunately, if there is an issue with the interrupts or DMA, then I think this will need to be addressed from C.

mischko
Posts: 18
Joined: Tue Feb 03, 2015 12:32 am

Re: Can you get the supply voltage?

Post by mischko » Fri Feb 06, 2015 6:37 pm

Dave,
I'm not clear what you mean by "an issue with the interrupts or DMA".

Do you mean that the VREFINT is only accessible using interrupts or DMA and therefore it needs to be done in C?

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Can you get the supply voltage?

Post by blmorris » Fri Feb 06, 2015 6:46 pm

mischko wrote:How do I get at that from Python?
I'm new to microcontrollers and hardware in general, having been a Python/Javascript/Database developer for years.
This is all swimming in the deep end for me.

Thanks,
Scott
In addition to the 'pyb.ADC' class there is also the 'pyb.ADCAll' class which gives you access to a few additional features of the ADC hardware:

Code: Select all

>>> dir(pyb.ADCAll)
['read_channel', 'read_core_temp', 'read_core_vbat', 'read_core_vref']
If you want to measure the voltage from VREFINT you can do the following:

Code: Select all

>>> adc = pyb.ADCAll(12)  # Initialize with 12-bit resolution
>>> adc.read_core_vref()
2.410547
Or an average from many readings:

Code: Select all

vr = 0
for i in range(1000):
    vr += adc.read_core_vref()
    pyb.delay(1)

>>> vr / 1000
2.412333
The reading that you get is actually the internal reference voltage calculated by measuring against the 3.3V supply voltage, which is a little odd because the internal reference voltage may in fact be more stable than the 3.3V supply.
Also, there appears to be a bug in 'adc.c'; according to section 5.3.23 of the hardware data sheet the internal reference voltage is 1.21V +/- 0.03V over the -40C - +105C temp range. (This is standard for a silicon band-gap reference.) There is a stray factor of 2 - I found it in the source, it was incorrectly copied from the code that calculates VBAT. (VBAT can be higher than 3.3V, so the ADC hardware measures it through a 2:1 voltage divider.)
I'll put together the pull request.
-Bryan
(small grammar and code edit)
Last edited by blmorris on Fri Feb 06, 2015 8:05 pm, edited 1 time in total.

mischko
Posts: 18
Joined: Tue Feb 03, 2015 12:32 am

Re: Can you get the supply voltage?

Post by mischko » Fri Feb 06, 2015 7:01 pm

Big thanks for the details!
I'm looking forward to getting the new firmware with your fix in it!

Damien
Site Admin
Posts: 634
Joined: Mon Dec 09, 2013 5:02 pm

Re: Can you get the supply voltage?

Post by Damien » Fri Feb 06, 2015 9:57 pm

read_core_vref function is now fixed (wait until 00:30 GMT for new firmware to be available at micropython.org/download).

You will still need to use a voltage divider to bring the VIN voltage down to below 3.3v for input into one of the ADCs. It's simple to do: just 2 large resistors of the same value, say 47kOhm each, in series from VIN to GND, with the connection to the ADC from the middle of the 2 resistors. Then when you measure VIN it will be half of what it actually is.

Post Reply