Advertisement
microrobotics

INA226 Current Sensor

Apr 18th, 2023 (edited)
4,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.33 KB | None | 0 0
  1. /*******************************************************************************************************************
  2. ** Program to demonstrate the INA226 library for the Arduino IDE. A simple infinite loop of measurments will      **
  3. ** display the bus voltage and current running through the INA226.                                                **
  4. **                                                                                                                **
  5. ** Detailed documentation can be found on the GitHub Wiki pages at https://github.com/SV-Zanshin/INA226/wiki      **
  6. **                                                                                                                **
  7. ** This example is for a INA226 set up to measure a 5-Volt load with a 0.1 Ohm resistor in place, this is the same**
  8. ** setup that can be found in the Adafruit INA219 breakout board.  The complex calibration options are done at    **
  9. ** runtime using the 2 parameters specified in the "begin()" call and the library has gone to great lengths to    **
  10. ** avoid the use of floating point to conserve space and minimize runtime.  This demo program uses floating point **
  11. ** only to convert and display the data conveniently. The INA226 uses 15 bits of precision, and even though the   **
  12. ** current and watt information is returned using 32-bit integers the precision remains the same.                 **
  13. **                                                                                                                **
  14. ** As of version 1.0.3 the library supports multiple INA226 devices.  The Atmel's EEPROM is used to store the 96  **
  15. ** bytes of static information per device using https://www.arduino.cc/en/Reference/EEPROM function calls.        **
  16. ** Although up to 16 devices could theoretically be present on the I2C bus the actual limit is determined by the  **
  17. ** available EEPROM - ATmega328 UNO has 1024k so can support up to 10 devices but the ATmega168 only has 512 bytes**
  18. ** which limits it to supporting at most 5 INA226s.  The library has been modified to be backwards compatible and **
  19. ** the device number (from 0 to number of devices found) is passed as the last parameter.                         **
  20. **                                                                                                                **
  21. ** The datasheet for the INA226 can be found at http://www.ti.com/lit/ds/symlink/ina226.pdf and it contains the   **
  22. ** information required in order to hook up the device. Unfortunately it comes as a VSSOP package but it can be   **
  23. ** soldered onto a breakout board for breadboard use. The INA226 is quite similar to the INA219 mentioned above,  **
  24. ** but it can take bus voltages of up to 36V (which I needed in order to monitor a 24V battery system which goes  **
  25. ** above 28V while charging and which is above the absolute limits of the INA219). It is also significantly more  **
  26. ** accurate than the INA219.                                                                                      **
  27. **                                                                                                                **
  28. ** This program is free software: you can redistribute it and/or modify it under the terms of the GNU General     **
  29. ** Public License as published by the Free Software Foundation, either version 3 of the License, or (at your      **
  30. ** option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY     **
  31. ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   **
  32. ** GNU General Public License for more details. You should have received a copy of the GNU General Public License **
  33. ** along with this program.  If not, see <http://www.gnu.org/licenses/>.                                          **
  34. **                                                                                                                **
  35. ** Vers.  Date       Developer                     Comments                                                       **
  36. ** ====== ========== ============================= ============================================================== **
  37. ** 1.0.5  2018-06-08 https://github.com/SV-Zanshin removed unneeded prototype definitions                         **
  38. ** 1.0.4  2018-06-01 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/11 Corrected loop  **
  39. ** 1.0.3  2017-09-18 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/6 Multiple INA226s **
  40. ** 1.0.2  2017-08-09 https://github.com/SV-Zanshin Cosmetic changes                                               **
  41. ** 1.0.1  2017-01-12 https://github.com/SV-Zanshin Minor code cleanup and added more comments                     **
  42. ** 1.0.0  2017-01-09 https://github.com/SV-Zanshin Cloned example from test program suite                         **
  43. **                                                                                                                **
  44. *******************************************************************************************************************/
  45. #include <INA226.h>                                                           // INA226 Library                   //
  46. /*******************************************************************************************************************
  47. ** Declare program Constants                                                                                      **
  48. *******************************************************************************************************************/
  49. const uint32_t SERIAL_SPEED          = 115200;                                // Use fast serial speed            //
  50. /*******************************************************************************************************************
  51. ** Declare global variables and instantiate classes                                                               **
  52. *******************************************************************************************************************/
  53. INA226_Class INA226;                                                          // INA class instantiation          //
  54. uint8_t devicesFound = 0;                                                     // Number of INA226s found          //
  55. /*******************************************************************************************************************
  56. ** Method Setup(). This is an Arduino IDE method which is called first upon initial boot or restart. It is only   **
  57. ** called one time and all of the variables and other initialization calls are done here prior to entering the    **
  58. ** main loop for data measurement and storage.                                                                    **
  59. *******************************************************************************************************************/
  60. void setup() {                                                                //                                  //
  61.   Serial.begin(SERIAL_SPEED);                                                 // Start serial communications      //
  62.   #ifdef  __AVR_ATmega32U4__                                                  // If we are a 32U4 processor, then //
  63.     delay(2000);                                                              // wait 2 seconds for the serial    //
  64.   #endif                                                                      // interface to initialize          //
  65.   Serial.print(F("\n\nDisplay INA226 Readings V1.0.5\n"));                    // Display program information      //
  66.   Serial.print(F(" - Searching & Initializing INA226\n"));                    // Display program information      //
  67.   // The begin initializes the calibration for an expected ±1 Amps maximum current and for a 0.1Ohm resistor, and //
  68.   // since no specific device is given as the 3rd parameter all devices are initially set to these values         //
  69.   devicesFound = INA226.begin(1,100000);                                      // Set expected Amps and resistor   //
  70.   Serial.print(F(" - Detected "));                                            //                                  //
  71.   Serial.print(devicesFound);                                                 //                                  //
  72.   Serial.println(F(" INA226 devices on I2C bus"));                            //                                  //
  73.   INA226.setAveraging(4);                                                     // Average each reading n-times     //
  74.   INA226.setBusConversion(7);                                                 // Maximum conversion time 8.244ms  //
  75.   INA226.setShuntConversion(7);                                               // Maximum conversion time 8.244ms  //
  76.   INA226.setMode(INA_MODE_CONTINUOUS_BOTH);                                   // Bus/shunt measured continuously  //
  77. } // of method setup()                                                        //                                  //
  78. /*******************************************************************************************************************
  79. ** This is the main program for the Arduino IDE, it is called in an infinite loop. The INA226 measurements are    **
  80. ** run in a simple infinite loop                                                                                  **
  81. *******************************************************************************************************************/
  82. void loop() {                                                                 // Main program loop                //
  83.   static uint16_t loopCounter = 0;                                            // Count the number of iterations   //
  84.   for (uint8_t i=0;i<devicesFound;i++) {                                      // Loop through all devices found   //
  85.     Serial.print(F("Bus voltage   "));                                        //                                  //
  86.     Serial.print(i+1);                                                        //                                  //
  87.     Serial.print(F(": "));                                                    //                                  //
  88.     Serial.print((float)INA226.getBusMilliVolts(true,i)/1000.0,4);            // Convert to millivolts            //
  89.     Serial.print(F("V\nShunt voltage "));                                     //                                  //
  90.     Serial.print(i+1);                                                        //                                  //
  91.     Serial.print(F(": "));                                                    //                                  //
  92.     Serial.print((float)INA226.getShuntMicroVolts(true,i)/1000.0,3);          // Convert to millivolts            //
  93.     Serial.print(F("mV\nBus amperage  "));                                    //                                  //
  94.     Serial.print(i+1);                                                        //                                  //
  95.     Serial.print(F(": "));                                                    //                                  //
  96.     Serial.print((float)INA226.getBusMicroAmps(i)/1000.0,4);                  // Convert to milliamp              //
  97.     Serial.print(F("mA\nBus wattage   "));                                    //                                  //
  98.     Serial.print(i+1);                                                        //                                  //
  99.     Serial.print(F(":  "));                                                   //                                  //
  100.     Serial.print((float)INA226.getBusMicroWatts(i)/1000.0,4);                 // Convert to milliwatts            //
  101.     Serial.print(F("mW\n\n"));                                                //                                  //
  102.   } // of for-next each device loop                                           //                                  //
  103.   delay(5000);                                                                // Wait 5 seconds for next reading  //
  104.   Serial.print(F("Loop iteration ")  );                                       //                                  //
  105.   Serial.print(++loopCounter);                                                //                                  //
  106.   Serial.print(F("\n\n")  );                                                  //                                  //
  107. } // of method loop                                                           //----------------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement