ads1x15

package
v3.3.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2018 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package ads1x15 controls ADS1015/ADS1115 Analog-Digital Converters (ADC) via i2c interface.

Datasheet

ADS1015: http://www.ti.com/product/ADS1015 ADS1115: http://www.ti.com/product/ADS1115

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/i2c/i2creg"
	"periph.io/x/periph/conn/physic"
	"periph.io/x/periph/experimental/devices/ads1x15"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Open default I²C bus.
	bus, err := i2creg.Open("")
	if err != nil {
		log.Fatalf("failed to open I²C: %v", err)
	}
	defer bus.Close()

	// Create a new ADS1115 ADC.
	adc, err := ads1x15.NewADS1115(bus, &ads1x15.DefaultOpts)
	if err != nil {
		log.Fatalln(err)
	}

	// Obtain an analog pin from the ADC
	pin, err := adc.PinForDifferenceOfChannels(ads1x15.Channel0, ads1x15.Channel3, 5*physic.Volt, 1*physic.Hertz, ads1x15.SaveEnergy)
	if err != nil {
		log.Fatalln(err)
	}
	defer pin.Halt()

	// Read values from ADC.
	fmt.Println("Single reading")
	reading, err := pin.Read()

	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(reading)

	// Read values continously from ADC.
	fmt.Println("Continuous reading")
	c := pin.ReadContinuous()

	for reading := range c {
		fmt.Println(reading)
	}
}
Output:

Index

Examples

Constants

View Source
const (
	// I2CAddr is the default I2C address for the ADS1x15 components.
	I2CAddr uint16 = 0x48

	Channel0 = 0
	Channel1 = 1
	Channel2 = 2
	Channel3 = 3
)

Variables

View Source
var DefaultOpts = Opts{
	I2cAddress: I2CAddr,
}

DefaultOpts are the recommended default options.

Functions

This section is empty.

Types

type AnalogPin

type AnalogPin interface {
	pin.Pin
	// Range returns the maximum supported range [min, max] of the values.
	Range() (Reading, Reading)
	// Read returns the current pin level.
	Read() (Reading, error)
	// ReadContinuous opens a channel and reads continuously
	ReadContinuous() <-chan Reading
}

AnalogPin represents a pin which is able to read an electric potential

type ConversionQuality

type ConversionQuality int

ConversionQuality represents a request for a compromise between energy saving versus conversion quality.

const (
	// SaveEnergy optimizes the power consumption of the ADC, at the expense of
	// the quality by converting at the gihest rate possible.
	SaveEnergy ConversionQuality = 0
	// BestQuality will use the lowest suitable data rate to reduce the impact of
	// the noise on the reading.
	BestQuality ConversionQuality = 1
)

type Dev

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

Dev is an handle to an ADS1015/ADS1115 ADC.

func NewADS1015

func NewADS1015(i i2c.Bus, opts *Opts) (*Dev, error)

NewADS1015 creates a new driver for the ADS1015 (12-bit ADC).

func NewADS1115

func NewADS1115(i i2c.Bus, opts *Opts) (*Dev, error)

NewADS1115 creates a new driver for the ADS1115 (16-bit ADC).

func (*Dev) Halt

func (d *Dev) Halt() error

Halt implements conn.Resource.

func (*Dev) PinForChannel

func (d *Dev) PinForChannel(channel int, maxVoltage physic.ElectricPotential, requestedFrequency physic.Frequency, conversionQuality ConversionQuality) (AnalogPin, error)

PinForChannel returns a pin able to measure the electric potential at the given channel.

func (*Dev) PinForDifferenceOfChannels

func (d *Dev) PinForDifferenceOfChannels(channelA int, channelB int, maxVoltage physic.ElectricPotential, requestedFrequency physic.Frequency, conversionQuality ConversionQuality) (AnalogPin, error)

PinForDifferenceOfChannels returns a pin which measures the difference in volts between 2 inputs: channelA - channelB. diff can be: * Channel 0 - channel 1 * Channel 0 - channel 3 * Channel 1 - channel 3 * Channel 2 - channel 3

func (*Dev) String

func (d *Dev) String() string

String implements conn.Resource.

type Opts

type Opts struct {
	I2cAddress uint16
}

Opts holds the configuration options.

type Reading

type Reading struct {
	V   physic.ElectricPotential
	Raw int32
}

Reading is the result of AnalogPin.Read() (obviously not the case right now but this could be)

Jump to

Keyboard shortcuts

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