ina219

package
v0.37.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RegConfig       uint8 = 0x0
	RegShuntVoltage uint8 = 0x1
	RegBusVoltage   uint8 = 0x2
	RegPower        uint8 = 0x3
	RegCurrent      uint8 = 0x4
	RegCalibration  uint8 = 0x5
)
View Source
const Address = 0x40

The default I2C address for this device.

Variables

View Source
var (
	// Config32V2A is a configuration for a 32V 2A range.
	Config32V2A = Config{
		BusVoltageRange: Range32V,
		PGA:             PGA8,
		BusADC:          ADC12,
		ShuntADC:        SADC12,
		Mode:            ModeContShuntBus,
		Calibration:     Calibration16V400mA,
		CurrentDivider:  10.0,
		PowerMultiplier: 2.0,
	}

	// Config32V1A is a configuration for a 32V 1A range.
	Config32V1A = Config{
		BusVoltageRange: Range32V,
		PGA:             PGA8,
		BusADC:          ADC12,
		ShuntADC:        SADC12,
		Mode:            ModeContShuntBus,
		Calibration:     Calibration32V1A,
		CurrentDivider:  25.0,
		PowerMultiplier: 0.8,
	}

	// Config16V400mA is a configuration for a 16V 400mA range.
	Config16V400mA = Config{
		BusVoltageRange: Range16V,
		PGA:             PGA1,
		BusADC:          ADC12,
		ShuntADC:        SADC12,
		Mode:            ModeContShuntBus,
		Calibration:     Calibration16V400mA,
		CurrentDivider:  20.0,
		PowerMultiplier: 1.0,
	}
)

Configurations from https://github.com/adafruit/Adafruit_INA219/blob/master/Adafruit_INA219.cpp

Functions

func ModeTriggered

func ModeTriggered(m Mode) bool

ModeTriggered returns true if the mode is a triggered mode.

Types

type BusADC

type BusADC int8

BusADC is the bus ADC resolution.

const (
	ADC9  BusADC = 0 // 9-bit
	ADC10 BusADC = 1 // 10-bit
	ADC11 BusADC = 2 // 11-bit
	ADC12 BusADC = 3 // 12-bit
)

func (BusADC) RegisterValue

func (b BusADC) RegisterValue() uint16

type BusVoltageRange

type BusVoltageRange int8

BusVoltageRange is the bus voltage range.

const (
	Range16V BusVoltageRange = 0 // 0-16V
	Range32V BusVoltageRange = 1 // 0-32V
)

func (BusVoltageRange) RegisterValue

func (r BusVoltageRange) RegisterValue() uint16

type Calibration

type Calibration uint16

Calibration is the calibration register for the INA219. Values from: https://github.com/adafruit/Adafruit_INA219/blob/master/Adafruit_INA219.cpp

const (
	Calibration32V2A    Calibration = 4096
	Calibration32V1A    Calibration = 10240
	Calibration16V400mA Calibration = 8192
)

func (Calibration) RegisterValue

func (c Calibration) RegisterValue() uint16

type Config

type Config struct {
	// BusVoltageRange sets the bus voltage range.
	BusVoltageRange BusVoltageRange

	// PGA sets the programmable gain amplifier.
	PGA PGA

	// BusADC sets the bus ADC resolution.
	BusADC BusADC

	// ShuntADC sets the shunt ADC resolution.
	ShuntADC ShuntADC

	// Mode sets the operating mode.
	Mode Mode

	// Calibration sets the calibration value for the expected
	// voltage and current values.
	Calibration Calibration

	// 1000 / uA per bit
	CurrentDivider float32

	// 1mW per bit
	PowerMultiplier float32
}

func NewConfig

func NewConfig(config int16, calibration int16) Config

Generate a new configuration from a register value.

func (*Config) RegisterValue

func (c *Config) RegisterValue() uint16

RegisterValue returns the register value of the configuration.

type Device

type Device struct {
	Address uint16
	// contains filtered or unexported fields
}

An INA219 device.

func New

func New(bus drivers.I2C) Device

Create a new INA219 device with the default configuration and the given I2C bus at the default address.

