// Kookabus-01 demo for Arduino (any board with Wire and a 3.3 V Qwiic port: // SparkFun RedBoard, ESP32, RP2040, Teensy...). Needs no library. // // The board's INA226 runs from the host's 3.3 V, so it resets with the host: // kbBegin() must run after every power-up. #include const uint8_t KB_ADDR = 0x40; const float R_SHUNT = 0.005f; // ohm; trim against a multimeter if current reads off const float I_LSB = 0.0005f; // A per CURRENT bit const uint16_t CAL = (uint16_t)(0.00512f / (I_LSB * R_SHUNT) + 0.5f); // 2048 const uint16_t CONFIG = 0x4527; // 16-sample average, 1.1 ms conversions, continuous const float V_CUTOFF = 5 * 3.0f; // 5S pack empty static void kbWrite(uint8_t reg, uint16_t val) { Wire.beginTransmission(KB_ADDR); Wire.write(reg); Wire.write(val >> 8); Wire.write(val & 0xFF); Wire.endTransmission(); } static uint16_t kbRead(uint8_t reg) { Wire.beginTransmission(KB_ADDR); Wire.write(reg); Wire.endTransmission(false); Wire.requestFrom(KB_ADDR, (uint8_t)2); uint16_t hi = Wire.read(); return (hi << 8) | Wire.read(); } static bool kbBegin() { if (kbRead(0xFE) != 0x5449) return false; // TI manufacturer ID kbWrite(0x00, CONFIG); kbWrite(0x05, CAL); return true; } float kbVolts() { return kbRead(0x02) * 0.00125f; } // at the LOAD connector float kbAmps() { return (int16_t)kbRead(0x04) * I_LSB; } // into the load float kbWatts() { return kbRead(0x03) * 25 * I_LSB; } void setup() { Serial.begin(115200); Wire.begin(); Wire.setClock(400000); while (!kbBegin()) { Serial.println("Kookabus-01 not found at 0x40, check the Qwiic cable"); delay(1000); } } void loop() { float v = kbVolts(); Serial.print(v, 2); Serial.print(" V "); Serial.print(kbAmps(), 3); Serial.print(" A "); Serial.print(kbWatts(), 1); Serial.print(" W"); if (v < V_CUTOFF) Serial.print(" LOW - stop the load"); Serial.println(); delay(1000); }