ads

package module
v0.0.0-...-9b714bd Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2019 License: MIT Imports: 5 Imported by: 0

README

Go ADS1113, ADS1114, and ADS1115 interface

Golang ADS1113, ADS1114, and ADS1115 interface using periph.io driver

GoDoc Reference Go Report Card

Should be easily extendable for ADS1111, ADS1112, and ADS1113, devices (ADS111x) with very little work. If you are intrested, let me know and we can work togeter to get it to work.

Please note

Please make sure to setup your ADS1113, ADS1114, and ADS1115 correctly. Do a search on the internet to find guide. Here is an example of a guide:

https://learn.adafruit.com/adafruit-4-channel-adc-breakouts/

The examples below are from using a Raspberry Pi 3 with I2C1 for the bus and 0x48 for the address. Your setup may be different, if so, your bus and address would need to change in each example.

Tested on Raspberry Pi 3 with ADS1115. Please open an issue if there are any issues.

Get

go get github.com/MichaelS11/go-ads

ReadRetry example

package main

import (
	"fmt"

	"github.com/MichaelS11/go-ads"
)

func main() {
	// call HostInit once
	err := ads.HostInit()
	if err != nil {
		fmt.Println(err)
	}

	// create new ads with wanted busName and address. 
	var ads1 *ads.ADS
	ads1, err = ads.NewADS("I2C1", 0x48, "")
	if err != nil {
		fmt.Println(err)
	}

	// example changing config gain (2/3 is default, so only an example)
	ads1.SetConfigGain(ads.ConfigGain2_3)

	// read retry from ads chip
	var result uint16
	result, err = ads1.ReadRetry(5)
	if err != nil {
		ads1.Close()
		fmt.Println(err)
	}

	// close ads bus
	err = ads1.Close()
	if err != nil {
		fmt.Println(err)
	}

	// print results
	fmt.Println("result:", result)
	volts := (float64(result) / 32767.0 * 5.0)
	fmt.Println("volts:", volts)
	psi := 360.0*volts - 25.0
	fmt.Println("psi:", psi)
}

ReadBackground example

package main

import (
	"fmt"
	"time"

	"github.com/MichaelS11/go-ads"
)

func main() {
	// call HostInit once
	err := ads.HostInit()
	if err != nil {
		fmt.Println(err)
	}

	// create new ads with wanted busName and address
	var ads1 *ads.ADS
	ads1, err = ads.NewADS("I2C1", 0x48, "")
	if err != nil {
		fmt.Println(err)
	}

	// changing config gain
	ads1.SetConfigGain(ads.ConfigGain1)

	stop := make(chan struct{})
	stopped := make(chan struct{})
	var result uint16

	// get ads chip reading every 5 seconds in background
	go ads1.ReadBackground(&result, 5*time.Second, stop, stopped)

	// should have at least read the ads chip twice after 15 seconds
	time.Sleep(15 * time.Second)

	// to stop ReadBackground after done with reading, close the stop channel
	close(stop)

	// can check stopped channel to know when ReadBackground has stopped
	<-stopped

	// close ads bus
	err = ads1.Close()
	if err != nil {
		fmt.Println(err)
	}

	// print results
	fmt.Println("result:", result)
	volts := (float64(result) / 32767.0 * 5.0)
	fmt.Println("volts:", volts)
	psi := 360.0*volts - 25.0
	fmt.Println("psi:", psi)
}

Read example

	// call HostInit once
	err := ads.HostInit()
	if err != nil {
		fmt.Println(err)
	}

	// create new ads with wanted busName and address. 
	var ads1 *ads.ADS
	ads1, err = ads.NewADS("I2C1", 0x48, "")
	if err != nil {
		fmt.Println(err)
	}

	// example changing config gain (2/3 is default, so only an example)
	ads1.SetConfigGain(ads.ConfigGain2_3)

	// read from ads chip
	var result uint16
	result, err = ads1.Read()
	if err != nil {
		ads1.Close()
		fmt.Println(err)
	}

	// close ads bus
	err = ads1.Close()
	if err != nil {
		fmt.Println(err)
	}

	// print results
	fmt.Println("result:", result)
	volts := (float64(result) / 32767.0 * 5.0)
	fmt.Println("volts:", volts)
	psi := 360.0*volts - 25.0
	fmt.Println("psi:", psi)
}

Documentation

Index

Constants

