├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── githubci.yml ├── .gitignore ├── Adafruit_INA219.cpp ├── Adafruit_INA219.h ├── README.md ├── assets └── board.jpg ├── code-of-conduct.md ├── examples ├── getcurrent │ └── getcurrent.ino └── ina_poweroled │ └── ina_poweroled.ino ├── library.properties └── license.txt /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit INA219 Arduino Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # osx 2 | .DS_Store 3 | 4 | # doxygen 5 | Doxyfile* 6 | doxygen_sqlite3.db 7 | html 8 | *.tmp 9 | -------------------------------------------------------------------------------- /Adafruit_INA219.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_INA219.cpp 3 | * 4 | * @mainpage Adafruit INA219 current/power monitor IC 5 | * 6 | * @section intro_sec Introduction 7 | * 8 | * Driver for the INA219 current sensor 9 | * 10 | * This is a library for the Adafruit INA219 breakout 11 | * ----> https://www.adafruit.com/product/904 12 | * 13 | * Adafruit invests time and resources providing this open source code, 14 | * please support Adafruit and open-source hardware by purchasing 15 | * products from Adafruit! 16 | * 17 | * @section author Author 18 | * 19 | * Written by Bryan Siepert and Kevin "KTOWN" Townsend for Adafruit Industries. 20 | * 21 | * @section license License 22 | * 23 | * BSD license, all text here must be included in any redistribution. 24 | * 25 | */ 26 | 27 | #include "Arduino.h" 28 | 29 | #include 30 | 31 | #include "Adafruit_INA219.h" 32 | 33 | /*! 34 | * @brief Instantiates a new INA219 class 35 | * @param addr the I2C address the device can be found on. Default is 0x40 36 | */ 37 | Adafruit_INA219::Adafruit_INA219(uint8_t addr) { 38 | ina219_i2caddr = addr; 39 | ina219_currentDivider_mA = 0; 40 | ina219_powerMultiplier_mW = 0.0f; 41 | } 42 | 43 | /*! 44 | * @brief INA219 class destructor 45 | */ 46 | Adafruit_INA219::~Adafruit_INA219() { delete i2c_dev; } 47 | 48 | /*! 49 | * @brief Sets up the HW (defaults to 32V and 2A for calibration values) 50 | * @param theWire the TwoWire object to use 51 | * @return true: success false: Failed to start I2C 52 | */ 53 | bool Adafruit_INA219::begin(TwoWire *theWire) { 54 | if (!i2c_dev) { 55 | i2c_dev = new Adafruit_I2CDevice(ina219_i2caddr, theWire); 56 | } 57 | 58 | if (!i2c_dev->begin()) { 59 | return false; 60 | } 61 | init(); 62 | return true; 63 | } 64 | 65 | /*! 66 | * @brief begin I2C and set up the hardware 67 | */ 68 | void Adafruit_INA219::init() { 69 | // Set chip to large range config values to start 70 | setCalibration_32V_2A(); 71 | } 72 | 73 | /*! 74 | * @brief Gets the raw bus voltage (16-bit signed integer, so +-32767) 75 | * @return the raw bus voltage reading 76 | */ 77 | int16_t Adafruit_INA219::getBusVoltage_raw() { 78 | uint16_t value; 79 | 80 | Adafruit_BusIO_Register bus_voltage_reg = 81 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_BUSVOLTAGE, 2, MSBFIRST); 82 | _success = bus_voltage_reg.read(&value); 83 | 84 | // Shift to the right 3 to drop CNVR and OVF and multiply by LSB 85 | return (int16_t)((value >> 3) * 4); 86 | } 87 | 88 | /*! 89 | * @brief Gets the raw shunt voltage (16-bit signed integer, so +-32767) 90 | * @return the raw shunt voltage reading 91 | */ 92 | int16_t Adafruit_INA219::getShuntVoltage_raw() { 93 | uint16_t value; 94 | Adafruit_BusIO_Register shunt_voltage_reg = 95 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_SHUNTVOLTAGE, 2, MSBFIRST); 96 | _success = shunt_voltage_reg.read(&value); 97 | return value; 98 | } 99 | 100 | /*! 101 | * @brief Gets the raw current value (16-bit signed integer, so +-32767) 102 | * @return the raw current reading 103 | */ 104 | int16_t Adafruit_INA219::getCurrent_raw() { 105 | uint16_t value; 106 | 107 | // Sometimes a sharp load will reset the INA219, which will 108 | // reset the cal register, meaning CURRENT and POWER will 109 | // not be available ... avoid this by always setting a cal 110 | // value even if it's an unfortunate extra step 111 | Adafruit_BusIO_Register calibration_reg = 112 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST); 113 | calibration_reg.write(ina219_calValue, 2); 114 | 115 | // Now we can safely read the CURRENT register! 116 | Adafruit_BusIO_Register current_reg = 117 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CURRENT, 2, MSBFIRST); 118 | _success = current_reg.read(&value); 119 | return value; 120 | } 121 | 122 | /*! 123 | * @brief Gets the raw power value (16-bit signed integer, so +-32767) 124 | * @return raw power reading 125 | */ 126 | int16_t Adafruit_INA219::getPower_raw() { 127 | uint16_t value; 128 | 129 | // Sometimes a sharp load will reset the INA219, which will 130 | // reset the cal register, meaning CURRENT and POWER will 131 | // not be available ... avoid this by always setting a cal 132 | // value even if it's an unfortunate extra step 133 | Adafruit_BusIO_Register calibration_reg = 134 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST); 135 | calibration_reg.write(ina219_calValue, 2); 136 | 137 | // Now we can safely read the POWER register! 138 | Adafruit_BusIO_Register power_reg = 139 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_POWER, 2, MSBFIRST); 140 | _success = power_reg.read(&value); 141 | return value; 142 | } 143 | 144 | /*! 145 | * @brief Gets the shunt voltage in mV (so +-327mV) 146 | * @return the shunt voltage converted to millivolts 147 | */ 148 | float Adafruit_INA219::getShuntVoltage_mV() { 149 | int16_t value; 150 | value = getShuntVoltage_raw(); 151 | return value * 0.01; 152 | } 153 | 154 | /*! 155 | * @brief Gets the bus voltage in volts 156 | * @return the bus voltage converted to volts 157 | */ 158 | float Adafruit_INA219::getBusVoltage_V() { 159 | int16_t value = getBusVoltage_raw(); 160 | return value * 0.001; 161 | } 162 | 163 | /*! 164 | * @brief Gets the current value in mA, taking into account the 165 | * config settings and current LSB 166 | * @return the current reading convereted to milliamps 167 | */ 168 | float Adafruit_INA219::getCurrent_mA() { 169 | float valueDec = getCurrent_raw(); 170 | valueDec /= ina219_currentDivider_mA; 171 | return valueDec; 172 | } 173 | 174 | /*! 175 | * @brief Gets the power value in mW, taking into account the 176 | * config settings and current LSB 177 | * @return power reading converted to milliwatts 178 | */ 179 | float Adafruit_INA219::getPower_mW() { 180 | float valueDec = getPower_raw(); 181 | valueDec *= ina219_powerMultiplier_mW; 182 | return valueDec; 183 | } 184 | 185 | /*! 186 | * @brief Configures to INA219 to be able to measure up to 32V and 2A 187 | * of current. Each unit of current corresponds to 100uA, and 188 | * each unit of power corresponds to 2mW. Counter overflow 189 | * occurs at 3.2A. 190 | * @note These calculations assume a 0.1 ohm resistor is present 191 | */ 192 | void Adafruit_INA219::setCalibration_32V_2A() { 193 | // By default we use a pretty huge range for the input voltage, 194 | // which probably isn't the most appropriate choice for system 195 | // that don't use a lot of power. But all of the calculations 196 | // are shown below if you want to change the settings. You will 197 | // also need to change any relevant register settings, such as 198 | // setting the VBUS_MAX to 16V instead of 32V, etc. 199 | 200 | // VBUS_MAX = 32V (Assumes 32V, can also be set to 16V) 201 | // VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 202 | // 0.04) RSHUNT = 0.1 (Resistor value in ohms) 203 | 204 | // 1. Determine max possible current 205 | // MaxPossible_I = VSHUNT_MAX / RSHUNT 206 | // MaxPossible_I = 3.2A 207 | 208 | // 2. Determine max expected current 209 | // MaxExpected_I = 2.0A 210 | 211 | // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) 212 | // MinimumLSB = MaxExpected_I/32767 213 | // MinimumLSB = 0.000061 (61uA per bit) 214 | // MaximumLSB = MaxExpected_I/4096 215 | // MaximumLSB = 0,000488 (488uA per bit) 216 | 217 | // 4. Choose an LSB between the min and max values 218 | // (Preferrably a roundish number close to MinLSB) 219 | // CurrentLSB = 0.0001 (100uA per bit) 220 | 221 | // 5. Compute the calibration register 222 | // Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) 223 | // Cal = 4096 (0x1000) 224 | 225 | ina219_calValue = 4096; 226 | 227 | // 6. Calculate the power LSB 228 | // PowerLSB = 20 * CurrentLSB 229 | // PowerLSB = 0.002 (2mW per bit) 230 | 231 | // 7. Compute the maximum current and shunt voltage values before overflow 232 | // 233 | // Max_Current = Current_LSB * 32767 234 | // Max_Current = 3.2767A before overflow 235 | // 236 | // If Max_Current > Max_Possible_I then 237 | // Max_Current_Before_Overflow = MaxPossible_I 238 | // Else 239 | // Max_Current_Before_Overflow = Max_Current 240 | // End If 241 | // 242 | // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT 243 | // Max_ShuntVoltage = 0.32V 244 | // 245 | // If Max_ShuntVoltage >= VSHUNT_MAX 246 | // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 247 | // Else 248 | // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage 249 | // End If 250 | 251 | // 8. Compute the Maximum Power 252 | // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX 253 | // MaximumPower = 3.2 * 32V 254 | // MaximumPower = 102.4W 255 | 256 | // Set multipliers to convert raw current/power values 257 | ina219_currentDivider_mA = 10; // Current LSB = 100uA per bit (1000/100 = 10) 258 | ina219_powerMultiplier_mW = 2; // Power LSB = 1mW per bit (2/1) 259 | 260 | // Set Calibration register to 'Cal' calculated above 261 | Adafruit_BusIO_Register calibration_reg = 262 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST); 263 | calibration_reg.write(ina219_calValue, 2); 264 | 265 | // Set Config register to take into account the settings above 266 | uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V | 267 | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | 268 | INA219_CONFIG_SADCRES_12BIT_1S_532US | 269 | INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; 270 | Adafruit_BusIO_Register config_reg = 271 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST); 272 | _success = config_reg.write(config, 2); 273 | } 274 | 275 | /*! 276 | * @brief Set power save mode according to parameters 277 | * @param on 278 | * boolean value 279 | */ 280 | void Adafruit_INA219::powerSave(bool on) { 281 | Adafruit_BusIO_Register config_reg = 282 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST); 283 | 284 | Adafruit_BusIO_RegisterBits mode_bits = 285 | Adafruit_BusIO_RegisterBits(&config_reg, 3, 0); 286 | if (on) { 287 | _success = mode_bits.write(INA219_CONFIG_MODE_POWERDOWN); 288 | } else { 289 | _success = mode_bits.write(INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS); 290 | } 291 | } 292 | 293 | /*! 294 | * @brief Configures to INA219 to be able to measure up to 32V and 1A 295 | * of current. Each unit of current corresponds to 40uA, and each 296 | * unit of power corresponds to 800uW. Counter overflow occurs at 297 | * 1.3A. 298 | * @note These calculations assume a 0.1 ohm resistor is present 299 | */ 300 | void Adafruit_INA219::setCalibration_32V_1A() { 301 | // By default we use a pretty huge range for the input voltage, 302 | // which probably isn't the most appropriate choice for system 303 | // that don't use a lot of power. But all of the calculations 304 | // are shown below if you want to change the settings. You will 305 | // also need to change any relevant register settings, such as 306 | // setting the VBUS_MAX to 16V instead of 32V, etc. 307 | 308 | // VBUS_MAX = 32V (Assumes 32V, can also be set to 16V) 309 | // VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04) 310 | // RSHUNT = 0.1 (Resistor value in ohms) 311 | 312 | // 1. Determine max possible current 313 | // MaxPossible_I = VSHUNT_MAX / RSHUNT 314 | // MaxPossible_I = 3.2A 315 | 316 | // 2. Determine max expected current 317 | // MaxExpected_I = 1.0A 318 | 319 | // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) 320 | // MinimumLSB = MaxExpected_I/32767 321 | // MinimumLSB = 0.0000305 (30.5uA per bit) 322 | // MaximumLSB = MaxExpected_I/4096 323 | // MaximumLSB = 0.000244 (244uA per bit) 324 | 325 | // 4. Choose an LSB between the min and max values 326 | // (Preferrably a roundish number close to MinLSB) 327 | // CurrentLSB = 0.0000400 (40uA per bit) 328 | 329 | // 5. Compute the calibration register 330 | // Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) 331 | // Cal = 10240 (0x2800) 332 | 333 | ina219_calValue = 10240; 334 | 335 | // 6. Calculate the power LSB 336 | // PowerLSB = 20 * CurrentLSB 337 | // PowerLSB = 0.0008 (800uW per bit) 338 | 339 | // 7. Compute the maximum current and shunt voltage values before overflow 340 | // 341 | // Max_Current = Current_LSB * 32767 342 | // Max_Current = 1.31068A before overflow 343 | // 344 | // If Max_Current > Max_Possible_I then 345 | // Max_Current_Before_Overflow = MaxPossible_I 346 | // Else 347 | // Max_Current_Before_Overflow = Max_Current 348 | // End If 349 | // 350 | // ... In this case, we're good though since Max_Current is less than 351 | // MaxPossible_I 352 | // 353 | // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT 354 | // Max_ShuntVoltage = 0.131068V 355 | // 356 | // If Max_ShuntVoltage >= VSHUNT_MAX 357 | // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 358 | // Else 359 | // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage 360 | // End If 361 | 362 | // 8. Compute the Maximum Power 363 | // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX 364 | // MaximumPower = 1.31068 * 32V 365 | // MaximumPower = 41.94176W 366 | 367 | // Set multipliers to convert raw current/power values 368 | ina219_currentDivider_mA = 25; // Current LSB = 40uA per bit (1000/40 = 25) 369 | ina219_powerMultiplier_mW = 0.8f; // Power LSB = 800uW per bit 370 | 371 | // Set Calibration register to 'Cal' calculated above 372 | Adafruit_BusIO_Register calibration_reg = 373 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST); 374 | calibration_reg.write(ina219_calValue, 2); 375 | 376 | // Set Config register to take into account the settings above 377 | uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V | 378 | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | 379 | INA219_CONFIG_SADCRES_12BIT_1S_532US | 380 | INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; 381 | Adafruit_BusIO_Register config_reg = 382 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST); 383 | _success = config_reg.write(config, 2); 384 | } 385 | 386 | /*! 387 | * @brief set device to alibration which uses the highest precision for 388 | * current measurement (0.1mA), at the expense of 389 | * only supporting 16V at 400mA max. 390 | */ 391 | void Adafruit_INA219::setCalibration_16V_400mA() { 392 | 393 | // Calibration which uses the highest precision for 394 | // current measurement (0.1mA), at the expense of 395 | // only supporting 16V at 400mA max. 396 | 397 | // VBUS_MAX = 16V 398 | // VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV) 399 | // RSHUNT = 0.1 (Resistor value in ohms) 400 | 401 | // 1. Determine max possible current 402 | // MaxPossible_I = VSHUNT_MAX / RSHUNT 403 | // MaxPossible_I = 0.4A 404 | 405 | // 2. Determine max expected current 406 | // MaxExpected_I = 0.4A 407 | 408 | // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) 409 | // MinimumLSB = MaxExpected_I/32767 410 | // MinimumLSB = 0.0000122 (12uA per bit) 411 | // MaximumLSB = MaxExpected_I/4096 412 | // MaximumLSB = 0.0000977 (98uA per bit) 413 | 414 | // 4. Choose an LSB between the min and max values 415 | // (Preferrably a roundish number close to MinLSB) 416 | // CurrentLSB = 0.00005 (50uA per bit) 417 | 418 | // 5. Compute the calibration register 419 | // Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) 420 | // Cal = 8192 (0x2000) 421 | 422 | ina219_calValue = 8192; 423 | 424 | // 6. Calculate the power LSB 425 | // PowerLSB = 20 * CurrentLSB 426 | // PowerLSB = 0.001 (1mW per bit) 427 | 428 | // 7. Compute the maximum current and shunt voltage values before overflow 429 | // 430 | // Max_Current = Current_LSB * 32767 431 | // Max_Current = 1.63835A before overflow 432 | // 433 | // If Max_Current > Max_Possible_I then 434 | // Max_Current_Before_Overflow = MaxPossible_I 435 | // Else 436 | // Max_Current_Before_Overflow = Max_Current 437 | // End If 438 | // 439 | // Max_Current_Before_Overflow = MaxPossible_I 440 | // Max_Current_Before_Overflow = 0.4 441 | // 442 | // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT 443 | // Max_ShuntVoltage = 0.04V 444 | // 445 | // If Max_ShuntVoltage >= VSHUNT_MAX 446 | // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 447 | // Else 448 | // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage 449 | // End If 450 | // 451 | // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 452 | // Max_ShuntVoltage_Before_Overflow = 0.04V 453 | 454 | // 8. Compute the Maximum Power 455 | // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX 456 | // MaximumPower = 0.4 * 16V 457 | // MaximumPower = 6.4W 458 | 459 | // Set multipliers to convert raw current/power values 460 | ina219_currentDivider_mA = 20; // Current LSB = 50uA per bit (1000/50 = 20) 461 | ina219_powerMultiplier_mW = 1.0f; // Power LSB = 1mW per bit 462 | 463 | // Set Calibration register to 'Cal' calculated above 464 | Adafruit_BusIO_Register calibration_reg = 465 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST); 466 | calibration_reg.write(ina219_calValue, 2); 467 | // Set Config register to take into account the settings above 468 | uint16_t config = INA219_CONFIG_BVOLTAGERANGE_16V | 469 | INA219_CONFIG_GAIN_1_40MV | INA219_CONFIG_BADCRES_12BIT | 470 | INA219_CONFIG_SADCRES_12BIT_1S_532US | 471 | INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; 472 | 473 | Adafruit_BusIO_Register config_reg = 474 | Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST); 475 | _success = config_reg.write(config, 2); 476 | } 477 | 478 | /*! 479 | * @brief Provides the the underlying return value from the last operation 480 | * called on the device. 481 | * @return true: Last operation was successful false: Last operation failed 482 | * @note For function calls that have intermediary device operations, 483 | * e.g. calibration before read/write, only the final operation's 484 | * result is stored. 485 | */ 486 | bool Adafruit_INA219::success() { return _success; } 487 | -------------------------------------------------------------------------------- /Adafruit_INA219.h: -------------------------------------------------------------------------------- 1 | /*! 2 | * @file Adafruit_INA219.h 3 | * 4 | * This is a library for the Adafruit INA219 breakout board 5 | * ----> https://www.adafruit.com/product/904 6 | * 7 | * Adafruit invests time and resources providing this open source code, 8 | * please support Adafruit and open-source hardware by purchasing 9 | * products from Adafruit! 10 | * 11 | * Written by Bryan Siepert and Kevin "KTOWN" Townsend for Adafruit Industries. 12 | * 13 | * BSD license, all text here must be included in any redistribution. 14 | * 15 | */ 16 | 17 | #ifndef _LIB_ADAFRUIT_INA219_ 18 | #define _LIB_ADAFRUIT_INA219_ 19 | 20 | #include "Arduino.h" 21 | #include 22 | #include 23 | #include 24 | 25 | /** calculated I2C address: 0 = GND, 1 = V+ **/ 26 | /* The address is controlled by the A0 and A1 inputs on the INA219: 27 | * 28 | * Calculated address: b100ABCD 29 | * A0 controls C and D: GND = 00, V+ = 01, SDA = 10, SCL = 11 30 | * A1 controls A and B: GND = 00, V+ = 01, SDA = 10, SCL = 11 31 | * 32 | * E.g. if A0 is tied to ground and A1 is tied to V+, 33 | * the resulting address is b1000100 = 0x44 34 | * 35 | * SDA and SCL options aren't implemented. 36 | */ 37 | #define INA219_CALC_ADDRESS(INA_ADDR0, INA_ADDR1) \ 38 | (0x40 | (INA_ADDR0 != 0 ? 0x01 : 0x00) | (INA_ADDR1 != 0 ? 0x04 : 0x00)) 39 | 40 | /** default I2C address **/ 41 | #define INA219_ADDRESS (0x40) // 1000000 (A0+A1=GND) 42 | 43 | /** read **/ 44 | #define INA219_READ (0x01) 45 | 46 | /*========================================================================= 47 | CONFIG REGISTER (R/W) 48 | **************************************************************************/ 49 | 50 | /** config register address **/ 51 | #define INA219_REG_CONFIG (0x00) 52 | 53 | /** reset bit **/ 54 | #define INA219_CONFIG_RESET (0x8000) // Reset Bit 55 | 56 | /** mask for bus voltage range **/ 57 | #define INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000) // Bus Voltage Range Mask 58 | 59 | /** bus voltage range values **/ 60 | enum { 61 | INA219_CONFIG_BVOLTAGERANGE_16V = (0x0000), // 0-16V Range 62 | INA219_CONFIG_BVOLTAGERANGE_32V = (0x2000), // 0-32V Range 63 | }; 64 | 65 | /** mask for gain bits **/ 66 | #define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask 67 | 68 | /** values for gain bits **/ 69 | enum { 70 | INA219_CONFIG_GAIN_1_40MV = (0x0000), // Gain 1, 40mV Range 71 | INA219_CONFIG_GAIN_2_80MV = (0x0800), // Gain 2, 80mV Range 72 | INA219_CONFIG_GAIN_4_160MV = (0x1000), // Gain 4, 160mV Range 73 | INA219_CONFIG_GAIN_8_320MV = (0x1800), // Gain 8, 320mV Range 74 | }; 75 | 76 | /** mask for bus ADC resolution bits **/ 77 | #define INA219_CONFIG_BADCRES_MASK (0x0780) 78 | 79 | /** values for bus ADC resolution **/ 80 | enum { 81 | INA219_CONFIG_BADCRES_9BIT = (0x0000), // 9-bit bus res = 0..511 82 | INA219_CONFIG_BADCRES_10BIT = (0x0080), // 10-bit bus res = 0..1023 83 | INA219_CONFIG_BADCRES_11BIT = (0x0100), // 11-bit bus res = 0..2047 84 | INA219_CONFIG_BADCRES_12BIT = (0x0180), // 12-bit bus res = 0..4097 85 | INA219_CONFIG_BADCRES_12BIT_2S_1060US = 86 | (0x0480), // 2 x 12-bit bus samples averaged together 87 | INA219_CONFIG_BADCRES_12BIT_4S_2130US = 88 | (0x0500), // 4 x 12-bit bus samples averaged together 89 | INA219_CONFIG_BADCRES_12BIT_8S_4260US = 90 | (0x0580), // 8 x 12-bit bus samples averaged together 91 | INA219_CONFIG_BADCRES_12BIT_16S_8510US = 92 | (0x0600), // 16 x 12-bit bus samples averaged together 93 | INA219_CONFIG_BADCRES_12BIT_32S_17MS = 94 | (0x0680), // 32 x 12-bit bus samples averaged together 95 | INA219_CONFIG_BADCRES_12BIT_64S_34MS = 96 | (0x0700), // 64 x 12-bit bus samples averaged together 97 | INA219_CONFIG_BADCRES_12BIT_128S_69MS = 98 | (0x0780), // 128 x 12-bit bus samples averaged together 99 | 100 | }; 101 | 102 | /** mask for shunt ADC resolution bits **/ 103 | #define INA219_CONFIG_SADCRES_MASK \ 104 | (0x0078) // Shunt ADC Resolution and Averaging Mask 105 | 106 | /** values for shunt ADC resolution **/ 107 | enum { 108 | INA219_CONFIG_SADCRES_9BIT_1S_84US = (0x0000), // 1 x 9-bit shunt sample 109 | INA219_CONFIG_SADCRES_10BIT_1S_148US = (0x0008), // 1 x 10-bit shunt sample 110 | INA219_CONFIG_SADCRES_11BIT_1S_276US = (0x0010), // 1 x 11-bit shunt sample 111 | INA219_CONFIG_SADCRES_12BIT_1S_532US = (0x0018), // 1 x 12-bit shunt sample 112 | INA219_CONFIG_SADCRES_12BIT_2S_1060US = 113 | (0x0048), // 2 x 12-bit shunt samples averaged together 114 | INA219_CONFIG_SADCRES_12BIT_4S_2130US = 115 | (0x0050), // 4 x 12-bit shunt samples averaged together 116 | INA219_CONFIG_SADCRES_12BIT_8S_4260US = 117 | (0x0058), // 8 x 12-bit shunt samples averaged together 118 | INA219_CONFIG_SADCRES_12BIT_16S_8510US = 119 | (0x0060), // 16 x 12-bit shunt samples averaged together 120 | INA219_CONFIG_SADCRES_12BIT_32S_17MS = 121 | (0x0068), // 32 x 12-bit shunt samples averaged together 122 | INA219_CONFIG_SADCRES_12BIT_64S_34MS = 123 | (0x0070), // 64 x 12-bit shunt samples averaged together 124 | INA219_CONFIG_SADCRES_12BIT_128S_69MS = 125 | (0x0078), // 128 x 12-bit shunt samples averaged together 126 | }; 127 | 128 | /** mask for operating mode bits **/ 129 | #define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask 130 | 131 | /** values for operating mode **/ 132 | enum { 133 | INA219_CONFIG_MODE_POWERDOWN = 0x00, /**< power down */ 134 | INA219_CONFIG_MODE_SVOLT_TRIGGERED = 0x01, /**< shunt voltage triggered */ 135 | INA219_CONFIG_MODE_BVOLT_TRIGGERED = 0x02, /**< bus voltage triggered */ 136 | INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED = 137 | 0x03, /**< shunt and bus voltage triggered */ 138 | INA219_CONFIG_MODE_ADCOFF = 0x04, /**< ADC off */ 139 | INA219_CONFIG_MODE_SVOLT_CONTINUOUS = 0x05, /**< shunt voltage continuous */ 140 | INA219_CONFIG_MODE_BVOLT_CONTINUOUS = 0x06, /**< bus voltage continuous */ 141 | INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS = 142 | 0x07, /**< shunt and bus voltage continuous */ 143 | }; 144 | 145 | /** shunt voltage register **/ 146 | #define INA219_REG_SHUNTVOLTAGE (0x01) 147 | 148 | /** bus voltage register **/ 149 | #define INA219_REG_BUSVOLTAGE (0x02) 150 | 151 | /** power register **/ 152 | #define INA219_REG_POWER (0x03) 153 | 154 | /** current register **/ 155 | #define INA219_REG_CURRENT (0x04) 156 | 157 | /** calibration register **/ 158 | #define INA219_REG_CALIBRATION (0x05) 159 | 160 | /*! 161 | * @brief Class that stores state and functions for interacting with INA219 162 | * current/power monitor IC 163 | */ 164 | class Adafruit_INA219 { 165 | public: 166 | Adafruit_INA219(uint8_t addr = INA219_ADDRESS); 167 | ~Adafruit_INA219(); 168 | bool begin(TwoWire *theWire = &Wire); 169 | void setCalibration_32V_2A(); 170 | void setCalibration_32V_1A(); 171 | void setCalibration_16V_400mA(); 172 | float getBusVoltage_V(); 173 | float getShuntVoltage_mV(); 174 | float getCurrent_mA(); 175 | float getPower_mW(); 176 | void powerSave(bool on); 177 | bool success(); 178 | 179 | private: 180 | Adafruit_I2CDevice *i2c_dev = NULL; 181 | 182 | bool _success; 183 | 184 | uint8_t ina219_i2caddr = -1; 185 | uint32_t ina219_calValue; 186 | // The following multipliers are used to convert raw current and power 187 | // values to mA and mW, taking into account the current config settings 188 | uint32_t ina219_currentDivider_mA; 189 | float ina219_powerMultiplier_mW; 190 | 191 | void init(); 192 | int16_t getBusVoltage_raw(); 193 | int16_t getShuntVoltage_raw(); 194 | int16_t getCurrent_raw(); 195 | int16_t getPower_raw(); 196 | }; 197 | 198 | #endif 199 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adafruit INA219 Library [![Build Status](https://github.com/adafruit/Adafruit_INA219/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_INA219/actions)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_INA219/html/index.html) 2 | 3 | 4 | 5 | This is a library for the Adafruit INA219 high side DC current sensor boards: 6 | * https://www.adafruit.com/products/904 7 | * https://www.adafruit.com/product/3650 8 | 9 | Check out the links above for our tutorials and wiring diagrams. This chip uses I2C to communicate. 10 | 11 | To install, use the Arduino Library Manager and search for 'Adafruit INA219' and install the library. 12 | 13 | Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! 14 | 15 | Written by Ktown for Adafruit Industries. 16 | MIT license, all text above must be included in any redistribution 17 | -------------------------------------------------------------------------------- /assets/board.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/Adafruit_INA219/ce59837b4a8feccd767c67d26806a0bd89d1d8eb/assets/board.jpg -------------------------------------------------------------------------------- /code-of-conduct.md: -------------------------------------------------------------------------------- 1 | # Adafruit Community Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and leaders pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level or type of 9 | experience, education, socio-economic status, nationality, personal appearance, 10 | race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | We are committed to providing a friendly, safe and welcoming environment for 15 | all. 16 | 17 | Examples of behavior that contributes to creating a positive environment 18 | include: 19 | 20 | * Be kind and courteous to others 21 | * Using welcoming and inclusive language 22 | * Being respectful of differing viewpoints and experiences 23 | * Collaborating with other community members 24 | * Gracefully accepting constructive criticism 25 | * Focusing on what is best for the community 26 | * Showing empathy towards other community members 27 | 28 | Examples of unacceptable behavior by participants include: 29 | 30 | * The use of sexualized language or imagery and sexual attention or advances 31 | * The use of inappropriate images, including in a community member's avatar 32 | * The use of inappropriate language, including in a community member's nickname 33 | * Any spamming, flaming, baiting or other attention-stealing behavior 34 | * Excessive or unwelcome helping; answering outside the scope of the question 35 | asked 36 | * Trolling, insulting/derogatory comments, and personal or political attacks 37 | * Public or private harassment 38 | * Publishing others' private information, such as a physical or electronic 39 | address, without explicit permission 40 | * Other conduct which could reasonably be considered inappropriate 41 | 42 | The goal of the standards and moderation guidelines outlined here is to build 43 | and maintain a respectful community. We ask that you don’t just aim to be 44 | "technically unimpeachable", but rather try to be your best self. 45 | 46 | We value many things beyond technical expertise, including collaboration and 47 | supporting others within our community. Providing a positive experience for 48 | other community members can have a much more significant impact than simply 49 | providing the correct answer. 50 | 51 | ## Our Responsibilities 52 | 53 | Project leaders are responsible for clarifying the standards of acceptable 54 | behavior and are expected to take appropriate and fair corrective action in 55 | response to any instances of unacceptable behavior. 56 | 57 | Project leaders have the right and responsibility to remove, edit, or 58 | reject messages, comments, commits, code, issues, and other contributions 59 | that are not aligned to this Code of Conduct, or to ban temporarily or 60 | permanently any community member for other behaviors that they deem 61 | inappropriate, threatening, offensive, or harmful. 62 | 63 | ## Moderation 64 | 65 | Instances of behaviors that violate the Adafruit Community Code of Conduct 66 | may be reported by any member of the community. Community members are 67 | encouraged to report these situations, including situations they witness 68 | involving other community members. 69 | 70 | You may report in the following ways: 71 | 72 | In any situation, you may send an email to . 73 | 74 | On the Adafruit Discord, you may send an open message from any channel 75 | to all Community Helpers by tagging @community helpers. You may also send an 76 | open message from any channel, or a direct message to @kattni#1507, 77 | @tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or 78 | @Andon#8175. 79 | 80 | Email and direct message reports will be kept confidential. 81 | 82 | In situations on Discord where the issue is particularly egregious, possibly 83 | illegal, requires immediate action, or violates the Discord terms of service, 84 | you should also report the message directly to Discord. 85 | 86 | These are the steps for upholding our community’s standards of conduct. 87 | 88 | 1. Any member of the community may report any situation that violates the 89 | Adafruit Community Code of Conduct. All reports will be reviewed and 90 | investigated. 91 | 2. If the behavior is an egregious violation, the community member who 92 | committed the violation may be banned immediately, without warning. 93 | 3. Otherwise, moderators will first respond to such behavior with a warning. 94 | 4. Moderators follow a soft "three strikes" policy - the community member may 95 | be given another chance, if they are receptive to the warning and change their 96 | behavior. 97 | 5. If the community member is unreceptive or unreasonable when warned by a 98 | moderator, or the warning goes unheeded, they may be banned for a first or 99 | second offense. Repeated offenses will result in the community member being 100 | banned. 101 | 102 | ## Scope 103 | 104 | This Code of Conduct and the enforcement policies listed above apply to all 105 | Adafruit Community venues. This includes but is not limited to any community 106 | spaces (both public and private), the entire Adafruit Discord server, and 107 | Adafruit GitHub repositories. Examples of Adafruit Community spaces include 108 | but are not limited to meet-ups, audio chats on the Adafruit Discord, or 109 | interaction at a conference. 110 | 111 | This Code of Conduct applies both within project spaces and in public spaces 112 | when an individual is representing the project or its community. As a community 113 | member, you are representing our community, and are expected to behave 114 | accordingly. 115 | 116 | ## Attribution 117 | 118 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 119 | version 1.4, available at 120 | , 121 | and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). 122 | 123 | For other projects adopting the Adafruit Community Code of 124 | Conduct, please contact the maintainers of those projects for enforcement. 125 | If you wish to use this code of conduct for your own project, consider 126 | explicitly mentioning your moderation policy or making a copy with your 127 | own moderation policy so as to avoid confusion. 128 | -------------------------------------------------------------------------------- /examples/getcurrent/getcurrent.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | Adafruit_INA219 ina219; 5 | 6 | 7 | void setup(void) 8 | { 9 | Serial.begin(115200); 10 | while (!Serial) { 11 | // will pause Zero, Leonardo, etc until serial console opens 12 | delay(1); 13 | } 14 | 15 | Serial.println("Hello!"); 16 | 17 | // Initialize the INA219. 18 | // By default the initialization will use the largest range (32V, 2A). However 19 | // you can call a setCalibration function to change this range (see comments). 20 | if (! ina219.begin()) { 21 | Serial.println("Failed to find INA219 chip"); 22 | while (1) { delay(10); } 23 | } 24 | // To use a slightly lower 32V, 1A range (higher precision on amps): 25 | //ina219.setCalibration_32V_1A(); 26 | // Or to use a lower 16V, 400mA range (higher precision on volts and amps): 27 | //ina219.setCalibration_16V_400mA(); 28 | 29 | Serial.println("Measuring voltage and current with INA219 ..."); 30 | } 31 | 32 | void loop(void) 33 | { 34 | float shuntvoltage = 0; 35 | float busvoltage = 0; 36 | float current_mA = 0; 37 | float loadvoltage = 0; 38 | float power_mW = 0; 39 | 40 | shuntvoltage = ina219.getShuntVoltage_mV(); 41 | busvoltage = ina219.getBusVoltage_V(); 42 | current_mA = ina219.getCurrent_mA(); 43 | power_mW = ina219.getPower_mW(); 44 | loadvoltage = busvoltage + (shuntvoltage / 1000); 45 | 46 | Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V"); 47 | Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV"); 48 | Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V"); 49 | Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA"); 50 | Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW"); 51 | Serial.println(""); 52 | 53 | delay(2000); 54 | } 55 | -------------------------------------------------------------------------------- /examples/ina_poweroled/ina_poweroled.ino: -------------------------------------------------------------------------------- 1 | // Feather Power Meter 2 | // 3 | // Small Feather-based power monitor using an INA219 breakout and 4 | // monochrome OLED display. 5 | // 6 | // Author: Tony DiCola (modded by ladyada) 7 | // 8 | // Released under a MIT license: http://opensource.org/licenses/MIT 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | // Configure orientation of the display. 18 | // 0 = none, 1 = 90 degrees clockwise, 2 = 180 degrees, 3 = 270 degrees CW 19 | #define ROTATION 0 20 | 21 | #define NEO_PIN 6 22 | 23 | // Create NeoPixel, OLED and INA219 globals. 24 | Adafruit_SSD1306 display; 25 | Adafruit_INA219 ina219; 26 | Adafruit_NeoPixel pixels = Adafruit_NeoPixel(144, NEO_PIN, NEO_GRB + NEO_KHZ800); 27 | 28 | // Keep track of total time and milliamp measurements for milliamp-hour computation. 29 | uint32_t total_sec = 0; 30 | float total_mA = 0.0; 31 | 32 | uint8_t counter = 0; 33 | void pixel_show_and_powerupdate() { 34 | pixels.show(); 35 | if (counter == 0) { 36 | update_power_display(); 37 | } else { 38 | counter = (counter+1) % 10 ; 39 | } 40 | } 41 | 42 | void setup() { 43 | Serial.begin(115200); 44 | while (!Serial) { 45 | // will pause Zero, Leonardo, etc until serial console opens 46 | delay(1); 47 | } 48 | pixels.begin(); 49 | pixels.show(); // Initialize all pixels to 'off' 50 | // Try to initialize the INA219 51 | if (! ina219.begin()) { 52 | Serial.println("Failed to find INA219 chip"); 53 | while (1) { delay(10); } 54 | } 55 | // By default the INA219 will be calibrated with a range of 32V, 2A. 56 | // However uncomment one of the below to change the range. A smaller 57 | // range can't measure as large of values but will measure with slightly 58 | // better precision. 59 | //ina219.setCalibration_32V_1A(); 60 | //ina219.setCalibration_16V_400mA(); 61 | 62 | // Setup the OLED display. 63 | display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 64 | Wire.setClock(400000); 65 | 66 | // Clear the display. 67 | display.clearDisplay(); 68 | display.display(); 69 | 70 | // Set rotation. 71 | display.setRotation(ROTATION); 72 | 73 | // Set text size and color. 74 | display.setTextSize(2); 75 | display.setTextColor(WHITE); 76 | } 77 | 78 | void loop() { 79 | // Some example procedures showing how to display to the pixels: 80 | colorWipe(pixels.Color(255, 0, 0), 50); // Red 81 | colorWipe(pixels.Color(0, 255, 0), 50); // Green 82 | colorWipe(pixels.Color(0, 0, 255), 50); // Blue 83 | //colorWipe(pixels.Color(0, 0, 0, 255), 50); // White RGBW 84 | // Send a theater pixel chase in... 85 | theaterChase(pixels.Color(127, 127, 127), 50); // White 86 | theaterChase(pixels.Color(127, 0, 0), 50); // Red 87 | theaterChase(pixels.Color(0, 0, 127), 50); // Blue 88 | 89 | rainbow(20); 90 | rainbowCycle(20); 91 | theaterChaseRainbow(50); 92 | } 93 | 94 | 95 | // Fill the dots one after the other with a color 96 | void colorWipe(uint32_t c, uint8_t wait) { 97 | for(uint16_t i=0; i maxWidth) { 238 | // Fill width with dashes (and add extra dash for negative values); 239 | for (int i=0; i < maxWidth; ++i) { 240 | display.print('-'); 241 | } 242 | if (value < 0.0) { 243 | display.print('-'); 244 | } 245 | return; 246 | } 247 | 248 | // Compute actual precision for printed value based on space left after 249 | // printing digits and decimal point. Clamp within 0 to desired precision. 250 | int actualPrecision = constrain(maxWidth-digits-1, 0, precision); 251 | 252 | // Compute how much padding to add to right justify. 253 | int padding = maxWidth-digits-1-actualPrecision; 254 | for (int i=0; i < padding; ++i) { 255 | display.print(' '); 256 | } 257 | 258 | // Finally, print the value! 259 | display.print(value, actualPrecision); 260 | } 261 | 262 | 263 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit INA219 2 | version=1.2.3 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=INA219 Current Sensor 6 | paragraph=INA219 Current Sensor 7 | category=Sensors 8 | url=https://github.com/adafruit/Adafruit_INA219 9 | architectures=* 10 | depends=Adafruit NeoPixel, Adafruit GFX Library, Adafruit SSD1306, Adafruit BusIO 11 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2012, Adafruit Industries 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holders nor the 14 | names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | --------------------------------------------------------------------------------