# Howto analogRead accumulator voltage on Teensy 4.1 w. Teensy Feather Adapter

• 06-25-2020, 08:21 PM
mstiller
Howto analogRead accumulator voltage on Teensy 4.1 w. Teensy Feather Adapter
Hi,

i am trying to read the voltage of a lipo accumulator attached to a Teensy Feather Adapter on a Teensy 4.1.

Reading works, but i have no clue how to calculate the voltage from the values read.

What i do:

Code:

```setup() { } uint32_t last = 0; void loop() {    float vbat = analogRead(A7);   if (millis() > last + 30000) {     Serial.println(vbat);     last = millis();   } }```
Raw values are 686.00...

According the the Schematic there is a voltage divider with 2x 100k resistors attached to Teensy Analog 7. This seems to be correct as different accus display different values and over time (on the lower valued one)
the value rises.

But how is that correct value calculated? :confused:
• 06-25-2020, 09:36 PM
defragster
Is that a freshly charged battery at 4.42 V ?

assuming 10 bit analog res - full scale on Teeny is 3.3 V would read 1023

Since the battery will sit over 3.3V Full or empty the divider will drop the voltage to the read pin by half to with the usably safe range of the Teensy.

So the 686 value as a percentage of 3.3 V is 686/1023, so multiple by 3.3V gives a value with regard to the 3.3 V reference voltage in the Teensy. Because of the voltage divider doubling that should arrive at the battery measured voltage.
• 06-25-2020, 09:53 PM
mstiller

It arrived today from the store and is a big one (6600mAh). I don't think it is fully charged.

If i understand you correctly i have to double the raw value, divide by 1023 and multiply by 3.3 (reference voltage).

This gives me: raw: 688.000000, vbat: 4.438710.

Isn't the 4.4V value too high? It is labeled 3.7V 6600mAh. I know from experience that they actually have values > 4V if they are "full" but i would expect like max 4.21?
• 06-25-2020, 10:02 PM
defragster
Perhaps you can use a DVM to read the presented bat voltage directly? Also read the 3.3V and that A7 pin voltage to make sure the numbers all agree with expectations.

That math seems right AFAIK - 4.4V is high - especially as shipped. Fresh off charge they can be ~4.2V.
• 06-25-2020, 10:18 PM
mstiller
Good idea, so A7 reads 1.75V and the 3.3Pin reads 3.28V.

Then i had the idea that maybe some internal pull-something is enabled and did a pinMode(A7, INPUT) before.

Now the values read: raw: 570.000000, vbat: 3.677419, which i think is quite ok.

This is the code now:

Code:

```void loop() {    float raw = analogRead(A7);   float vbat = (raw * 2) / 1023;   vbat *= 3.3;   if (millis() > last) {     Serial.printf("raw: %f, vbat: %f\r\n", raw, vbat);     last = millis() + 30000;;   } }```
Thanks for your help, i think it works now as expected. The Adapter charges with max 500mAh, so it will take a while until full.
• 06-25-2020, 10:36 PM
defragster
Glad it worked - doing math in public can go so wrong :) 3.67 is way less than 4.42

Just powering up some new NRF feather units so will be looking for this - after posting ... went back to that webpage and right there was their dense code that comes to that same result - with 3 or so #defines for misdirection. Funny LiPo here is 400 mAh and charges at 100mAh - and factory neopixel color cycle running after charging now about 40 hours - measured draw at 10 mAh - so should call it good and make sure it doesn't take it too low as not read about shutoff.