View Source
const (

	// ConfigInputMultiplexerDifferential01 000 : AINP = AIN0 and AINN = AIN1 (default)
	ConfigInputMultiplexerDifferential01 ConfigInputMultiplexer = 0x0000
	// ConfigInputMultiplexerDifferential03 001 : AINP = AIN0 and AINN = AIN3
	ConfigInputMultiplexerDifferential03 ConfigInputMultiplexer = 0x1000
	// ConfigInputMultiplexerDifferential13 010 : AINP = AIN1 and AINN = AIN3
	ConfigInputMultiplexerDifferential13 ConfigInputMultiplexer = 0x2000
	// ConfigInputMultiplexerDifferential23 011 : AINP = AIN2 and AINN = AIN3
	ConfigInputMultiplexerDifferential23 ConfigInputMultiplexer = 0x3000
	// ConfigInputMultiplexerSingle0 100 : AINP = AIN0 and AINN = GND
	ConfigInputMultiplexerSingle0 ConfigInputMultiplexer = 0x4000
	// ConfigInputMultiplexerSingle1 101 : AINP = AIN1 and AINN = GND
	ConfigInputMultiplexerSingle1 ConfigInputMultiplexer = 0x5000
	// ConfigInputMultiplexerSingle2 110 : AINP = AIN2 and AINN = GND
	ConfigInputMultiplexerSingle2 ConfigInputMultiplexer = 0x6000
	// ConfigInputMultiplexerSingle3 111 : AINP = AIN3 and AINN = GND
	ConfigInputMultiplexerSingle3 ConfigInputMultiplexer = 0x7000

	// ConfigGain2_3 gain amplifier 2/3 is +/-6.144V range (default)
	ConfigGain2_3 ConfigGain = 0x0000 // 000
	// ConfigGain1 gain amplifier 1 is +/-4.096V range
	ConfigGain1 ConfigGain = 0x0200 // 001
	// ConfigGain2 gain amplifier 2 is +/-2.048V range
	ConfigGain2 ConfigGain = 0x0400 // 010
	// ConfigGain4 gain amplifier 4 is +/-1.024V range
	ConfigGain4 ConfigGain = 0x0600 // 011
	// ConfigGain8 gain amplifier 8 is +/-0.512V range
	ConfigGain8 ConfigGain = 0x0800 // 100
	// ConfigGain16 gain amplifier 16 +/-0.256V range
	ConfigGain16 ConfigGain = 0x0A00 // 101

	// ConfigDataRate8 data rate of 8 samples per second
	ConfigDataRate8 ConfigDataRate = 0x0000 // 000
	// ConfigDataRate16 data rate of 16 samples per second
	ConfigDataRate16 ConfigDataRate = 0x0020 // 001
	// ConfigDataRate32 data rate of 32 samples per second
	ConfigDataRate32 ConfigDataRate = 0x0040 // 010
	// ConfigDataRate64 data rate of 64 samples per second
	ConfigDataRate64 ConfigDataRate = 0x0060 // 011
	// ConfigDataRate128 data rate of 128 samples per second (default)
	ConfigDataRate128 ConfigDataRate = 0x0080 // 100
	// ConfigDataRate250 data rate of 250 samples per second
	ConfigDataRate250 ConfigDataRate = 0x00A0 // 101
	// ConfigDataRate475 data rate of 475 samples per second
	ConfigDataRate475 ConfigDataRate = 0x00C0 // 110
	// ConfigDataRate860 data rate of 860 samples per second
	ConfigDataRate860 ConfigDataRate = 0x00E0 // 111

)

Variables

This section is empty.

Functions

func HostInit

func HostInit() error

HostInit calls periph.io host.Init(). This needs to be done before ADS can be used.

Types

type ADS

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

ADS struct to interface with the Analog-to-Digital Converter. Call NewADS to create a new one.

func NewADS

func NewADS(busName string, address uint16, sensorType string) (*ADS, error)

NewADS to create a new ADS struct. sensorType is not use at this time, it is for possible future use

func (*ADS) Close

func (ads *ADS) Close() error

Close closes bus

func (*ADS) Read

func (ads *ADS) Read() (uint16, error)

Read reads the ads chip

func (*ADS) ReadBackground

func (ads *ADS) ReadBackground(result *uint16, sleepDuration time.Duration, stop chan struct{}, stopped chan struct{})

ReadBackground it meant to be run in the background, run as a Goroutine. sleepDuration is how long it will try to sleep between reads. If there is ongoing read errors there will be no notice except that the result will not be updated. Will continue to read ads chip until stop is closed. After it has been stopped, the stopped chan will be closed. Will panic if result or stop are nil.

func (*ADS) ReadRetry

func (ads *ADS) ReadRetry(maxRetries int) (result uint16, err error)

ReadRetry will call Read until there is no errors or the maxRetries is hit. Suggest maxRetries to be set around 5.

func (*ADS) SetConfigDataRate

func (ads *ADS) SetConfigDataRate(configDataRate ConfigDataRate)

SetConfigDataRate sets data rate

func (*ADS) SetConfigGain

func (ads *ADS) SetConfigGain(configGain ConfigGain)

SetConfigGain sets gain

func (*ADS) SetConfigInputMultiplexer

func (ads *ADS) SetConfigInputMultiplexer(configInputMultiplexer ConfigInputMultiplexer)

SetConfigInputMultiplexer sets input multiplexer

type ConfigDataRate

type ConfigDataRate uint16

ConfigDataRate config data rate

type ConfigGain

type ConfigGain uint16

ConfigGain config gain amplifier

type ConfigInputMultiplexer

type ConfigInputMultiplexer uint16

ConfigInputMultiplexer config input multiplexer

Jump to

Keyboard shortcuts

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