Skip to content

First calibration

Pavel S edited this page May 29, 2021 · 1 revision

Run following code in order to prepare & calibrate the battery gauge. Apply precisely 3.6V to battery clips. (or adjust this values below).

Really important to power the board via programming header during calibration (no current can flow through battery). Be careful! the board runs at 2.5V so the programmer has to have a same voltage levels or the regulator (IC5) must not be populated when powering/programming at 3.3V.

#include "mbed.h"
#include "BQ35100.h"
#include "SHTC3.h"

I2C i2c(I2C_SDA, I2C_SCL);
BQ35100 gauge(GAUGE_ENABLE_PIN);
SHTC3 sht;

int main() {
    if (sht.init(&i2c)) {
        debug("SHTC3 init OK\n");

    } else {
        debug("SHTC3 init failed\n");
        return 0;
    }

    uint16_t real_temp;

    if (sht.read(&real_temp, nullptr)) {
        float temp = sht.toCelsius(real_temp) * 100;
        temp += 27315; // to 0.01 Kelvin
        temp /= 10; // to 0.1 Kelvin

        real_temp = (uint16_t)temp;

        debug("SHTC3 temperature: %uK\n", real_temp);

    } else {
        debug("SHTC3 temperature read failed\n");
        return 0;
    }

    if (gauge.init(&i2c)) {
        debug("Init OK\n");

    } else {
        debug("Could not init the gauge\n");
        return 0;
    }

    if (gauge.setSecurityMode(BQ35100::SECURITY_UNSEALED)) {
        debug("Device unsealed\n");

    } else {
        debug("Unseal failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.setGaugeMode(BQ35100::ACCUMULATOR_MODE)) {
        debug("Gauge mode set\n");

    } else {
        debug("Set gauge mode failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.setDesignCapacity(3800)) {
        debug("Design capacity set\n");

    } else {
        debug("Design capacity set failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.setBatteryAlert(0)) { // no alerts
        debug("Alerts cleared\n");

    } else {
        debug("Alert clear failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.startGauge()) {
        debug("Gauge started\n");

    } else {
        debug("Could not start the gauge\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.calibrateVoltage(3600)) { // mV
        debug("Voltage calibration successful\n");

    } else {
        debug("Voltage calibration failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.performCCOffset()) {
        debug("CC offset successful\n");

    } else {
        debug("CC offset failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.performBoardOffset()) {
        debug("Board offset successful\n");

    } else {
        debug("Board offset failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.useInternalTemp(true) && gauge.calibrateTemperature(real_temp)) {
        debug("Internal temperature calibration successful\n");

    } else {
        debug("Internal temperature calibration failed\n");
        return 0;
    }

    ThisThread::sleep_for(1s);

    if (gauge.useInternalTemp(false) && gauge.calibrateTemperature(real_temp)) {
        debug("External temperature calibration successful\n");

    } else {
        debug("External temperature calibration failed\n");
        return 0;
    }

    debug("Done\n");

    gauge.setSecurityMode(BQ35100::SECURITY_SEALED);
}
Clone this wiki locally