Last active
August 29, 2015 14:18
-
-
Save ersatzavian/92c92989e0974a185c00 to your computer and use it in GitHub Desktop.
Reading VBAT on imp002 EVB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| vbat_sns <- hardware.pinB; | |
| vbat_sns_en <- hardware.pin2; | |
| vbat_sns.configure(ANALOG_IN); | |
| vbat_sns_en.configure(DIGITAL_OUT, 0); | |
| function getVbat() { | |
| // enable the monitoring circuit | |
| vbat_sns_en.write(1); | |
| // average 10 readings to reduce noise | |
| local vpin = 0; | |
| for (local i = 0; i < 10; i++) { | |
| vpin += vbat_sns.read(); | |
| } | |
| vpin = (vpin * 1.0) / 10.0; | |
| // disable the monitoring circuit to save power | |
| vbat_sns_en.write(0); | |
| // convert ADC output to voltage | |
| vpin = (vpin / 65535.0) * hardware.voltage(); | |
| // calculate VBAT from Vpin | |
| local vbat = vpin * (6.9 / 4.7); | |
| return vbat; | |
| } | |
| server.log(format("Battery Voltage: %0.2fV", getVbat())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment