pleasedontcode

INA260 SOC rev_12

Aug 26th, 2025
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: INA260 SOC
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-08-26 16:54:39
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* manage state of charge from 0 to 100% of a battery */
  21.     /* with nominal voltage of 14v. */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <Adafruit_INA260.h> //https://github.com/adafruit/Adafruit_INA260
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF I2C PINS *****/
  36. const uint8_t ina260_INA260_I2C_PIN_SDA_D21     = 21;
  37. const uint8_t ina260_INA260_I2C_PIN_SCL_D22     = 22;
  38. const uint8_t ina260_INA260_I2C_SLAVE_ADDRESS       = 64;
  39.  
  40. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  41. Adafruit_INA260 ina260 = Adafruit_INA260();
  42.  
  43. /****** GLOBALS *****/
  44. float battery_capacity_Ah = 5.0f; // Battery capacity in Amp-hours (adjust to your pack)
  45. float soc = 0.5f;                 // State of charge as a value 0.0 - 1.0
  46. unsigned long lastMillis = 0;
  47.  
  48. // Nominal voltage of the battery (used for energy-based SOC estimation)
  49. const float NOMINAL_VOLTAGE_V = 14.0f; // volts
  50. float energyTotal_Wh = 0.0f;          // Total energy in watt-hours (defined by capacity * nominal voltage)
  51. float energyRemaining_Wh = 0.0f;    // Remaining energy in watt-hours
  52.  
  53. void setup(void)
  54. {
  55.     // put your setup code here, to run once:
  56.     Serial.begin(115200);
  57.     while(!Serial) { delay(10); }
  58.  
  59.     // Initialize I2C on ESP32 with explicit pins
  60.     Wire.begin(ina260_INA260_I2C_PIN_SDA_D21, ina260_INA260_I2C_PIN_SCL_D22);
  61.  
  62.     Serial.println("Adafruit INA260 SOC Controller");
  63.  
  64.     if (!ina260.begin()) {
  65.         Serial.println("INA260 not found. Check wiring.");
  66.         while (1);
  67.     }
  68.  
  69.     // Optional: configure INA260 if needed
  70.     ina260.setAveragingCount(INA260_COUNT_16);
  71.     ina260.setVoltageConversionTime(INA260_TIME_140_US);
  72.     ina260.setCurrentConversionTime(INA260_TIME_140_US);
  73.  
  74.     // Initialize energy-based SOC tracking
  75.     energyTotal_Wh = battery_capacity_Ah * NOMINAL_VOLTAGE_V;
  76.     energyRemaining_Wh = soc * energyTotal_Wh;
  77.  
  78.     lastMillis = millis();
  79.     Serial.println("INA260 ready");
  80. }
  81.  
  82. void loop(void)
  83. {
  84.     // coulomb/energy counting based SOC estimation with nominal voltage reference
  85.     unsigned long now = millis();
  86.     float dt_s = (now - lastMillis) / 1000.0f;
  87.     if (dt_s <= 0.0f) dt_s = 0.001f; // avoid division by zero
  88.     lastMillis = now;
  89.  
  90.     // Read instantaneous measurements
  91.     long busVoltage_mV = ina260.readBusVoltage();
  92.     float Vbus_V = busVoltage_mV / 1000.0f;
  93.     float currentA = ina260.readCurrent() / 1000.0f; // in amperes
  94.     long power_mW = ina260.readPower();
  95.  
  96.     // Update energy remaining using instantaneous power estimate
  97.     float deltaWh = Vbus_V * currentA * dt_s / 3600.0f; // energy transferred this interval (Wh)
  98.     energyRemaining_Wh -= deltaWh; // subtract energy drawn from the battery
  99.     if (energyRemaining_Wh < 0.0f) energyRemaining_Wh = 0.0f;
  100.     if (energyRemaining_Wh > energyTotal_Wh) energyRemaining_Wh = energyTotal_Wh;
  101.  
  102.     // Update SOC (0.0 - 1.0 scale)
  103.     soc = energyRemaining_Wh / energyTotal_Wh;
  104.     if (soc < 0.0f) soc = 0.0f;
  105.     if (soc > 1.0f) soc = 1.0f;
  106.  
  107.     // Print measurements and SOC percentage
  108.     Serial.print("I: ");
  109.     Serial.print(currentA * 1000.0f);
  110.     Serial.print(" mA  V: ");
  111.     Serial.print(Vbus_V);
  112.     Serial.print(" V  ");
  113.     Serial.print("P: ");
  114.     Serial.print(power_mW);
  115.     Serial.print(" mW  SOC: ");
  116.     Serial.print(soc * 100.0f);
  117.     Serial.println(" %");
  118.  
  119.     delay(1000);
  120. }
  121.  
  122. /* END CODE */
  123.  
Advertisement
Add Comment
Please, Sign In to add comment