├── .gitattributes ├── .gitignore ├── Examples ├── BackgroundRead │ └── BackgroundRead.ino └── DisplayReadings │ └── DisplayReadings.ino ├── INA226.cpp ├── INA226.h ├── Images └── INA226.jpg ├── README.md ├── keywords.txt └── library.properties /.gitattributes: -------------------------------------------------------------------------------- 1 | ########################################################################################## 2 | # This section defines which file types are associated with which linguist-documentation # 3 | # language types. Without this, linguist detects this as mere c++ code and not as an # 4 | # Arduino library # 5 | ########################################################################################## 6 | examples/ linguist-documentation=false 7 | *.ino linguist-language=Arduino 8 | *.c linguist-language=Arduino 9 | *.cpp linguist-language=Arduino 10 | *.h linguist-language=Arduino 11 | 12 | # Auto detect text files and perform LF normalization 13 | * text=auto 14 | 15 | # Custom for Visual Studio 16 | *.cs diff=csharp 17 | 18 | # Standard to msysgit 19 | *.doc diff=astextplain 20 | *.DOC diff=astextplain 21 | *.docx diff=astextplain 22 | *.DOCX diff=astextplain 23 | *.dot diff=astextplain 24 | *.DOT diff=astextplain 25 | *.pdf diff=astextplain 26 | *.PDF diff=astextplain 27 | *.rtf diff=astextplain 28 | *.RTF diff=astextplain 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | 49 | # Files and directories from the Atmel Studio Arduino IDE 50 | .vs 51 | __vm 52 | Debug 53 | *.atsln 54 | *.componentinfo.xml 55 | *.cppproj 56 | Release 57 | -------------------------------------------------------------------------------- /Examples/BackgroundRead/BackgroundRead.ino: -------------------------------------------------------------------------------- 1 | /******************************************************************************************************************* 2 | ** Program to demonstrate using the interrupt pin of the INA226, a pin-change interrupt handler and the INA226 ** 3 | ** to read voltage and current information in the background while allowing the main Arduino code to continue ** 4 | ** processing normally until it is ready to consume the readings. ** 5 | ** ** 6 | ** Detailed documentation can be found on the GitHub Wiki pages at https://github.com/SV-Zanshin/INA226/wiki ** 7 | ** ** 8 | ** This example is for a INA226 set up to measure a 5-Volt load with a 0.1Ω resistor in place, this is the same ** 9 | ** setup that can be found in the Adafruit INA219 breakout board. The complex calibration options are done at ** 10 | ** runtime using the 2 parameters specified in the "begin()" call and the library has gone to great lengths to ** 11 | ** avoid the use of floating point to conserve space and minimize runtime. This demo program uses floating point ** 12 | ** only to convert and display the data conveniently. The INA226 uses 15 bits of precision, and even though the ** 13 | ** current and watt information is returned using 32-bit integers the precision remains the same. ** 14 | ** ** 15 | ** The INA226 is set up to measure using the maximum conversion length (and maximum accuracy) and then average ** 16 | ** those readings 64 times. This results in readings taking 8.244ms x 64 = 527.616ms or just less than 2 times ** 17 | ** a second. The pin-change interrupt handler is called when a reading is finished and the INA226 pulls the pin ** 18 | ** down to ground, it resets the pin status and adds the readings to the global variables. The main program will ** 19 | ** do whatever processing it has to and every 5 seconds it will display the current averaged readings and reset ** 20 | ** them. ** 21 | ** ** 22 | ** The datasheet for the INA226 can be found at http://www.ti.com/lit/ds/symlink/ina226.pdf and it contains the ** 23 | ** information required in order to hook up the device. Unfortunately it comes as a VSSOP package but it can be ** 24 | ** soldered onto a breakout board for breadboard use. The INA226 is quite similar to the INA219 mentioned above, ** 25 | ** but it can take bus voltages of up to 36V (which I needed in order to monitor a 24V battery system which goes ** 26 | ** above 28V while charging and which is above the absolute limits of the INA219). It is also significantly more ** 27 | ** accurate than the INA219. ** 28 | ** ** 29 | ** The interrupt is set to pin 8. The tests were done on an Arduino Micro, and the Atmel 82U4 chip only allows ** 30 | ** pin change interrupt on selected pins (SS,SCK,MISO,MOSI,8) so pin 8 was chosen. ** 31 | ** ** 32 | ** This program is free software: you can redistribute it and/or modify it under the terms of the GNU General ** 33 | ** Public License as published by the Free Software Foundation, either version 3 of the License, or (at your ** 34 | ** option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY ** 35 | ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 36 | ** GNU General Public License for more details. You should have received a copy of the GNU General Public License ** 37 | ** along with this program. If not, see . ** 38 | ** ** 39 | ** Vers. Date Developer Comments ** 40 | ** ====== ========== ============================= ============================================================== ** 41 | ** 1.0.4 2018-05-29 https://github.com/SV-Zanshin Added checking if the device is actually detected to code ** 42 | ** 1.0.3 2017-09-29 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/8. Default values ** 43 | ** 1.0.2 2017-08-10 https://github.com/SV-Zanshin Further changes to comments ** 44 | ** 1.0.1 2017-07-15 https://github.com/SV-Zanshin Cleaned up comments ** 45 | ** 1.0.0 2017-01-12 https://github.com/SV-Zanshin Created example ** 46 | ** ** 47 | *******************************************************************************************************************/ 48 | #include // INA226 Library // 49 | /******************************************************************************************************************* 50 | ** Declare program Constants ** 51 | *******************************************************************************************************************/ 52 | const uint8_t INA226_ALERT_PIN = 8; // Pin 8. Micro only allows SS,SCK, // 53 | const uint8_t GREEN_LED_PIN = 13; // Green LED (nonstandard location) // 54 | const uint32_t SERIAL_SPEED = 115200; // Use fast serial speed // 55 | /******************************************************************************************************************* 56 | ** Declare global variables and instantiate classes ** 57 | *******************************************************************************************************************/ 58 | INA226_Class INA226; // INA class instantiation // 59 | volatile uint64_t sumBusMillVolts = 0; // Sum of bus voltage readings // 60 | volatile int64_t sumBusMicroAmps = 0; // Sum of bus amperage readings // 61 | volatile uint8_t readings = 0; // Number of measurements taken // 62 | /******************************************************************************************************************* 63 | ** Declare interrupt service routine for the pin-change interrupt on pin 8 which is set in the setup() method ** 64 | *******************************************************************************************************************/ 65 | ISR (PCINT0_vect) { // handle pin change interrupt D8 // 66 | digitalWrite(GREEN_LED_PIN,!digitalRead(GREEN_LED_PIN)); // Toggle LED to show we are working// 67 | *digitalPinToPCMSK(INA226_ALERT_PIN)&=~bit(digitalPinToPCMSKbit(INA226_ALERT_PIN)); // Disable PCMSK pin // 68 | PCICR &= ~bit(digitalPinToPCICRbit(INA226_ALERT_PIN)); // disable interrupt for the group // 69 | sei(); // Enable interrupts for I2C calls // 70 | sumBusMillVolts += INA226.getBusMilliVolts(); // Read the current value // 71 | sumBusMicroAmps += INA226.getBusMicroAmps(); // Read the current value // 72 | readings++; // Increment the number of readings // 73 | INA226.waitForConversion(); // Resets INA226 interrupt flag // 74 | cli(); // Disable interrupts // 75 | *digitalPinToPCMSK(INA226_ALERT_PIN)|=bit(digitalPinToPCMSKbit(INA226_ALERT_PIN)); // Enable PCMSK pin // 76 | PCIFR |= bit (digitalPinToPCICRbit(INA226_ALERT_PIN)); // clear any outstanding interrupt // 77 | PCICR |= bit (digitalPinToPCICRbit(INA226_ALERT_PIN)); // enable interrupt for the group // 78 | } // of ISR handler for INT0 group of pins // // 79 | /******************************************************************************************************************* 80 | ** Method Setup(). This is an Arduino IDE method which is called first upon initial boot or restart. It is only ** 81 | ** called one time and all of the variables and other initialization calls are done here prior to entering the ** 82 | ** main loop for data measurement. ** 83 | *******************************************************************************************************************/ 84 | void setup() { // // 85 | pinMode(GREEN_LED_PIN, OUTPUT); // Define the green LED as an output// 86 | digitalWrite(GREEN_LED_PIN,true); // Turn on the LED // 87 | pinMode(INA226_ALERT_PIN,INPUT_PULLUP); // Declare pin with pullup resistors// 88 | *digitalPinToPCMSK(INA226_ALERT_PIN)|=bit(digitalPinToPCMSKbit(INA226_ALERT_PIN)); // Enable PCMSK pin // 89 | PCIFR |= bit (digitalPinToPCICRbit(INA226_ALERT_PIN)); // clear any outstanding interrupt // 90 | PCICR |= bit (digitalPinToPCICRbit(INA226_ALERT_PIN)); // enable interrupt for the group // 91 | Serial.begin(SERIAL_SPEED); // Start serial communications // 92 | #ifdef __AVR_ATmega32U4__ // If this is a 32U4 processor, // 93 | delay(3000); // wait 3 seconds for serial port // 94 | #endif // interface to initialize // 95 | Serial.print(F("\n\nBackground INA226 Read V1.0.0\n")); // Display program information // 96 | // The begin initialized the calibration for an expected ±1 Amps maximum current and for a 0.1Ω resistor // 97 | while (INA226.begin(1,100000)==0) { // // 98 | Serial.print(F("No Device detected. Sleeping 10 seconds.\n")); // // 99 | delay(10000); // // 100 | } // of if-then no device found // // 101 | INA226.setAveraging(64); // Average each reading n-times // 102 | INA226.setBusConversion(7); // Maximum conversion time 8.244ms // 103 | INA226.setShuntConversion(7); // Maximum conversion time 8.244ms // 104 | INA226.setMode(INA_MODE_CONTINUOUS_BOTH); // Bus/shunt measured continuously // 105 | INA226.setAlertPinOnConversion(true); // Make alert pin go low on finish // 106 | } // of method setup() // // 107 | /******************************************************************************************************************* 108 | ** This is the main program for the Arduino IDE, it is called in an infinite loop. The INA226 measurements are ** 109 | ** run in a simple infinite loop ** 110 | *******************************************************************************************************************/ 111 | void loop() { // Main program loop // 112 | static long lastMillis = millis(); // Store the last time we printed // 113 | /***************************************************************************************************************** 114 | ** Check to see if we have collected 10 or more readings each main loop iteration, and display the time and ** 115 | ** average information before resetting the values. Interrupts are turned off when resetting the values to ** 116 | ** ensure atomic operations ** 117 | *****************************************************************************************************************/ 118 | if (readings>=10) { // If it is time to display results // 119 | Serial.print(F("Averaging readings taken over ")); // // 120 | Serial.print((float)(millis()-lastMillis)/1000,2); // // 121 | Serial.print(F(" seconds.\nBus voltage: ")); // // 122 | Serial.print((float)sumBusMillVolts/readings/1000.0,4); // // 123 | Serial.print(F("V\nBus amperage: ")); // // 124 | Serial.print((float)sumBusMicroAmps/readings/1000.0,4); // // 125 | Serial.print(F("mA\n\n")); // // 126 | lastMillis = millis(); // // 127 | cli(); // Disable interrupts // 128 | readings = 0; // Reset values // 129 | sumBusMillVolts = 0; // Reset values // 130 | sumBusMicroAmps = 0; // Reset values // 131 | sei(); // Enable interrupts // 132 | } // of if-then we've reached the required amount of readings // // 133 | } // of method loop //----------------------------------// 134 | -------------------------------------------------------------------------------- /Examples/DisplayReadings/DisplayReadings.ino: -------------------------------------------------------------------------------- 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 . ** 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 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. ** 13 | ** ** 14 | *******************************************************************************************************************/ 15 | #include "INA226.h" // Include the header definition // 16 | #include // I2C Library definition // 17 | #include // Include the EEPROM library // 18 | INA226_Class::INA226_Class() {} // Class constructor // 19 | INA226_Class::~INA226_Class() {} // Unused class destructor // 20 | /******************************************************************************************************************* 21 | ** Method begin() sets the INA226 Configuration details, without which meaningful readings cannot be made. If it ** 22 | ** is called without the option deviceNumber parameter then the settings are applied to all devices, otherwise ** 23 | ** just that specific device is targeted. ** 24 | *******************************************************************************************************************/ 25 | uint8_t INA226_Class::begin(const uint8_t maxBusAmps, // Class initializer // 26 | const uint32_t microOhmR, // // 27 | const uint8_t deviceNumber ) { // // 28 | inaDet ina; // Hold device details in structure // 29 | uint16_t manufacturerId; // Read manufacturerId from device // 30 | if (_DeviceCount==0) { // Enumerate devices in first call // 31 | Wire.begin(); // Start the I2C wire subsystem // 32 | for(uint8_t deviceAddress = 64;deviceAddress<79;deviceAddress++) { // Loop for each possible address // 33 | Wire.beginTransmission(deviceAddress); // See if something is at address // 34 | if (Wire.endTransmission() == 0) { // by checking the return error // 35 | if (readWord(INA_MANUFACTURER_ID_REGISTER,deviceAddress)==0x5449) { // Check hard-coded manufacturerId // 36 | writeWord(INA_CONFIGURATION_REGISTER,INA_RESET_DEVICE,deviceAddress);// Force INAs to reset // 37 | delay(I2C_DELAY); // Wait for INA to finish resetting // 38 | if (readWord(INA_CONFIGURATION_REGISTER,deviceAddress) // Yes, we've found an INA226! // 39 | ==INA_DEFAULT_CONFIGURATION) { // // 40 | ina.address = deviceAddress; // Store device address // 41 | ina.operatingMode = B111; // Default to continuous mode // 42 | if ((_DeviceCount*sizeof(ina)). ** 33 | ** ** 34 | ** Vers. Date Developer Comments ** 35 | ** ====== ========== ============================= ============================================================== ** 36 | ** 1.0.7 2018-06-08 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/14. Missing calls ** 37 | ** EEPROM.Get() for device number caused errors sporadic errors ** 38 | ** 1.0.6 2018-06-01 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/12. Add getMode() ** 39 | ** 1.0.6 2018-05-29 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/10. Limit Scan addr** 40 | ** 1.0.5 2017-09-24 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/6. Multiple INA226 ** 41 | ** 1.0.5b 2017-09-23 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/6. Multiple INA226 ** 42 | ** 1.0.5a 2017-09-18 https://github.com/SV-Zanshin https://github.com/SV-Zanshin/INA226/issues/6. Multiple INA226 ** 43 | ** 1.0.4 2017-08-13 https://github.com/SV-Zanshin Enhancement #5, removed while() loop after Wire.requestFrom() ** 44 | ** 1.0.3 2017-08-09 https://github.com/SV-Zanshin Fix https://github.com/SV-Zanshin/INA226/issues/4. Overflows ** 45 | ** in computations of begin() and getShuntMicroVolts() functions. ** 46 | ** 1.0.2 2017-07-30 https://github.com/SV-Zanshin Optional parameter default values only in function prototypes ** 47 | ** 1.0.1 2017-05-26 https://github.com/SV-Zanshin Changed _CurrentLSB from uint16_t to uint32_t due to overflow ** 48 | ** 1.0.0 2017-01-10 https://github.com/SV-Zanshin Fixed library file name, added constants for setMode() call ** 49 | ** 1.0.0 2017-01-09 https://github.com/SV-Zanshin Added reset() and setMode() calls ** 50 | ** 1.0.b2 2017-01-08 https://github.com/SV-Zanshin Removed INA219 code, concentrating on only the INA226 ** 51 | ** 1.0.b1 2017-01-05 https://github.com/SV-Zanshin Created class ** 52 | ** ** 53 | *******************************************************************************************************************/ 54 | #include "Arduino.h" // Arduino data type definitions // 55 | #ifndef INA226_Class_h // Guard code definition // 56 | #define debug_Mode // Comment out when not needed // 57 | #define INA226__Class_h // Define the name inside guard code// 58 | /***************************************************************************************************************** 59 | ** Declare structures used in the class ** 60 | *****************************************************************************************************************/ 61 | typedef struct { // Structure of values per device // 62 | uint8_t address; // I2C Address of device // 63 | uint16_t calibration; // Calibration register value // 64 | uint32_t current_LSB; // Amperage LSB // 65 | uint32_t power_LSB; // Wattage LSB // 66 | uint8_t operatingMode; // Default continuous mode operation// 67 | } inaDet; // of structure // // 68 | /***************************************************************************************************************** 69 | ** Declare constants used in the class ** 70 | *****************************************************************************************************************/ 71 | const uint8_t I2C_DELAY = 10; // Microsecond delay on write // 72 | const uint8_t INA_CONFIGURATION_REGISTER = 0; // Registers common to all INAs // 73 | const uint8_t INA_SHUNT_VOLTAGE_REGISTER = 1; // // 74 | const uint8_t INA_BUS_VOLTAGE_REGISTER = 2; // // 75 | const uint8_t INA_POWER_REGISTER = 3; // // 76 | const uint8_t INA_CURRENT_REGISTER = 4; // // 77 | const uint8_t INA_CALIBRATION_REGISTER = 5; // // 78 | const uint8_t INA_MASK_ENABLE_REGISTER = 6; // // 79 | const uint8_t INA_MANUFACTURER_ID_REGISTER = 0xFE; // // 80 | const uint16_t INA_RESET_DEVICE = 0x8000; // Write to configuration to reset // 81 | const uint16_t INA_DEFAULT_CONFIGURATION = 0x4127; // Default configuration register // 82 | const uint16_t INA_BUS_VOLTAGE_LSB = 125; // LSB in uV *100 1.25mV // 83 | const uint16_t INA_SHUNT_VOLTAGE_LSB = 25; // LSB in uV *10 2.5uV // 84 | const uint16_t INA_CONFIG_AVG_MASK = 0x0E00; // Bits 9-11 // 85 | const uint16_t INA_CONFIG_BUS_TIME_MASK = 0x01C0; // Bits 6-8 // 86 | const uint16_t INA_CONFIG_SHUNT_TIME_MASK = 0x0038; // Bits 3-5 // 87 | const uint16_t INA_CONVERSION_READY_MASK = 0x0080; // Bit 4 // 88 | const uint16_t INA_CONFIG_MODE_MASK = 0x0007; // Bits 0-3 // 89 | const uint8_t INA_MODE_TRIGGERED_SHUNT = B001; // Triggered shunt, no bus // 90 | const uint8_t INA_MODE_TRIGGERED_BUS = B010; // Triggered bus, no shunt // 91 | const uint8_t INA_MODE_TRIGGERED_BOTH = B011; // Triggered bus and shunt // 92 | const uint8_t INA_MODE_POWER_DOWN = B100; // shutdown or power-down // 93 | const uint8_t INA_MODE_CONTINUOUS_SHUNT = B101; // Continuous shunt, no bus // 94 | const uint8_t INA_MODE_CONTINUOUS_BUS = B110; // Continuous bus, no shunt // 95 | const uint8_t INA_MODE_CONTINUOUS_BOTH = B111; // Both continuous, default value // 96 | /***************************************************************************************************************** 97 | ** Declare class header ** 98 | *****************************************************************************************************************/ 99 | class INA226_Class { // Class definition // 100 | public: // Publicly visible methods // 101 | INA226_Class(); // Class constructor // 102 | ~INA226_Class(); // Class destructor // 103 | uint8_t begin(const uint8_t maxBusAmps, // Class initializer // 104 | const uint32_t microOhmR, // // 105 | const uint8_t deviceNumber = UINT8_MAX ); // // 106 | uint16_t getBusMilliVolts(const bool waitSwitch=false, // Retrieve Bus voltage in mV // 107 | const uint8_t deviceNumber=0); // // 108 | int16_t getShuntMicroVolts(const bool waitSwitch=false, // Retrieve Shunt voltage in uV // 109 | const uint8_t deviceNumber=0); // // 110 | int32_t getBusMicroAmps(const uint8_t deviceNumber=0); // Retrieve micro-amps // 111 | int32_t getBusMicroWatts(const uint8_t deviceNumber=0); // Retrieve micro-watts // 112 | void reset(const uint8_t deviceNumber=0); // Reset the device // 113 | void setMode(const uint8_t mode,const uint8_t devNumber=UINT8_MAX); // Set the monitoring mode // 114 | uint8_t getMode(const uint8_t devNumber=UINT8_MAX); // Get the monitoring mode // 115 | void setAveraging(const uint16_t averages, // Set the number of averages taken // 116 | const uint8_t deviceNumber=UINT8_MAX); // // 117 | void setBusConversion(uint8_t convTime, // Set timing for Bus conversions // 118 | const uint8_t deviceNumber=UINT8_MAX); // // 119 | void setShuntConversion(uint8_t convTime, // Set timing for Shunt conversions // 120 | const uint8_t deviceNumber=UINT8_MAX); // // 121 | void waitForConversion(const uint8_t deviceNumber=UINT8_MAX); // wait for conversion to complete // 122 | void setAlertPinOnConversion(const bool alertState, // Enable pin change on conversion // 123 | const uint8_t deviceNumber=UINT8_MAX); // // 124 | private: // Private variables and methods // 125 | uint8_t readByte(const uint8_t addr, const uint8_t deviceAddress); // Read a byte from an I2C address // 126 | int16_t readWord(const uint8_t addr, const uint8_t deviceAddress); // Read a word from an I2C address // 127 | void writeByte(const uint8_t addr, const uint8_t data, // Write a byte to an I2C address // 128 | const uint8_t deviceAddress); // // 129 | void writeWord(const uint8_t addr, const uint16_t data, // Write two bytes to an I2C address// 130 | const uint8_t deviceAddress); // // 131 | uint8_t _TransmissionStatus = 0; // Return code for I2C transmission // 132 | uint8_t _DeviceCount = 0; // Number of INA226s detected // 133 | }; // of INA226_Class definition // // 134 | #endif //----------------------------------// 135 | -------------------------------------------------------------------------------- /Images/INA226.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zanduino/INA226/6a63f4f04f3766a97db893b0e33f79b3c4f3a4a1/Images/INA226.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # INA226 - Merged into new library [INA](https://github.com/SV-Zanshin/INA) 2 | ## :bangbang: This library has been deprecated and is obsolete :bangbang: 3 | Further development and bug fixes will be done in the new [INA](https://github.com/SV-Zanshin/INA) library. 4 | 5 | 6 | The new [INA](https://github.com/SV-Zanshin/INA) library along with the associated example programs and documentation at https://github.com/SV-Zanshin/INA/wiki supports multiple devices of multiple types in one combined library. The INA226 library functions have been duplicated in the new INA library and several additional functions have been incorporated as well. In addition, several minor bugs were detected and fixed during the transition to the new library. 7 | 8 | ## Original page follows: 9 | INA226 High-Side/Low-Side Bi-Directional I2C Current and Power Monitor library for the _Arduino_. Texas Instruments produces this family of power monitors and the series is described at on their product page at [INA226](http://www.ti.com/product/INA226). 10 | ## INA219 vs INA226 11 | Several breakout boards, tutorials and libraries exist for the INA219, which is the "little brother" to this INA226 chip. While the pin 12 | layout is similar, with the INA219 having 8 pins and the INA226 having 2 more pins, the internal configuration settings and registers are 13 | different and require the functions and methods in this library to access. 14 | ## Hardware layout 15 | The [datasheet](http://www.ti.com/lit/ds/symlink/ina226.pdf) has examples of how to hook up INA226. The package is a small VSSOP and I used a blank breakout board, some solder paste, a frying pan, desoldering braid, a magnifying glass and quite a bit of time to set up the first breadboard example. I've since seen breakout boards available on the web but since only a few external components are necessary apart from connecting the 10 pins of the INA226 I'll remain with self-build. 16 | ## Library description 17 | The library locates all INA226 devices on the I2C chain. Each unit can be individually configured with 4 setup parameters describing the expected voltage, shunt / resistor values which then set the internal configuration registers is ready to begin accurate measurements. The details of how to setup the library along with all of the publicly available methods can be found on the [INA226 wiki pages](https://github.com/SV-Zanshin/INA226/wiki). 18 | Great lengths have been taken to avoid the use of floating point in the library. To keep the original level of precision without loss but to allow the full range of voltages and amperes to be returned the amperage results are returned as 32-bit integers. 19 | 20 | The INA226 has a dedicated interrupt pin which can be used to trigger pin-change interrupts on the Arduino and the examples contain a program that measures readings using this output pin so that the Arduino can perform other concurrent tasks while still retrieving measurements. 21 | 22 | ![Zanshin Logo](https://www.sv-zanshin.com/r/images/site/gif/zanshinkanjitiny.gif) 23 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ################################### 2 | # Syntax Coloring Map for library # 3 | ################################### 4 | 5 | ################################ 6 | # Classes/Datatypes (KEYWORD1) # 7 | ################################ 8 | INA226_Class KEYWORD1 9 | 10 | #################################### 11 | # Methods and Functions (KEYWORD2) # 12 | #################################### 13 | begin KEYWORD2 14 | getBusMilliVolts KEYWORD2 15 | getShuntMicroVolts KEYWORD2 16 | getBusMicroAmps KEYWORD2 17 | getBusMicroWatts KEYWORD2 18 | reset KEYWORD2 19 | setMode KEYWORD2 20 | setAveraging KEYWORD2 21 | setBusConversion KEYWORD2 22 | setShuntConversion KEYWORD2 23 | setAlertPinOnConversion KEYWORD2 24 | waitForConversion KEYWORD2 25 | 26 | ######################## 27 | # Constants (LITERAL1) # 28 | ######################## 29 | 30 | 31 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=INA226 2 | version=1.0.3 3 | author=https://github.com/SV-Zanshin 4 | maintainer=https://github.com/SV-Zanshin 5 | sentence=Read INA226 current and voltage data 6 | paragraph=Read INA226 current and voltage data 7 | category=Device Control 8 | url=https://github.com/SV-Zanshin/INA226 9 | architectures=* 10 | --------------------------------------------------------------------------------