RTC Voltage/Power Good check


#1

Does anyone know if there is a way to check if there is a voltage on the RTC?

Right now I’m seeing devices with a coin cell battery installed for the RTC but there is some flux between the battery and the RTC. This makes the device not maintain the RTC. To check it requires disassembly.

Any software check would be awesome.


#2

What device?
Electrons do have a 0Ohm resistor between 3v3 and VBAT, Photons don’t.


#3

Its on a P1


#4

If VBAT_MICRO is powerd (min. 1.65V) the RTC is powered even with no voltage at 3v3.

I assume you have also applied 3.3V to all required VBAT pins on the P1


#5

Here is an application note:

on page 16.
Many ST serial real-time clocks include a battery monitor function which sets the BL bit
when the battery is low. The basic functionality is to periodically compare the battery to the
internal 2.5 V reference and set the BL bit whenever the battery voltage is less than 2.5 V.


#6

The VBAT_MICRO (pin 38) is connected directly to a CR1220 Battery providing the voltage. That is the only connection to the Vbat


#7

#8

Yes, it’s connected to ADC1_IN18 internally. I probably have some code to read that somewhere.


#9

That would be AWESOME!


#10

This looks like the application note

Seems thats its also possible to get the temperaure?
image


#11

Done. Here are the instructions and Github:

And it’s photon-vbat in the community libraries.


Class the read the value of the VBAT and the temperature sensor on the Photon or P1.

Also works on the Electron, however VBAT is generally connected to 3V3 so it’s always 3.3V.

This only works on the STM32F2xx devices including the Photon, Electron, and E Series.

It does not work on the Spark Core or Mesh devices as it directly accesses the hardware.

Example usage:

#include "photon-vbat.h"

// Parameters are:
// A7 = An unused pin that you are not normally analogReading from that's safe to pass to analogRead
// 6.54 = VBAT calibration constant (optional)
// 943 = temperature calibration constant (optional)
PhotonVBAT photonVBAT(A7, 6.54, 943);

void setup() {
	Serial.begin(9600);
}

void loop() {
	// Read battery voltage (in volts)
	float vbat = photonVBAT.readVBAT();

	// Read processor temperature (in degrees C)
	float tempC = photonVBAT.readTempC();

	float tempF = tempC * 9.0 / 5.0 + 32.0;

	Serial.printlnf("vbat=%f tempC=%f tempF=%f A0=%d", vbat, tempC, tempF, analogRead(A0));
	delay(1000);
}

Sample output:

vbat=2.925832 tempC=48.200001 tempF=118.760002 A0=1
vbat=2.925832 tempC=48.599998 tempF=119.479996 A0=1
vbat=2.925832 tempC=47.799999 tempF=118.040001 A0=0
vbat=2.925832 tempC=49.799999 tempF=121.639999 A0=0

#12

Thats so cool!.

Unfortanely, I dont have any open/spare A/D pins available. Would it be possilbe to switch between the two?


#13

It doesn’t really need to be a spare pin. It can actually be almost any A pin. The only constraint is that it can’t be a pin that’s a 5V input, and it can’t be the pin you plan to analogRead() from right after you read the battery (or temperature).

In other words, it can be WKP (A7) if you’re using it for waking up or GPIO (as long as you’re not using it as a 5V tolerant input).

Or any A pin you’re using as a GPIO input (as long as you’re not feeding it 5V). This also includes P1S0-P1S3 on the P1.

It can also be any A pin you use as a GPIO output. In retrospect, that probably won’t work quite right.

It can be an analog input you are using in your own code… however you need to read any other pin first. So say you read A1 and A2 in your app. You can pass A2 as long as you always read A1 first. It’s working around an annoying thing in system firmware where if you read the same pin you last read the ADC is not completely reset and you’ll get wrong readings.

(I can’t save and restore the settings, because there’s no way to read them out first.)


#14

Ok thanks for the extra info, do you think the P1spare pins could work too like below?

P1S0 12-bit Analog-to-Digital (A/D) inputs (0-4095), and also can be used as a digital GPIO or PWM[1].
P1S1 12-bit Analog-to-Digital (A/D) inputs (0-4095), and also can be used as a digital GPIO or PWM[1].
P1S2 12-bit Analog-to-Digital (A/D) inputs (0-4095), and also can be used as a digital GPIO.
P1S3 12-bit Analog-to-Digital (A/D) inputs (0-4095), and also can be used as a digital GPIO.

#15

I just tried it with pin P1S0 the code seems work but hijacks the pins other function.

  1. pinMode(P1S0, OUTPUT); //stepper motor pin Used as digital Output
  2. Use P1S0 as a digital output and works
  3. vbat = photonVBAT.readVBAT(); GETs A GOOD READING
  4. use P1S0 as digital output but does not work

Do you have any thoughts or am I missing something?


#16

I just tried the below: by re-defining the pinMode and its now working as expected.

vbat = photonVBAT.readVBAT();
pinMode(P1S0, OUTPUT); //stepper mortor pin


#17

@rickkas7 do you think reading the bvat can drain the battery?

Over the coarse of 11 hours of reading the bvat every 400ms the battery voltage has dropped from 3.05 to 2.98.

EDIT: It looks like enabling the ADC does cause power consumption of the battery. Is there a way to turn off the bridge divider once turned on?

From:

3.1.2 Special firmware configuration
ADC application
● The ADC can be also a source of current draw during the Stop mode, unless it is
disabled before entering it. To disable it, write the ADON bit in the ADC_CR2 register to
0.
● In run mode, ADC can be configured in power-down mode by clearing the ADON bit. In
this mode the ADC consumes almost no power (only a few µA).
● In the application controlling the battery threshold through ADC_IN18 input channel,
when setting VBATE bit in ADC_CCR register in run mode, there is an extra current
consumption seen in Vbat pin. As a consequence, this may impact the battery life time.
In fact, as the VBAT voltage could be higher than VDDA, to ensure the correct
operation of the ADC, the VBAT pin is internally connected to a bridge divider by 2. This
bridge is automatically enabled when VBATE is set, to connect VBAT/2 to the
ADC1_IN18 input channel. To prevent any unwanted consumption on the battery, we
recommend enabling the bridge divider only when needed, for ADC conversion.


#18

I updated the photon-vbat library to only turn on vbat detection right before reading then turn it off after. This should eliminate the battery drain issue.


#19

Thanks! I’ll test it out again