Set Address after New to change the address.

Call Configure after New to write the configuration to the device. If you don't call Configure, the device may have a different configuration and the power divider and current multiplier are probably wrong.

func (*Device) BusVoltage

func (d *Device) BusVoltage() (voltage int16, err error)

BusVoltage reads the "bus" voltage in millivolts.

It returns an error if the value is invalid due to overflow or if the conversion is not ready yet. In a continuous mode there should always be a measurement available after the device is ready. See above notes on Trigger.

func (*Device) Configure

func (d *Device) Configure() (err error)

Write the current configuration to the device.

func (*Device) Current

func (d *Device) Current() (current float32, err error)

Current reads the current in milliamps.

func (*Device) Measurements

func (d *Device) Measurements() (
	busVoltage int16,
	shuntVoltage int16,
	current float32,
	power float32,
	err error,
)

Measurements reads the bus voltage, shunt voltage, current, and power from the device.

func (*Device) Power

func (d *Device) Power() (power float32, err error)

Power reads the power in milliwatts.

func (*Device) ReadConfig

func (d *Device) ReadConfig() (config Config, err error)

Read the configuration from the device.

func (*Device) ReadRegister

func (d *Device) ReadRegister(reg uint8) (val int16, err error)

Read a register from the device.

func (*Device) SetConfig

func (d *Device) SetConfig(config Config)

Set the configuration for the device. This only changes the configuration in memory, not on the device. Call Configure to write the configuration to the device.

func (*Device) ShuntVoltage

func (d *Device) ShuntVoltage() (voltage int16, err error)

ShuntVoltage reads the "shunt" voltage in 100ths of a millivolt.

func (*Device) Trigger

func (d *Device) Trigger() (err error)

Trigger a conversion. This is only necessary if the device is in trigger mode. In continuous mode (the default), the device will automatically trigger conversions and this has no effect. See config.go.

Triggering a conversion or reading the "power" register resets the conversion ready bit.

func (*Device) WriteRegister

func (d *Device) WriteRegister(reg uint8, val uint16) error

Write to a register on the device.

type ErrConfigMismatch

type ErrConfigMismatch struct{}

func (ErrConfigMismatch) Error

func (e ErrConfigMismatch) Error() string

type ErrNotReady

type ErrNotReady struct{}

func (ErrNotReady) Error

func (e ErrNotReady) Error() string

type ErrOverflow

type ErrOverflow struct{}

func (ErrOverflow) Error

func (e ErrOverflow) Error() string

type Mode

type Mode int8

Mode is the operating mode.

const (
	ModePowerDown    Mode = 0 // power-down
	ModeTrigShunt    Mode = 1 // triggered shunt voltage
	ModeTrigBus      Mode = 2 // triggered bus voltage
	ModeTrigShuntBus Mode = 3 // triggered shunt and bus voltage
	ModeADCOff       Mode = 4 // ADC off
	ModeContShunt    Mode = 5 // continuous shunt voltage
	ModeContBus      Mode = 6 // continuous bus voltage
	ModeContShuntBus Mode = 7 // continuous shunt and bus voltage
)
const ModeTriggeredMask Mode = 0x4

ModeTriggered is a mask for triggered modes.

func (Mode) RegisterValue

func (m Mode) RegisterValue() uint16

type PGA

type PGA int8

PGA is the programmable gain amplifier.

const (
	PGA1 PGA = 0 // 40mV
	PGA2 PGA = 1 // 80mV
	PGA4 PGA = 2 // 160mV
	PGA8 PGA = 3 // 320mV
)

func (PGA) RegisterValue

func (p PGA) RegisterValue() uint16

type ShuntADC

type ShuntADC int8

ShuntADC is the shunt ADC resolution.

const (
	SADC9  ShuntADC = 0 // 9-bit
	SADC10 ShuntADC = 1 // 10-bit
	SADC11 ShuntADC = 2 // 11-bit
	SADC12 ShuntADC = 3 // 12-bit
)

func (ShuntADC) RegisterValue

func (s ShuntADC) RegisterValue() uint16

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL