npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ina226

v3.0.0

Published

Node.js module to read values from the INA226 bi-directional current and power monitor.

Downloads

82

Readme

ina226

NPM version Downloads

NPM

Node.js module to read values from the INA226 bi-directional current and power monitor.

For more information about the INA226 please consult the data sheet from Texas Instruments.

Features

  • Read shunt and bus voltages.
  • Calculate current and power.
  • Read the value from a register.
  • Write a value to a register.

Installation

Make sure you are using Node.js v8.x or higher.

npm install ina226

This module is written in TypeScript and typings are included.

Example

Note that you need to construct the i2c-bus object and pass it in to the INA226 class.

The example blow can be found in the examples directory of this package together with a TypeScript example.

// Require the ina226 module
var INA226 = require('ina226').INA226;

// Require the i2c-bus module and open the bus
var i2cBus = require('i2c-bus').openSync(1);

// Define the address of the INA226 and the shunt resistance value
var addr = 0x40;
var rShunt = 0.1;

// Init a new INA226
var ina = new INA226(i2cBus, addr, rShunt);

// Write to the Configuration Register
// 0x4427 means 16 averages, 1.1ms conversion time, shunt and bus continuous
ina.writeRegister(CONFIGURATION_REGISTER, 0x4427)
.then(function(){
  console.log('Configuration written');
});

// Read the actual bus voltage
ina.readBusVoltage()
.then(function(busVoltage){
  console.log('Bus Voltage: ' + busVoltage.toFixed(2) + 'V');
});

// Read the actual shunt voltage
ina.readShuntVoltage()
.then(function(shuntVoltage){
  console.log('Shunt Voltage: ' + shuntVoltage.toFixed(5) + 'V');
});

// Read the actual shunt voltage and calculate the current
ina.readShuntVoltage()
.then(function(){
  var current = ina.calcCurrent();
  console.log('Current: ' + current.toFixed(2) + 'A');
})

// Then read the actual bus voltage and calculate the power
.then(ina.readBusVoltage.bind(ina))
.then(function(){
  var power = ina.calcPower();
  console.log('Power: ' + power.toFixed(2) + 'W');
});

API

new INA226(i2cBus, address, rShunt)

constructor(i2cBus:I2cBus, address:number, rShunt:number=0.1)

Constructor for a new INA226 instance.

  • i2cBus - Instance of an opened i2c-bus.
  • address - The address of the INA226 IC.
  • rShunt - The shunt resistance value. Defaults to 0.1 Ohm.

Note that you need to construct the i2c-bus object and pass it in to the module.

writeRegister(register, value)

writeRegister(register:number, value:number):Promise<{}>

Writes a value to a specific register. Returns a Promise which will be resolves if the value is written, or rejected in case of an error.

  • register - The register address.
  • value - The value. Should be 16bit integer.

readRegister(register)

readRegister(register:number):Promise<number>

Reads a value from a specific register. Returns a Promise which will be resolved with the read value, or rejected in case of an error.

  • register - The register address.

readBusVoltage()

readBusVoltage():Promise<number>

Reads the actual bus voltage. Returns a Promise which will be resolved with the bus voltage, or rejected in case of an error.

readShuntVoltage()

readShuntVoltage():Promise<number>

Reads the actual shunt voltage. Returns a Promise which will be resolved with the shunt voltage, or rejected in case of an error.

calcCurrent(shuntVoltage)

calcCurrent(shuntVoltage?:number):number

Calculates the current in Ampere based on the shunt voltage an the shunt resistance value.

  • shuntVoltage - Optional. The shunt voltage which is used for the calculation. Defaults to the last read shunt voltage.

calcPower(busVoltage, shuntVoltage)

calcPower(busVoltage?:number, shuntVoltage?:number):number

Calculates the power in Watt based on the bus voltage, the shunt voltage and the shunt resistance value.

  • busVoltage - Optional. The bus voltage which is used for the calculation. Defaults to the last read bus voltage.
  • shuntVoltage - Optional. The shunt voltage which is used for the calculation. Defaults to the last read shunt voltage.

Exported constants

The register addresses are exported as constants.

| Constant | Value | |---|---| | CONFIGURATION_REGISTER | 0x00 | | SHUNT_VOLTAGE_REGISTER | 0x01 | | BUS_VOLTAGE_REGISTER | 0x02 | | POWER_REGISTER | 0x03 | | CURRENT_REGISTER | 0x04 | | CALIBRATION_REGISTER | 0x05 | | MASK_ENABLE_REGISTER | 0x06 | | ALERT_LIMIT_REGISTER | 0x07 | | MANUFACTOR_ID_REGISTER | 0xFE | | DIE_ID_REGISTER | 0xFF |

License

Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)

Copyright (c) 2017-2021 Peter Müller [email protected] https://crycode.de/