Skip to content

Commit 5217449

Browse files
author
microbuilder
committed
Added VBAT ADC example
1 parent 84553d5 commit 5217449

File tree

1 file changed

+44
-0
lines changed
  • libraries/AdafruitFeather52Examples/Hardware/adc_vbat

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#define VBAT_PIN (A7)
2+
#define VBAT_MV_PER_LSB (0.73242188F) // 3.0V ADC range and 12-bit ADC resolution = 3000mV/4096
3+
#define VBAT_DIVIDER (0.71275837F) // 2M + 0.806M voltage divider on VBAT = (2M / (0.806M + 2M))
4+
#define VBAT_DIVIDER_COMP (1.403F) // Compensation factor for the VBAT divider
5+
6+
float readVBAT(void) {
7+
float raw;
8+
9+
// Set the analog reference to 3.0V (default = 3.6V)
10+
analogReference(AR_INTERNAL_3_0);
11+
12+
// Set the resolution to 12-bit (0..4095)
13+
analogReadResolution(12); // Can be 8, 10, 12 or 14
14+
15+
// Let the ADC settle
16+
delay(1);
17+
18+
// Get the raw 12-bit, 0..3000mV ADC value
19+
raw = analogRead(VBAT_PIN);
20+
21+
// Set the ADC back to the default settings
22+
analogReference(AR_INTERNAL);
23+
analogReadResolution(10);
24+
25+
// Convert the raw value to mv
26+
// ADC range is 0..3000mV and resolution is 12-bit (0..4095)
27+
// VBAT voltage divider is 2M + 0.806M, which needs to be added back
28+
return raw * VBAT_MV_PER_LSB * VBAT_DIVIDER_COMP;
29+
}
30+
31+
void setup() {
32+
Serial.begin(115200);
33+
}
34+
35+
void loop() {
36+
// Get a fresh ADC value
37+
float vbat_val = readVBAT();
38+
39+
// Display the results
40+
Serial.print(vbat_val);
41+
Serial.println(" mV");
42+
43+
delay(1000);
44+
}

0 commit comments

Comments
 (0)