Well, I’m baaaaaaack
Update to mithundotdas (and anyone else out there who is still interested):
I loaded your test program, and a little experimentation indicated that the problem manifests itself with the analogRead() of the ADC value, not the digitalWrite() of the enable signal.
So…
I wired an external voltage divider that used the XIAO BLE analog pin 3 as the ADC input and digital pin 4 as the enable signal.
Result was: Taa-Daa!
Details:
1.
I connected a 1 Megohm resistor from the + terminal of the battery to Pin A3.
2.
I connected a 560K Ohm from Pin A3 to Pin D4. (The schematic shows 510K here, but my spare parts box didn’t have a 510K, so I used something not too different that I did have.)
3.
I made some definitions in the sketch and changed the formula to take into account the changes.
I started the sketch and tested the reported values on the Serial Monitor and on my phone. (Green LED on the XIAO BLE came on, then turned Blue upon successful connection to Central, ADC voltage values were printed out every second, and the phone app could obtain the battery voltage, as expected.)
I used a generic app, LightBlue, instead of the specific app for this program. It’s somewhat awkward, but for debugging, I don’t have to worry about understanding (and debugging) some specific program other than the one I’m working on with the XIAO BLE.
Bottom line:
The existing board is useless for the application you have in mind without a little (very little, actually) external circuit . (Pardon me if you have heard this before, but don’t those Seeed guys actually test this stuff before releasing hardware and/or software and/or the integration?)
There are other discrepancies on the schematic involving various connections, and it just may happen that my selection of pins for the external voltage divider screw the pooch for other applications, but maybe it gets you past top dead center for further testing.
Final note:
Wiring the new connections on my solderless breadboard was quick and easy, but note that it tends to be pretty noisy unless you dress the leads carefully. Adding a small capacitor from the ADC input pin to ground might help a little. A simple running average in the program will make it settle down a lot. (Or so I claim.)
Here’s my modified sketch:
/**
This prorgam is written to collect IMU data from XIAO BLE Sense and send to EI Blue mobile app.
EI Blue mobile app uploads the data to Edge Impulse Studio.
Visit https://wiki.seeedstudio.com/XIAO-BLE-Sense-Bluetooth-Usage/ to setup Arduino IDE
Get EI Blue mobile app from https://github.com/just4give/ei-blue
Some mods by davekw7x
March, 18, 2022
*/
REDIRECT_STDOUT_TO(Serial);
#include <ArduinoBLE.h>
#include <LSM6DS3.h>
#include <Wire.h>
#define BLENAME "EIBLUE"
#define SERVICE_UUID "4D7D1101-EE27-40B2-836C-17505C1044D7"
#define TX_PRED_CHAR_UUID "4D7D1106-EE27-40B2-836C-17505C1044D7"
#define TX_BAT_CHAR_UUID "4D7D1107-EE27-40B2-836C-17505C1044D7"
#define RX_CHAR_UUID "4D7D1108-EE27-40B2-836C-17505C1044D7"
#define SAMPLING_RATE 50 //Hz
#define DURATION 1 //seconds
BLEService bleService(SERVICE_UUID); // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEStringCharacteristic rxCharacteristic(RX_CHAR_UUID, BLEWrite, 1024);
BLEStringCharacteristic txPredCharacteristic(TX_PRED_CHAR_UUID, BLERead | BLENotify, 1024);
BLEStringCharacteristic txBatCharacteristic(TX_BAT_CHAR_UUID, BLERead | BLENotify, 1024);
LSM6DS3 myIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
float aX, aY, aZ, gX, gY, gZ;
const float accelerationThreshold = 2.5; // threshold of significant in G's
const double vRef = 3.3; // Assumes 3.3V regulator output is ADC reference voltage
const unsigned int numReadings = 1024;
#define EXTERNAL_VBAT PIN_A3
#define EXTERNAL_VBAT_READ_ENABLE D4
const double rhik = 1000.0; // 1000K Ohms = 1 Meg to Vref
const double rlok = 560.0; // 560K Ohms to Gnd
const double dividerFactor = (rhik + rlok)/rlok; // Multiply ADC voltage by this to get Vbat
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("BLE Test compiled on " __DATE__ " at " __TIME__);
Serial.println("BLE Test compiled on " __DATE__ " at " __TIME__);
Serial.print("EXTERNAL_VBAT_READ_ENABLE = ");Serial.println(EXTERNAL_VBAT_READ_ENABLE);
Serial.print("EXTERNAL_VBAT = "); Serial.println(EXTERNAL_VBAT);
pinMode(EXTERNAL_VBAT_READ_ENABLE, OUTPUT);
digitalWrite(EXTERNAL_VBAT_READ_ENABLE,LOW);
// set LED pin to output mode
pinMode(LEDB, OUTPUT);
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, HIGH);
digitalWrite(LEDG, LOW);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName(BLENAME);
BLE.setDeviceName(BLENAME);
BLE.setAdvertisedService(bleService);
// add the characteristic to the service
bleService.addCharacteristic(txBatCharacteristic);
bleService.addCharacteristic(txPredCharacteristic);
bleService.addCharacteristic(rxCharacteristic);
// add service
BLE.addService(bleService);
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// start advertising
BLE.advertise();
Serial.println("BLE Peripheral");
if (myIMU.begin() != 0) {
Serial.println("Device error");
} else {
Serial.println("aX,aY,aZ,gX,gY,gZ");
}
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
//unsigned int adcCount = 400;
unsigned int adcCount = analogRead(EXTERNAL_VBAT);
double adcVoltage = (adcCount * vRef) / numReadings;
double vBat = adcVoltage*dividerFactor; // Voltage divider from Vbat to ADC
printf("adcCount = %3u = 0x%03X, adcVoltage = %.3fV, vBat = %.3f\n",
adcCount, adcCount, adcVoltage, vBat);
String data="";
data = String(myIMU.readFloatAccelX(), 3)+","+String(myIMU.readFloatAccelY(), 3)+","+String(myIMU.readFloatAccelZ(), 3);
txBatCharacteristic.writeValue(String(vBat));
txPredCharacteristic.writeValue(data.c_str());
delay(1000);
}
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
digitalWrite(LEDB, LOW);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDR, HIGH);
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
digitalWrite(LEDB, HIGH);
digitalWrite(LEDG, LOW);
digitalWrite(LEDR, HIGH);
}
Regards,
Dave