Read VCC

Post your cool example code here.
User avatar
Rick Kimball
Posts: 871
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Read VCC

Post by Rick Kimball » Wed Nov 04, 2015 9:05 pm

Code: Select all

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
}
Sample output:
Print Internal VCC Voltage
0 3.301V
1000 3.305V
2000 3.309V
3000 3.309V
4000 3.305V
5000 3.305V
6000 3.305V
7000 3.305V
8000 3.305V
9000 3.305V
10000 3.305V
-rick

User avatar
ahull
Posts: 1518
Joined: Mon Apr 27, 2015 11:04 pm
Location: Sunny Scotland
Contact:

Re: Read VCC

Post by 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
- Andy Hull -

tiger762
Posts: 22
Joined: Tue Jan 26, 2016 11:40 pm

Re: Read VCC

Post by 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)

User avatar
mrburnette
Posts: 1783
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Read VCC

Post by 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
I've long passed the mental-reference period, rather these days I just print to PDF and save 'em in a folder /Arduino/online where I can search them later at my convenience. I even have the URL on the bottom of the page in case I need to go back and do a little pre/post article reading.


Ray

User avatar
Rick Kimball
Posts: 871
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Read VCC

Post by 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)
I guess I fixed this typo in my personal repo as I originally tested it with the STM32 Standard Peripheral Library. I thought I had seen someone had pushed a change in the main github repo. I guess not.

Here is the change I made to my personal repo to match the docs in the reference manual:
https://github.com/RickKimball/stm32dui ... 9fcc7b3211
user reference manual wrote:...
11.10 Temperature sensor
The temperature sensor can be used to measure the ambient temperature (TA) of the
device.
The temperature sensor is internally connected to the ADCx_IN16 input channel which is
used to convert the sensor output voltage into a digital value. The recommended sampling
time for the temperature sensor is 17.1 µs.
The block diagram of the temperature sensor is shown in Figure 39.
When not in use, this sensor can be put in power down mode.
Note: The TSVREFE bit must be set to enable both internal channels: ADCx_IN16 (temperature
sensor) and ADCx_IN17 (VREFINT) conversion.
The temperature sensor output voltage changes linearly with temperature. The offset of this
line varies from chip to chip due to process variation (up to 45 °C from one chip to another).
The internal temperature sensor is more suited to applications that detect temperature
variations instead of absolute temperatures. ...
-rick
-rick

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Read VCC

Post by 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!

User avatar
RogerClark
Posts: 5975
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Read VCC

Post by 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
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Read VCC

Post by 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....

User avatar
RogerClark
Posts: 5975
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Read VCC

Post by RogerClark » Mon Apr 25, 2016 11:13 pm

No worries

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

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: Read VCC

Post by 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests