ads1x15

package
v3.6.8+incompatible Latest Latest
Warning

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

Go to latest
Published: May 24, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package ads1x15 controls ADS1015/ADS1115 Analog-Digital Converters (ADC) via I²C 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.PinForChannel(ads1x15.Channel0Minus3, 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 continuously from ADC.
	fmt.Println("Continuous reading")
	c := pin.ReadContinuous()

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

Index

Examples

Constants

View Source
const I2CAddr uint16 = 0x48

I2CAddr is the default I2C address for the ADS1x15 components.

Variables

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

DefaultOpts are the recommended default options.

Functions

This section is empty.

Types

type Channel

type Channel int

Channel is the analog reading to do. It can be either an absolute reading or a differential reading between two pins.

const (
	// Absolute reading.
	Channel0 Channel = 4
	Channel1 Channel = 5
	Channel2 Channel = 6
	Channel3 Channel = 7

	// Differential reading.
	Channel0Minus1 Channel = 0
	Channel0Minus3 Channel = 1
	Channel1Minus3 Channel = 2
	Channel2Minus3 Channel = 3
)

Value channels.

func (Channel) String

func (c Channel) String() string

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 highest possible rate.
	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(c Channel, maxVoltage physic.ElectricPotential, f physic.Frequency, q ConversionQuality) (PinADC, error)

PinForChannel returns an AnalogPin for the requested channel at the requested frequency.

The channel can either be an absolute reading or a differential one.

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 PinADC

type PinADC interface {
	analog.PinADC
	// ReadContinuous opens a channel and reads continuously at the frequency the
	// pin was configured for.
	ReadContinuous() <-chan analog.Sample
}

PinADC represents a pin which is able to read an electric potential.

Jump to

Keyboard shortcuts

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