├── README.md ├── library.properties ├── library.json ├── CHANGELOG ├── LICENSE ├── keywords.txt ├── src ├── INA219.h └── INA219.cpp └── examples └── INA219_simple └── INA219_simple.ino /README.md: -------------------------------------------------------------------------------- 1 | Arduino-INA219 2 | ============== 3 | 4 | INA219 Zero-Drift, Bi-directional Current/Power Monitor Arduino Library 5 | 6 | Tutorials: http://www.jarzebski.pl/arduino/czujniki-i-sensory/cyfrowy-czujnik-pradu-mocy-ina219.html 7 | 8 | This library use I2C to communicate, 2 pins are required to interface. 9 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=INA219 Arduino Library 2 | version=1.1.0 3 | author=Korneliusz Jarzębski 4 | maintainer=Korneliusz Jarzębski 5 | sentence=INA219 Zero-Drift, Bi-directional Current/Power Monitor Arduino Library 6 | paragraph= 7 | category=Sensors 8 | url=https://github.com/jarzebski/Arduino-INA219 9 | architectures=* 10 | includes=INA219.h 11 | depends= 12 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Arduino-INA219", 3 | "keywords": "INA219, sensor, power monitoe, power, i2c, wire", 4 | "description": "INA219 Zero-Drift, Bi-directional Current/Power Monitor Arduino Library", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/jarzebski/Arduino-INA219" 8 | }, 9 | "authors": { 10 | "name": "Korneliusz Jarzębski", 11 | "url": "https://www.jarzebski.pl" 12 | }, 13 | "version": "1.1.0", 14 | "frameworks": "arduino", 15 | "platforms": "*" 16 | } 17 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | INA219 Arduino Library 1.1.0 / 18.05.2023 2 | ====================================================================== 3 | 4 | * Corrected bus voltage to read out an unsigned value 5 | * Remove beginTransmission and endTransmission in readRegister 6 | * Add PlatformIO and Arduino library informations 7 | * Change to MIT license 8 | * General cleanups 9 | 10 | INA219 Arduino Library 1.0.0 / 02.04.2014 11 | ====================================================================== 12 | 13 | * First release 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2014-2023 Korneliusz Jarzębski 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ########################################### 2 | # Syntax Coloring Map For HMC5883L 3 | ########################################### 4 | 5 | ########################################### 6 | # Datatypes (KEYWORD1) 7 | ########################################### 8 | 9 | INA219 KEYWORD1 10 | Vector KEYWORD1 11 | 12 | ########################################### 13 | # Methods and Functions (KEYWORD2) 14 | ########################################### 15 | begin KEYWORD2 16 | configure KEYWORD2 17 | calibrate KEYWORD2 18 | getRange KEYWORD2 19 | getGain KEYWORD2 20 | getBusRes KEYWORD2 21 | getShuntRes KEYWORD2 22 | getMode KEYWORD2 23 | readShuntCurrent KEYWORD2 24 | readShuntVoltage KEYWORD2 25 | readBusPower KEYWORD2 26 | readBusVoltage KEYWORD2 27 | getMaxPossibleCurrent KEYWORD2 28 | getMaxCurrent KEYWORD2 29 | getMaxShuntVoltage KEYWORD2 30 | getMaxPower KEYWORD2 31 | 32 | ########################################### 33 | # Constants (LITERAL1) 34 | ########################################### 35 | INA219_RANGE_16V LITERAL1 36 | INA219_RANGE_32V LITERAL1 37 | INA219_GAIN_40MV LITERAL1 38 | INA219_GAIN_80MV LITERAL1 39 | INA219_GAIN_160MV LITERAL1 40 | INA219_GAIN_320MV LITERAL1 41 | INA219_BUS_RES_9BIT LITERAL1 42 | INA219_BUS_RES_10BIT LITERAL1 43 | INA219_BUS_RES_11BIT LITERAL1 44 | INA219_BUS_RES_12BIT LITERAL1 45 | INA219_SHUNT_RES_9BIT_1S LITERAL1 46 | INA219_SHUNT_RES_10BIT_1S LITERAL1 47 | INA219_SHUNT_RES_11BIT_1S LITERAL1 48 | INA219_SHUNT_RES_12BIT_1S LITERAL1 49 | INA219_SHUNT_RES_12BIT_2S LITERAL1 50 | INA219_SHUNT_RES_12BIT_4S LITERAL1 51 | INA219_SHUNT_RES_12BIT_8S LITERAL1 52 | INA219_SHUNT_RES_12BIT_16S LITERAL1 53 | INA219_SHUNT_RES_12BIT_32S LITERAL1 54 | INA219_SHUNT_RES_12BIT_64S LITERAL1 55 | INA219_SHUNT_RES_12BIT_128S LITERAL1 56 | INA219_MODE_POWER_DOWN LITERAL1 57 | INA219_MODE_SHUNT_TRIG LITERAL1 58 | INA219_MODE_BUS_TRIG LITERAL1 59 | INA219_MODE_SHUNT_BUS_TRIG LITERAL1 60 | INA219_MODE_ADC_OFF LITERAL1 61 | INA219_MODE_SHUNT_CONT LITERAL1 62 | INA219_MODE_BUS_CONT LITERAL1 63 | INA219_MODE_SHUNT_BUS_CONT LITERAL1 64 | -------------------------------------------------------------------------------- /src/INA219.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2014-2023 Korneliusz Jarzębski 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #ifndef INA219_h 28 | #define INA219_h 29 | 30 | #if ARDUINO >= 100 31 | #include "Arduino.h" 32 | #else 33 | #include "WProgram.h" 34 | #endif 35 | 36 | #define INA219_ADDRESS (0x40) 37 | 38 | #define INA219_CMD_READ (0x01) 39 | 40 | #define INA219_REG_CONFIG (0x00) 41 | #define INA219_REG_SHUNTVOLTAGE (0x01) 42 | #define INA219_REG_BUSVOLTAGE (0x02) 43 | #define INA219_REG_POWER (0x03) 44 | #define INA219_REG_CURRENT (0x04) 45 | #define INA219_REG_CALIBRATION (0x05) 46 | 47 | typedef enum 48 | { 49 | INA219_RANGE_16V = 0b0, 50 | INA219_RANGE_32V = 0b1 51 | } ina219_range_t; 52 | 53 | typedef enum 54 | { 55 | INA219_GAIN_40MV = 0b00, 56 | INA219_GAIN_80MV = 0b01, 57 | INA219_GAIN_160MV = 0b10, 58 | INA219_GAIN_320MV = 0b11 59 | } ina219_gain_t; 60 | 61 | typedef enum 62 | { 63 | INA219_BUS_RES_9BIT = 0b0000, 64 | INA219_BUS_RES_10BIT = 0b0001, 65 | INA219_BUS_RES_11BIT = 0b0010, 66 | INA219_BUS_RES_12BIT = 0b0011 67 | } ina219_busRes_t; 68 | 69 | typedef enum 70 | { 71 | INA219_SHUNT_RES_9BIT_1S = 0b0000, 72 | INA219_SHUNT_RES_10BIT_1S = 0b0001, 73 | INA219_SHUNT_RES_11BIT_1S = 0b0010, 74 | INA219_SHUNT_RES_12BIT_1S = 0b0011, 75 | INA219_SHUNT_RES_12BIT_2S = 0b1001, 76 | INA219_SHUNT_RES_12BIT_4S = 0b1010, 77 | INA219_SHUNT_RES_12BIT_8S = 0b1011, 78 | INA219_SHUNT_RES_12BIT_16S = 0b1100, 79 | INA219_SHUNT_RES_12BIT_32S = 0b1101, 80 | INA219_SHUNT_RES_12BIT_64S = 0b1110, 81 | INA219_SHUNT_RES_12BIT_128S = 0b1111 82 | } ina219_shuntRes_t; 83 | 84 | typedef enum 85 | { 86 | INA219_MODE_POWER_DOWN = 0b000, 87 | INA219_MODE_SHUNT_TRIG = 0b001, 88 | INA219_MODE_BUS_TRIG = 0b010, 89 | INA219_MODE_SHUNT_BUS_TRIG = 0b011, 90 | INA219_MODE_ADC_OFF = 0b100, 91 | INA219_MODE_SHUNT_CONT = 0b101, 92 | INA219_MODE_BUS_CONT = 0b110, 93 | INA219_MODE_SHUNT_BUS_CONT = 0b111, 94 | } ina219_mode_t; 95 | 96 | 97 | class INA219 98 | { 99 | public: 100 | 101 | bool begin(uint8_t address = INA219_ADDRESS); 102 | bool configure(ina219_range_t range = INA219_RANGE_32V, ina219_gain_t gain = INA219_GAIN_320MV, ina219_busRes_t busRes = INA219_BUS_RES_12BIT, ina219_shuntRes_t shuntRes = INA219_SHUNT_RES_12BIT_1S, ina219_mode_t mode = INA219_MODE_SHUNT_BUS_CONT); 103 | bool calibrate(float rShuntValue = 0.1, float iMaxExcepted = 2); 104 | 105 | ina219_range_t getRange(void); 106 | ina219_gain_t getGain(void); 107 | ina219_busRes_t getBusRes(void); 108 | ina219_shuntRes_t getShuntRes(void); 109 | ina219_mode_t getMode(void); 110 | 111 | float readShuntCurrent(void); 112 | float readShuntVoltage(void); 113 | float readBusPower(void); 114 | float readBusVoltage(void); 115 | 116 | float getMaxPossibleCurrent(void); 117 | float getMaxCurrent(void); 118 | float getMaxShuntVoltage(void); 119 | float getMaxPower(void); 120 | 121 | private: 122 | 123 | int8_t inaAddress; 124 | float currentLSB, powerLSB; 125 | float vShuntMax, vBusMax, rShunt; 126 | 127 | void writeRegister16(uint8_t reg, uint16_t val); 128 | int16_t readRegister16(uint8_t reg); 129 | }; 130 | 131 | #endif -------------------------------------------------------------------------------- /examples/INA219_simple/INA219_simple.ino: -------------------------------------------------------------------------------- 1 | /* 2 | INA219 Zero-Drift, Bi-directional Current/Power Monitor. Simple Example. 3 | Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/cyfrowy-czujnik-pradu-mocy-ina219.html 4 | GIT: https://github.com/jarzebski/Arduino-INA219 5 | Web: http://www.jarzebski.pl 6 | (c) 2014 by Korneliusz Jarzebski 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | INA219 ina; 13 | 14 | void checkConfig() 15 | { 16 | Serial.print("Mode: "); 17 | switch (ina.getMode()) 18 | { 19 | case INA219_MODE_POWER_DOWN: Serial.println("Power-Down"); break; 20 | case INA219_MODE_SHUNT_TRIG: Serial.println("Shunt Voltage, Triggered"); break; 21 | case INA219_MODE_BUS_TRIG: Serial.println("Bus Voltage, Triggered"); break; 22 | case INA219_MODE_SHUNT_BUS_TRIG: Serial.println("Shunt and Bus, Triggered"); break; 23 | case INA219_MODE_ADC_OFF: Serial.println("ADC Off"); break; 24 | case INA219_MODE_SHUNT_CONT: Serial.println("Shunt Voltage, Continuous"); break; 25 | case INA219_MODE_BUS_CONT: Serial.println("Bus Voltage, Continuous"); break; 26 | case INA219_MODE_SHUNT_BUS_CONT: Serial.println("Shunt and Bus, Continuous"); break; 27 | default: Serial.println("unknown"); 28 | } 29 | 30 | Serial.print("Range: "); 31 | switch (ina.getRange()) 32 | { 33 | case INA219_RANGE_16V: Serial.println("16V"); break; 34 | case INA219_RANGE_32V: Serial.println("32V"); break; 35 | default: Serial.println("unknown"); 36 | } 37 | 38 | Serial.print("Gain: "); 39 | switch (ina.getGain()) 40 | { 41 | case INA219_GAIN_40MV: Serial.println("+/- 40mV"); break; 42 | case INA219_GAIN_80MV: Serial.println("+/- 80mV"); break; 43 | case INA219_GAIN_160MV: Serial.println("+/- 160mV"); break; 44 | case INA219_GAIN_320MV: Serial.println("+/- 320mV"); break; 45 | default: Serial.println("unknown"); 46 | } 47 | 48 | Serial.print("Bus resolution: "); 49 | switch (ina.getBusRes()) 50 | { 51 | case INA219_BUS_RES_9BIT: Serial.println("9-bit"); break; 52 | case INA219_BUS_RES_10BIT: Serial.println("10-bit"); break; 53 | case INA219_BUS_RES_11BIT: Serial.println("11-bit"); break; 54 | case INA219_BUS_RES_12BIT: Serial.println("12-bit"); break; 55 | default: Serial.println("unknown"); 56 | } 57 | 58 | Serial.print("Shunt resolution: "); 59 | switch (ina.getShuntRes()) 60 | { 61 | case INA219_SHUNT_RES_9BIT_1S: Serial.println("9-bit / 1 sample"); break; 62 | case INA219_SHUNT_RES_10BIT_1S: Serial.println("10-bit / 1 sample"); break; 63 | case INA219_SHUNT_RES_11BIT_1S: Serial.println("11-bit / 1 sample"); break; 64 | case INA219_SHUNT_RES_12BIT_1S: Serial.println("12-bit / 1 sample"); break; 65 | case INA219_SHUNT_RES_12BIT_2S: Serial.println("12-bit / 2 samples"); break; 66 | case INA219_SHUNT_RES_12BIT_4S: Serial.println("12-bit / 4 samples"); break; 67 | case INA219_SHUNT_RES_12BIT_8S: Serial.println("12-bit / 8 samples"); break; 68 | case INA219_SHUNT_RES_12BIT_16S: Serial.println("12-bit / 16 samples"); break; 69 | case INA219_SHUNT_RES_12BIT_32S: Serial.println("12-bit / 32 samples"); break; 70 | case INA219_SHUNT_RES_12BIT_64S: Serial.println("12-bit / 64 samples"); break; 71 | case INA219_SHUNT_RES_12BIT_128S: Serial.println("12-bit / 128 samples"); break; 72 | default: Serial.println("unknown"); 73 | } 74 | 75 | Serial.print("Max possible current: "); 76 | Serial.print(ina.getMaxPossibleCurrent()); 77 | Serial.println(" A"); 78 | 79 | Serial.print("Max current: "); 80 | Serial.print(ina.getMaxCurrent()); 81 | Serial.println(" A"); 82 | 83 | Serial.print("Max shunt voltage: "); 84 | Serial.print(ina.getMaxShuntVoltage()); 85 | Serial.println(" V"); 86 | 87 | Serial.print("Max power: "); 88 | Serial.print(ina.getMaxPower()); 89 | Serial.println(" W"); 90 | } 91 | 92 | void setup() 93 | { 94 | Serial.begin(115200); 95 | 96 | Serial.println("Initialize INA219"); 97 | Serial.println("-----------------------------------------------"); 98 | 99 | // Default INA219 address is 0x40 100 | ina.begin(); 101 | 102 | // Configure INA219 103 | ina.configure(INA219_RANGE_32V, INA219_GAIN_320MV, INA219_BUS_RES_12BIT, INA219_SHUNT_RES_12BIT_1S); 104 | 105 | // Calibrate INA219. Rshunt = 0.1 ohm, Max excepted current = 2A 106 | ina.calibrate(0.1, 2); 107 | 108 | // Display configuration 109 | checkConfig(); 110 | 111 | Serial.println("-----------------------------------------------"); 112 | } 113 | 114 | void loop() 115 | { 116 | Serial.print("Bus voltage: "); 117 | Serial.print(ina.readBusVoltage(), 5); 118 | Serial.println(" V"); 119 | 120 | Serial.print("Bus power: "); 121 | Serial.print(ina.readBusPower(), 5); 122 | Serial.println(" W"); 123 | 124 | 125 | Serial.print("Shunt voltage: "); 126 | Serial.print(ina.readShuntVoltage(), 5); 127 | Serial.println(" V"); 128 | 129 | Serial.print("Shunt current: "); 130 | Serial.print(ina.readShuntCurrent(), 5); 131 | Serial.println(" A"); 132 | 133 | Serial.println(""); 134 | delay(1000); 135 | } 136 | -------------------------------------------------------------------------------- /src/INA219.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2014-2023 Korneliusz Jarzębski 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | 25 | */ 26 | 27 | #if ARDUINO >= 100 28 | #include "Arduino.h" 29 | #else 30 | #include "WProgram.h" 31 | #endif 32 | 33 | #include 34 | 35 | #include "INA219.h" 36 | 37 | bool INA219::begin(uint8_t address) 38 | { 39 | Wire.begin(); 40 | inaAddress = address; 41 | return true; 42 | } 43 | 44 | bool INA219::configure(ina219_range_t range, ina219_gain_t gain, ina219_busRes_t busRes, ina219_shuntRes_t shuntRes, ina219_mode_t mode) 45 | { 46 | uint16_t config = 0; 47 | 48 | config |= (range << 13 | gain << 11 | busRes << 7 | shuntRes << 3 | mode); 49 | 50 | switch(range) 51 | { 52 | case INA219_RANGE_32V: 53 | vBusMax = 32.0f; 54 | break; 55 | case INA219_RANGE_16V: 56 | vBusMax = 16.0f; 57 | break; 58 | } 59 | 60 | switch(gain) 61 | { 62 | case INA219_GAIN_320MV: 63 | vShuntMax = 0.32f; 64 | break; 65 | case INA219_GAIN_160MV: 66 | vShuntMax = 0.16f; 67 | break; 68 | case INA219_GAIN_80MV: 69 | vShuntMax = 0.08f; 70 | break; 71 | case INA219_GAIN_40MV: 72 | vShuntMax = 0.04f; 73 | break; 74 | } 75 | 76 | writeRegister16(INA219_REG_CONFIG, config); 77 | 78 | return true; 79 | } 80 | 81 | bool INA219::calibrate(float rShuntValue, float iMaxExpected) 82 | { 83 | uint16_t calibrationValue; 84 | rShunt = rShuntValue; 85 | 86 | float iMaxPossible, minimumLSB; 87 | 88 | iMaxPossible = vShuntMax / rShunt; 89 | 90 | minimumLSB = iMaxExpected / 32767; 91 | 92 | currentLSB = (uint16_t)(minimumLSB * 100000000); 93 | currentLSB /= 100000000; 94 | currentLSB /= 0.0001; 95 | currentLSB = ceil(currentLSB); 96 | currentLSB *= 0.0001; 97 | 98 | powerLSB = currentLSB * 20; 99 | 100 | calibrationValue = (uint16_t)((0.04096) / (currentLSB * rShunt)); 101 | 102 | writeRegister16(INA219_REG_CALIBRATION, calibrationValue); 103 | 104 | return true; 105 | } 106 | 107 | float INA219::getMaxPossibleCurrent(void) 108 | { 109 | return (vShuntMax / rShunt); 110 | } 111 | 112 | float INA219::getMaxCurrent(void) 113 | { 114 | float maxCurrent = (currentLSB * 32767); 115 | float maxPossible = getMaxPossibleCurrent(); 116 | 117 | if (maxCurrent > maxPossible) 118 | { 119 | return maxPossible; 120 | } else 121 | { 122 | return maxCurrent; 123 | } 124 | } 125 | 126 | float INA219::getMaxShuntVoltage(void) 127 | { 128 | float maxVoltage = getMaxCurrent() * rShunt; 129 | 130 | if (maxVoltage >= vShuntMax) 131 | { 132 | return vShuntMax; 133 | } else 134 | { 135 | return maxVoltage; 136 | } 137 | } 138 | 139 | float INA219::getMaxPower(void) 140 | { 141 | return (getMaxCurrent() * vBusMax); 142 | } 143 | 144 | float INA219::readBusPower(void) 145 | { 146 | return (readRegister16(INA219_REG_POWER) * powerLSB); 147 | } 148 | 149 | float INA219::readShuntCurrent(void) 150 | { 151 | return (readRegister16(INA219_REG_CURRENT) * currentLSB); 152 | } 153 | 154 | float INA219::readShuntVoltage(void) 155 | { 156 | float voltage; 157 | 158 | voltage = readRegister16(INA219_REG_SHUNTVOLTAGE); 159 | 160 | return (voltage / 100000); 161 | } 162 | 163 | float INA219::readBusVoltage(void) 164 | { 165 | uint16_t voltage; 166 | 167 | voltage = readRegister16(INA219_REG_BUSVOLTAGE); 168 | voltage >>= 3; 169 | 170 | return (voltage * 0.004); 171 | } 172 | 173 | ina219_range_t INA219::getRange(void) 174 | { 175 | uint16_t value; 176 | 177 | value = readRegister16(INA219_REG_CONFIG); 178 | value &= 0b0010000000000000; 179 | value >>= 13; 180 | 181 | return (ina219_range_t)value; 182 | } 183 | 184 | ina219_gain_t INA219::getGain(void) 185 | { 186 | uint16_t value; 187 | 188 | value = readRegister16(INA219_REG_CONFIG); 189 | value &= 0b0001100000000000; 190 | value >>= 11; 191 | 192 | return (ina219_gain_t)value; 193 | } 194 | 195 | ina219_busRes_t INA219::getBusRes(void) 196 | { 197 | uint16_t value; 198 | 199 | value = readRegister16(INA219_REG_CONFIG); 200 | value &= 0b0000011110000000; 201 | value >>= 7; 202 | 203 | return (ina219_busRes_t)value; 204 | } 205 | 206 | ina219_shuntRes_t INA219::getShuntRes(void) 207 | { 208 | uint16_t value; 209 | 210 | value = readRegister16(INA219_REG_CONFIG); 211 | value &= 0b0000000001111000; 212 | value >>= 3; 213 | 214 | return (ina219_shuntRes_t)value; 215 | } 216 | 217 | ina219_mode_t INA219::getMode(void) 218 | { 219 | uint16_t value; 220 | 221 | value = readRegister16(INA219_REG_CONFIG); 222 | value &= 0b0000000000000111; 223 | 224 | return (ina219_mode_t)value; 225 | } 226 | 227 | int16_t INA219::readRegister16(uint8_t reg) 228 | { 229 | int16_t value; 230 | 231 | Wire.beginTransmission(inaAddress); 232 | #if ARDUINO >= 100 233 | Wire.write(reg); 234 | #else 235 | Wire.send(reg); 236 | #endif 237 | Wire.endTransmission(); 238 | 239 | delay(1); 240 | 241 | Wire.requestFrom(inaAddress, 2); 242 | 243 | #if ARDUINO >= 100 244 | uint8_t vha = Wire.read(); 245 | uint8_t vla = Wire.read(); 246 | #else 247 | uint8_t vha = Wire.receive(); 248 | uint8_t vla = Wire.receive(); 249 | #endif 250 | 251 | value = vha << 8 | vla; 252 | 253 | return value; 254 | } 255 | 256 | void INA219::writeRegister16(uint8_t reg, uint16_t val) 257 | { 258 | uint8_t vla; 259 | vla = (uint8_t)val; 260 | val >>= 8; 261 | 262 | Wire.beginTransmission(inaAddress); 263 | #if ARDUINO >= 100 264 | Wire.write(reg); 265 | Wire.write((uint8_t)val); 266 | Wire.write(vla); 267 | #else 268 | Wire.send(reg); 269 | Wire.send((uint8_t)val); 270 | Wire.send(vla); 271 | #endif 272 | Wire.endTransmission(); 273 | } 274 | --------------------------------------------------------------------------------