Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: INA260 SOC
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-08-26 16:54:39
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* manage state of charge from 0 to 100% of a battery */
- /* with nominal voltage of 14v. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Adafruit_INA260.h> //https://github.com/adafruit/Adafruit_INA260
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t ina260_INA260_I2C_PIN_SDA_D21 = 21;
- const uint8_t ina260_INA260_I2C_PIN_SCL_D22 = 22;
- const uint8_t ina260_INA260_I2C_SLAVE_ADDRESS = 64;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- Adafruit_INA260 ina260 = Adafruit_INA260();
- /****** GLOBALS *****/
- float battery_capacity_Ah = 5.0f; // Battery capacity in Amp-hours (adjust to your pack)
- float soc = 0.5f; // State of charge as a value 0.0 - 1.0
- unsigned long lastMillis = 0;
- // Nominal voltage of the battery (used for energy-based SOC estimation)
- const float NOMINAL_VOLTAGE_V = 14.0f; // volts
- float energyTotal_Wh = 0.0f; // Total energy in watt-hours (defined by capacity * nominal voltage)
- float energyRemaining_Wh = 0.0f; // Remaining energy in watt-hours
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- while(!Serial) { delay(10); }
- // Initialize I2C on ESP32 with explicit pins
- Wire.begin(ina260_INA260_I2C_PIN_SDA_D21, ina260_INA260_I2C_PIN_SCL_D22);
- Serial.println("Adafruit INA260 SOC Controller");
- if (!ina260.begin()) {
- Serial.println("INA260 not found. Check wiring.");
- while (1);
- }
- // Optional: configure INA260 if needed
- ina260.setAveragingCount(INA260_COUNT_16);
- ina260.setVoltageConversionTime(INA260_TIME_140_US);
- ina260.setCurrentConversionTime(INA260_TIME_140_US);
- // Initialize energy-based SOC tracking
- energyTotal_Wh = battery_capacity_Ah * NOMINAL_VOLTAGE_V;
- energyRemaining_Wh = soc * energyTotal_Wh;
- lastMillis = millis();
- Serial.println("INA260 ready");
- }
- void loop(void)
- {
- // coulomb/energy counting based SOC estimation with nominal voltage reference
- unsigned long now = millis();
- float dt_s = (now - lastMillis) / 1000.0f;
- if (dt_s <= 0.0f) dt_s = 0.001f; // avoid division by zero
- lastMillis = now;
- // Read instantaneous measurements
- long busVoltage_mV = ina260.readBusVoltage();
- float Vbus_V = busVoltage_mV / 1000.0f;
- float currentA = ina260.readCurrent() / 1000.0f; // in amperes
- long power_mW = ina260.readPower();
- // Update energy remaining using instantaneous power estimate
- float deltaWh = Vbus_V * currentA * dt_s / 3600.0f; // energy transferred this interval (Wh)
- energyRemaining_Wh -= deltaWh; // subtract energy drawn from the battery
- if (energyRemaining_Wh < 0.0f) energyRemaining_Wh = 0.0f;
- if (energyRemaining_Wh > energyTotal_Wh) energyRemaining_Wh = energyTotal_Wh;
- // Update SOC (0.0 - 1.0 scale)
- soc = energyRemaining_Wh / energyTotal_Wh;
- if (soc < 0.0f) soc = 0.0f;
- if (soc > 1.0f) soc = 1.0f;
- // Print measurements and SOC percentage
- Serial.print("I: ");
- Serial.print(currentA * 1000.0f);
- Serial.print(" mA V: ");
- Serial.print(Vbus_V);
- Serial.print(" V ");
- Serial.print("P: ");
- Serial.print(power_mW);
- Serial.print(" mW SOC: ");
- Serial.print(soc * 100.0f);
- Serial.println(" %");
- delay(1000);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment