aht20

package
v3.7.3 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package aht20 controls an AHT20 device over I²C.

More details

The sensor is a temperature and humidity sensor with a typical accuracy of ±2% RH and ±0.3°C. The aht20.Dev type implements the physic.SenseEnv interface. The physic.Env measurement results contain a temperature, pressure and humidity value though the pressure is not set. The only device address is 0x38.

Datasheet

http://www.aosong.com/userfiles/files/media/Data%20Sheet%20AHT20.pdf

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

// Use i2creg I²C bus registry to find the first available I²C bus.
b, err := i2creg.Open("")
if err != nil {
	log.Fatalf("failed to open I²C: %v", err)
}
defer b.Close()

// Create a new AHT20 device using I²C bus.
d, err := aht20.NewI2C(b, nil) // nil for default options or &aht20.DefaultOpts
if err != nil {
	log.Fatalf("failed to initialize AHT20: %v", err)
}

// Read temperature and humidity from the sensor
e := physic.Env{}
if err := d.Sense(&e); err != nil {
	log.Fatal(err)
}
fmt.Printf("%8s %9s\n", e.Temperature, e.Humidity)
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultOpts = Opts{
	MeasurementReadTimeout:  150 * time.Millisecond,
	MeasurementWaitInterval: 10 * time.Millisecond,
	ValidateData:            true,
}

DefaultOpts holds the default configuration options for the device.

Functions

This section is empty.

Types

type DataCorruptionError

type DataCorruptionError struct {
	// Calculated is the calculated CRC8 hash using the received data bytes.
	Calculated uint8
	// Received is the CRC8 hash received from the sensor.
	Received uint8
}

DataCorruptionError is returned when the data from the sensor does not match the CRC8 hash.

func (*DataCorruptionError) Error

func (e *DataCorruptionError) Error() string

type Dev

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

func NewI2C

func NewI2C(b i2c.Bus, opts *Opts) (*Dev, error)

NewI2C returns an object that communicates over I²C to AHT20 environmental sensor. The sensor will be calibrated and initialized if it is not already. The Opts can be nil.

func (*Dev) Halt

func (d *Dev) Halt() error

Halt stops the AHT20 from acquiring measurements as initiated by SenseContinuous().

func (*Dev) Initialize

func (d *Dev) Initialize() error

Initialize calibrates the sensor. It takes 10ms.

func (*Dev) IsInitialized

func (d *Dev) IsInitialized() (error, bool)

IsInitialized returns true if the sensor is initialized (calibrated)

func (*Dev) Precision

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

Precision implements physic.SenseEnv.

func (*Dev) Sense

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

Sense implements physic.SenseEnv. It returns the current temperature and humidity, the pressure is always 0 since the AH20 does not measure pressure. The measurement takes at least 80ms. If the configured timeout is reached, a ReadTimeoutError is returned. If the data is corrupt, a DataCorruptionError is returned. If the sensor is not initialized, a NotInitializedError is returned.

func (*Dev) SenseContinuous

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

SenseContinuous implements physic.SenseEnv. It returns a channel that will receive a measurement every interval. It is the caller's responsibility to call Halt() when done. The sensor tries to read the measurement at the given interval however it may take longer if the sensor is busy.

func (*Dev) SoftReset

func (d *Dev) SoftReset() error

SoftReset resets the sensor. It includes a reboot and a re-calibration.

type NotInitializedError

type NotInitializedError struct{}

NotInitializedError is returned when the sensor is not initialized but a measurement is requested.

func (*NotInitializedError) Error

func (e *NotInitializedError) Error() string

type Opts

type Opts struct {
	// MeasurementReadTimeout is the timeout for reading a single measurement. The timeout only applies after the measurement triggering which itself takes 80ms. Default is 150ms. 0 means no timeout.
	MeasurementReadTimeout time.Duration
	// MeasurementWaitInterval is the interval between subsequent sensor value reads. This applies only if the measurement is not finished after the initial 80ms wait. Do not confuse this interval with SenseContinuous. Default is 10ms. Leave 0 to use default.
	MeasurementWaitInterval time.Duration
	// ValidateData enables data validation using CRC8. If enabled, the sensor will return an error if the data is corrupt. Default is true.
	ValidateData bool
}

Opts holds the configuration options for the device.

type ReadTimeoutError

type ReadTimeoutError struct {
	// Timeout is the configured timeout.
	Timeout time.Duration
}

ReadTimeoutError is returned when the sensor does not finish a measurement in time.

func (*ReadTimeoutError) Error

func (e *ReadTimeoutError) Error() string

Jump to

Keyboard shortcuts

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