Read VCC

Rick Kimball
Wed Nov 04, 2015 9:05 pm
const uint8_t led = BOARD_LED_PIN;

enum { LED_ON=0, LED_OFF=1 }; // for Blue Pill .. active low led

void setup_vcc_sensor() {
adc_reg_map *regs = ADC1->regs;
regs->CR2 |= ADC_CR2_TSVREFE; // enable VREFINT and temp sensor
regs->SMPR1 = ADC_SMPR1_SMP17; // sample rate for VREFINT ADC channel
}

void setup(){
setup_vcc_sensor();
Serial1.begin(115200);
pinMode(led,OUTPUT);
Serial1.println("\r\nPrint Internal VCC Voltage");
systick_uptime_millis = 0; // really sleazy way to reset millis;
}

void loop() {
int millivolts;
uint32_t t0 = millis();

digitalWrite(led,LED_ON);
delay(50);
millivolts = 1200 * 4096 / adc_read(ADC1, 17); // ADC sample to millivolts
Serial1.print(t0); Serial1.print(" ");
Serial1.print(millivolts/1000,DEC);Serial1.print(".");Serial1.print(millivolts%1000,DEC);
Serial1.println("V");
digitalWrite(led,LED_OFF);

while((millis()-t0) < 1000); // delay 1000ms
}


ahull
Wed Nov 04, 2015 11:26 pm
Nice! Good for checking battery health and pre-empting power watchdog switching us off. I’ll need to file that in my cluttered mental archive for later. :D

tiger762
Fri Feb 05, 2016 5:25 pm
Not sure why, but I had to change the define to ADC_CR2_TSEREFE (instead of …TSVREFE)

mrburnette
Fri Feb 05, 2016 6:43 pm
ahull wrote:Nice! Good for checking battery health and pre-empting power watchdog switching us off. I’ll need to file that in my cluttered mental archive for later. :D

Rick Kimball
Fri Feb 05, 2016 10:14 pm
tiger762 wrote:Not sure why, but I had to change the define to ADC_CR2_TSEREFE (instead of …TSVREFE)

madias
Mon Apr 25, 2016 9:27 pm
So just 2 month later, I’m really happy about this code! Thanks!
Reason:
I’m building a NRF24 remote control with 18650 li-ion’s. I use 2 in parallel tested successfully with min. voltage: ~3 to max 4.2 (absolut minimum is about 2.5V but I won’t reach that limit). Before I was thinking about 2 in serial (so 6->8.4-V) but I didn’t need that voltage which results in burning battery time because of LDO’s (There are 3.3v components only).
So I got the problem with measuring voltage without having a constant 3.3V reference. On good old AVR’s there is an internal voltage reference about 1.1V, very handy for such exercises, but not on a STM32F1xx.
So with this code snipplet I got my (flexible) reference voltage, the rest are some easy calculations!

RogerClark
Mon Apr 25, 2016 9:52 pm
Matthias

Do you think we should add this function to the core ?

It should not make sketches any bigger unless it is used ( called )


madias
Mon Apr 25, 2016 10:03 pm
For sure we could put it into the core, but I believe nobody will recognize it :) (If you are not searching for it, as I have done, even on AVR the internal reference is – more or less – a “hidden” function for the arduino world)
Maybe it’s time for a “arduino -stm32duino” comparison table/sheet meanwhile a BIG todo….

RogerClark
Mon Apr 25, 2016 11:13 pm
No worries

I guess it would be better just to add it to the examples


madias
Tue Apr 26, 2016 5:47 am
+1 for that idea! (Maybe a slim version without “Serial1” and LED’s) I’ll try to extract this part out of my code, so there would be also an accurate example for reading external voltage.

RogerClark
Tue Apr 26, 2016 6:05 am
Thanks

ahull
Tue Apr 26, 2016 11:55 am
If you are looking for a voltage reference to compare with, one of these may help when calibrating.
(other suppliers are available of course).

Image

The datasheed claims 15 ppm/°C maximum, 0°C to 70°C which should be sufficiently accurate for most applications, including calibrating a cheap multimeter.


madias
Tue Apr 26, 2016 1:05 pm
Ahull: There are also smaller IC’s for that, just google for 1.25 voltage reference shunt ic and you get back thousands of results like REF1112
I believe a bigger problem is, that there is no “real” VREF input on the maple mini’s, so you need always a REF analog in pin and the need for a SW calculation (compare with another input pin). Correct me, if I’m wrong…

ahull
Tue Apr 26, 2016 1:14 pm
madias wrote:Ahull: There are also smaller IC’s for that, just google for 1.25 voltage reference shunt ic and you get back thousands of results like REF1112
I believe a bigger problem is, that there is no “real” VREF input on the maple mini’s, so you need always a REF analog in pin and the need for a SW calculation (compare with another input pin). Correct me, if I’m wrong…

mrburnette
Tue Apr 26, 2016 1:35 pm
Or…
After graduation, I managed a State University EE lab – mainly watched over undergrads and graduate students to make sure none of the equipment grew legs :lol: But, I also did some minimum calibration, etc. Research: Fun Job == lousy financial compensation.

A good overview of what calibration IS and ISNOT

One of the locked-up toys was a Standard Cell … a liquid battery in sealed glass with far too many decimal points to be comfortable. If you are an amateur chemist and want to do your own standard voltage battery, the math is pretty simple. Just be certain that you have access to chemical pure reagents and a high accuracy lab balance. Here is the link to the online calculator.

Ray


Pito
Wed Apr 27, 2016 7:02 am
The cheapest and easiest way is a venerable TL431 ($0.0something, you may find it in almost all scrap switching power sources). You need a 10k resistor in addition.
Input to +5V via 10k and Vka=Vref=2.5V, 6mV drift over temp range, good for 12bit.
tl431ref.JPG

Slammer
Wed Apr 27, 2016 10:09 am
In addition of TL431, there is also another very common reference device, the LM336-2.5

zoomx
Wed Apr 27, 2016 10:43 am
ahull wrote:
The datasheed claims 15 ppm/°C maximum, 0°C to 70°C which should be sufficiently accurate for most applications, including calibrating a cheap multimeter.

zmemw16
Sat Apr 30, 2016 12:49 am
mcp1525 2.56v
mcp1541 4.096v
aliexpress £0.18 & £0.26

srp


smithy
Mon Aug 29, 2016 1:34 am
Guess you are using the led to surge current, i have an oled connected at 3.3V so i dont need to use the led for that ?

bubulindo
Mon Sep 05, 2016 5:30 am
I was looking at this same features of the ADC (after managing to get interrupts from it and Analog Watchdog) and one can measure the internal temperature of the chip by reading channel 16 instead of channel 17. There is a formula and some parameters to calculate a number, but even then it should only be used for detecting temperature increases.

One thing that stands out is that the adc core was quite undeveloped. It does have a couple of functions that are interesting, but it seems as if the only development was done to get the equivalent of analogRead().

I managed to get the interrupts working and will be starting to look into the scan mode soon. :)


victor_pv
Tue Jan 10, 2017 10:42 pm
ahull wrote:Nice! Good for checking battery health and pre-empting power watchdog switching us off. I’ll need to file that in my cluttered mental archive for later. :D

ahull
Tue Jan 10, 2017 11:42 pm
Good find. I’ll have to have a play with that. I wonder how quickly it can react.

victor_pv
Wed Jan 11, 2017 1:35 pm
ahull wrote:Good find. I’ll have to have a play with that. I wonder how quickly it can react.

Leave a Reply

Your email address will not be published. Required fields are marked *