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 ¶
Index ¶
- Constants
- Variables
- type Alert
- type Dev
- func (d *Dev) Halt() error
- func (d *Dev) Precision(e *physic.Env)
- func (d *Dev) Sense(e *physic.Env) error
- func (d *Dev) SenseContinuous(interval time.Duration) (<-chan physic.Env, error)
- func (d *Dev) SenseTemp() (physic.Temperature, error)
- func (d *Dev) SenseWithAlerts(lower, upper, critical physic.Temperature) (physic.Temperature, []Alert, error)
- func (d *Dev) String() string
- type Opts
Examples ¶
Constants ¶
const ( Maximum resolution = 0 Low resolution = 1 Medium resolution = 2 High resolution = 3 )
Valid resolution values.
Variables ¶
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 (*Dev) Halt ¶
Halt put the mcp9808 into shutdown mode. It will not read temperatures while in shutdown mode.
func (*Dev) SenseContinuous ¶
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: