ina219

package module
v0.0.0-...-ae8cebf Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 3 Imported by: 0

README

INA-219 Sensor Go API

GoDoc

Provides an interface for using the INA-219 sensor on a Raspberry Pi via the I2C hardware interface.

You can Texas Instrument's official specifications for the INA-219 sensor here.

Features
  • Simple and easy interface for reading and configuring the INA-219 sensor
  • Leverages Periph.io's I2C library for low-level communication on Pi hardware
  • Completely customizable behavior using the ina219.Configuration struct
To-do
  • More pre-built configurations for easier set up for common situations
Installation
go get github.com/littlehawk93/go-ina219
Getting Started

Simple reading values from the sensor


address := 64 // (Hex address 0x80 this is the default I2C address)

sensor, err := ina219.NewSensor(address, ina219.BusDefault)

if err != nil {
    log.Fatalf("Error initializing INA219 sensor: %s\n", err.Error())
}

defer sensor.Close()

// Get the current voltage being detected by the sensor (value in V)
voltage, _ := sensor.GetBusVoltage()

// Get the current being read by the sensor (value in mA)
current, _ := sensor.GetCurrent()

// Get the current power draw detected by the sensor (value in mW)
power, _ := sensor.GetPower()

Put sensor in power saving mode

if err := sensor.SetPowerSavingMode(true); err != nil {
    log.Fatalf("Unable to put INA-219 in power-saving mode: %s", err.Error())
}

time.Sleep(time.Second)

if err := sensor.SetPowerSavingMode(false); err != nil {
    log.Fatalf("Unable to wake INA-219 from power-saving mode: %s", err.Error())
}

More examples coming in the future!

Documentation

Index

Constants

View Source
const (
	// BusDefault bind power sensor to the first available I2C bus
	BusDefault string = ""
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BusResolution

type BusResolution uint16

BusResolution the ADC bit resolution for the bus

const (
	// BusRes9 9 bit resolution
	BusRes9 BusResolution = 0x0000

	// BusRes10 10 bit resolution
	BusRes10 BusResolution = 0x0080

	// BusRes11 11 bit resolution
	BusRes11 BusResolution = 0x0100

	// BusRes12 12 bit resolution
	BusRes12 BusResolution = 0x0180
)

type BusVoltageTolerance

type BusVoltageTolerance uint16

BusVoltageTolerance defines the tolerated range of voltage values

const (
	// BusVoltageRange16 0-16 Volt range
	BusVoltageRange16 BusVoltageTolerance = 0x0000

	// BusVoltageRange32 0-32 Volt range
	BusVoltageRange32 BusVoltageTolerance = 0x2000
)

type CalibrationSetting

type CalibrationSetting uint16

CalibrationSetting predefined sensor calibration settings

const (
	// V32A2 calibration for 32 Volts, 2 Amps
	V32A2 CalibrationSetting = 4096

	// V32A1 calibration for 32 Volts, 1 Amp
	V32A1 CalibrationSetting = 10240

	// V16A04 calibration for 16 Volts, 400 mA
	V16A04 CalibrationSetting = 8192
)

type Configuration

type Configuration struct {
	Tolerance       BusVoltageTolerance
	ShuntResolution ShuntResolution
	BusResolution   BusResolution
	OperatingMode   OperatingMode
	Gain            Gain
}

Configuration INA219 configuration bit flags

func DefaultConfiguration

func DefaultConfiguration() Configuration

DefaultConfiguration returns the default configuration for the INA219 sensor

func (Configuration) Flatten

func (me Configuration) Flatten() uint16

Flatten combines all the bit flags in this configuration into a single 16 bit value

type Gain

type Gain uint16

Gain the voltage gain

const (
	// Gain40 40mV range
	Gain40 Gain = 0x0000

	// Gain80 80mV range
	Gain80 Gain = 0x0800

	// Gain160 160mV range
	Gain160 Gain = 0x1000

	// Gain320 320mV range
	Gain320 Gain = 0x1800
)

type OperatingMode

type OperatingMode uint16

OperatingMode defines an operating mode for the INA219 sensor

const (
	// ModePowerDown power down the sensor
	ModePowerDown OperatingMode = 0x0000

	// ModeShuntTriggered read shunt sensor values
	ModeShuntTriggered OperatingMode = 0x0001

	// ModeBusTriggered read bus sensor values
	ModeBusTriggered OperatingMode = 0x0002

	// ModeShuntBusTriggered read shunt and bus sensor values
	ModeShuntBusTriggered OperatingMode = 0x0003

	// ModeADCOff turn ADC off
	ModeADCOff OperatingMode = 0x0004

	// ModeShuntContinuous read shunt sensor values continuously
	ModeShuntContinuous OperatingMode = 0x0005

	// ModeBusContinuous read bus sensor values continuously
	ModeBusContinuous OperatingMode = 0x0006

	// ModeShuntBusContinuous read shunt and bus sensor values continuously
	ModeShuntBusContinuous OperatingMode = 0x0007
)

type Sensor

type Sensor struct {
	// contains filtered or unexported fields
}

Sensor handles interfacing with the INA219 power sensor via I2C

func NewSensor

func NewSensor(address uint16, bus string) (*Sensor, error)

NewSensor creates and initializes a new Power sensor

func (*Sensor) Close

func (me *Sensor) Close() error

Close closes the underlying I2C connection for this sensor

func (*Sensor) GetBusVoltage

func (me *Sensor) GetBusVoltage() (float64, error)

GetBusVoltage returns the current voltage from the bus sensor in V, or any errors encountered while reading the sensor

func (*Sensor) GetCurrent

func (me *Sensor) GetCurrent() (float64, error)

GetCurrent returns the current amp draw from the sensor in mA, or any errors encountered while reading the sensor

func (*Sensor) GetPower

func (me *Sensor) GetPower() (float64, error)

GetPower returns the current power from the sensor in mW, or any errors encountered while reading the sensor

func (*Sensor) GetShuntVoltage

func (me *Sensor) GetShuntVoltage() (float64, error)

GetShuntVoltage returns the current voltage from the shunt sensor in mV, or any errors encountered while reading the sensor

func (*Sensor) SetCalibrationRegister

func (me *Sensor) SetCalibrationRegister(val CalibrationSetting) error

SetCalibrationRegister set the calibration value for this sensor. Returns any errors that occur during calibration writing

func (*Sensor) SetConfig

func (me *Sensor) SetConfig(config Configuration) error

SetConfig set the configuration options for this sensor

func (*Sensor) SetPowerSavingMode

func (me *Sensor) SetPowerSavingMode(enabled bool) error

SetPowerSavingMode enables or disables the power saving operating mode on this sensor. Returns any errors that occur during operating mode change.

type ShuntResolution

type ShuntResolution uint16

ShuntResolution the bit resolution for samples taken by the shunt sensor

const (
	// Res9Bit 9 bit ADC resolution
	Res9Bit ShuntResolution = 0x0000

	// Res10Bit 10 bit ADC resolution
	Res10Bit ShuntResolution = 0x0008

	// Res11Bit 11 bit ADC resolution
	Res11Bit ShuntResolution = 0x0010

	// Res12Bit 12 bit ADC resolution
	Res12Bit ShuntResolution = 0x0018

	// Res12BitX2 12 bit ADC resolution sampled 2 times
	Res12BitX2 ShuntResolution = 0x0048

	// Res12BitX4 12 bit ADC resolution sampled 4 times
	Res12BitX4 ShuntResolution = 0x0050

	// Res12BitX8 12 bit ADC resolution sampled 8 times
	Res12BitX8 ShuntResolution = 0x0058

	// Res12BitX16 12 bit ADC resolution sampled 16 times
	Res12BitX16 ShuntResolution = 0x0060

	// Res12BitX32 12 bit ADC resolution sampled 32 times
	Res12BitX32 ShuntResolution = 0x0068

	// Res12BitX64 12 bit ADC resolution sampled 64 times
	Res12BitX64 ShuntResolution = 0x0070

	// Res12BitX128 12 bit ADC resolution sampled 128 times
	Res12BitX128 ShuntResolution = 0x0078
)

Jump to

Keyboard shortcuts

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