Hey community!
I can’t get a voltage reading from PIN_VBAT
in PlatformIO no matter what. However it’s working just fine in the Arduino IDE… I already isolated the problem and created two minimal repro projects.
I’m using the XIAO nrf52840 v1.0
Working Arduino project:
ReadBattery.ino
#include "Adafruit_TinyUSB.h"
#define VREF 3.6
#define ADC_MAX 4096
void setup() {
analogReadResolution(ADC_RESOLUTION);
pinMode(PIN_VBAT, INPUT);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, LOW);
}
void loop() {
Serial.print("V Bat: ");
Serial.println(getBatteryVoltage());
}
float getBatteryVoltage() {
unsigned int adcCount = analogRead(PIN_VBAT);
float adcVoltage = adcCount * VREF / ADC_MAX;
return adcVoltage * 1510 / 510;
}
Non-working PlatformIO project:
platformio.ini
[env]
platform = https://github.com/maxgerhardt/platform-nordicnrf52
[env:xiaoble]
board = xiaoble
framework = arduino
monitor_speed = 115200
main.cpp
#include <Arduino.h>
#define VBAT_ENABLE 14 <-- not auto-defined in PlatformIO
#define VREF 3.6
#define ADC_MAX 4096
float getBatteryVoltage();
void setup() {
analogReadResolution(ADC_RESOLUTION);
pinMode(PIN_VBAT, INPUT);
pinMode(VBAT_ENABLE, OUTPUT);
digitalWrite(VBAT_ENABLE, LOW);
}
void loop() {
Serial.print("V Bat: ");
Serial.println(getBatteryVoltage());
}
float getBatteryVoltage() {
unsigned int adcCount = analogRead(PIN_VBAT);
float adcVoltage = adcCount * VREF / ADC_MAX;
return adcVoltage * 1510 / 510;
}
As you can see there is no native support for the XIAO nrf52840 by PlatformIO, so I have to use the community implementation.
The issue arose from a ported Arduino project, everything ported fine except the battery monitoring. In the analogRead(PIN_VBAT)
I only get 4095 as a result no matter what.
As I checked out the schematics I also saw that PIN_VBAT is marked as P0.31_AIN7_BAT which indicates pin 31, but in Arduino and PlatformIO PIN_VBAT is defined as 32.
I only find that strange and don’t understand it, but it still works on Arduino, so thats fine I guess.
I don’t have the expert knowladge to determine which parts may cause this behaviour on PlatformIO, all I did to this point is to compare all #defines if they are the same and I can say that they all match.
Hope someone can help me, since I already wasted my whole Sunday on this!
Greetings from Germany,
Nils Ole