mcp9808

package
v3.6.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package mcp9808 controls a Microchip MCP9808 digital I²C temperature sensor.

Features

-40°C and +125°C Operating Range.

User-Selectable Measurement Resolution: +0.5°C, +0.25°C, +0.125°C, +0.0625°C.

User-Programmable Temperature Alerts.

Datasheet

http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf

Index

Examples

Constants

View Source
const (
	Maximum resolution = 0
	Low     resolution = 1
	Medium  resolution = 2
	High    resolution = 3
)

Valid resolution values.

Variables

View Source
var DefaultOpts = Opts{
	Addr: 0x18,
	Res:  Maximum,
}

DefaultOpts is the recommended default options.

Functions

This section is empty.

Types

type Alert

type Alert struct {
	AlertMode  string
	AlertLevel physic.Temperature
}

Alert represents an alert generated by the device.

type Dev

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

Dev is a handle to the mcp9808 sensor.

func New

func New(bus i2c.Bus, opts *Opts) (*Dev, error)

New opens a handle to an mcp9808 sensor.

func (*Dev) Halt

func (d *Dev) Halt() error

Halt put the mcp9808 into shutdown mode. It will not read temperatures while in shutdown mode.

func (*Dev) Precision

func (d *Dev) Precision(e *physic.Env)

Precision implement SenseEnv.

func (*Dev) Sense

func (d *Dev) Sense(e *physic.Env) error

Sense reads the current temperature.

func (*Dev) SenseContinuous

func (d *Dev) SenseContinuous(interval time.Duration) (<-chan physic.Env, error)

SenseContinuous returns measurements as °C, on a continuous basis. The application must call Halt() to stop the sensing when done to stop the sensor and close the channel. It's the responsibility of the caller to retrieve the values from the channel as fast as possible, otherwise the interval may not be respected.

func (*Dev) SenseTemp

func (d *Dev) SenseTemp() (physic.Temperature, error)

SenseTemp reads the current temperature.

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/i2c/i2creg"
	"periph.io/x/periph/experimental/devices/mcp9808"
	"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 temperature sensor.
	sensor, err := mcp9808.New(bus, &mcp9808.DefaultOpts)
	if err != nil {
		log.Fatalln(err)
	}

	// Read values from sensor.
	measurement, err := sensor.SenseTemp()

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

	fmt.Println(measurement)
}
Output:

func (*Dev) SenseWithAlerts

func (d *Dev) SenseWithAlerts(lower, upper, critical physic.Temperature) (physic.Temperature, []Alert, error)

SenseWithAlerts reads the ambient temperature and returns an slice of any alerts that have been tripped. Lower must be less than upper which must be less than critical.

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/mcp9808"
	"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 temperature sensor.
	sensor, err := mcp9808.New(bus, &mcp9808.DefaultOpts)
	if err != nil {
		log.Fatalln(err)
	}

	lower := physic.ZeroCelsius
	upper := physic.ZeroCelsius + 25*physic.Celsius
	critical := physic.ZeroCelsius + 32*physic.Celsius

	// Read values from sensor.
	temperature, alerts, err := sensor.SenseWithAlerts(lower, upper, critical)

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

	for _, alert := range alerts {
		fmt.Println(alert)
	}

	fmt.Println(temperature)
}
Output:

func (*Dev) String

func (d *Dev) String() string

type Opts

type Opts struct {
	Addr int
	Res  resolution
}

Opts holds the configuration options.

Slave Address

Depending which pins the A0, A1 and A2 pins are connected to will change the slave address. Default configuration is address 0x18 (Ax pins to GND). For a full address table see datasheet.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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