Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scd4x: SCD4x CO2 Sensor - Initial Add #81

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions scd4x/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Sensirion SCD4x CO<sub>2</sub> Sensors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason to not put this into doc.go? I would expect to read the doc from pkg.go.dev, so I would think it would be more discoverable by being there.


## Overview

This package provides a driver for the Sensirion SCD4x CO<sub>2</sub> sensors. This is a
compact sensor that provides temperature, humidity, and CO<sub>2</sub> concentration
readings. The datasheet for this device is available at:

https://sensirion.com/media/documents/48C4B7FB/66E05452/CD_DS_SCD4x_Datasheet_D1.pdf

## Testing

The unit tests can function with either a live sensor, or in playback mode. If the
environment variable SCD4X is set, then the self test code will use a live
sensor on the default I<sup>2</sup>C bus. For example:

```bash
$> SCD4X=1 go test -v
```
If the environment variable is not present, then unit tests will be conducted using
playback values.

## Notes

### Acquisition Time

The minimum acquisition time for the sensor is 5 seconds. If you call Sense() more
frequently, it will block until a reading is ready.

### Forced Calibration and Self-Test

These functions are not implemented. From examining the datasheet, and
experimenting, it appears that these two calls require the i2c communication
driver to wait a specified period before initiating the read. The periph.io
I<sup>2</sup>C library doesn't support this functionality. This means that attempts
to call these functions will always fail so they're not implemented.

### Acquisition Mode

Only certain commands can be issued while the device is running in acquisition
mode. If you're working on the low-level code, be aware that attempts to send
a non-allowed command while in acquisition mode will return an i2c remote
io-error.

### Automatic Self Calibration

When Automatic Self Calibration is enabled, and the sensor has run for the
required period, it will adjust itself so that the LOWEST recorded reading
during the period yields the value set for ASC Target. The factory default
target is 400PPM, but the current PPM is ~425PPM. To get a more accurate
value for CO2 concentration in Earth's atmosphere, refer to:

https://www.co2.earth/daily-co2

For more details, refer to the datasheet.

12 changes: 12 additions & 0 deletions scd4x/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

// This package provides a driver for the Sensiron SCD4x CO2 sensors.
// The scd4x family provide a compact sensor that can be used to measure
// Temperature, Humidity, and CO2 concentration.
//
// Refer to the datasheet for more information.
//
// https://sensirion.com/media/documents/48C4B7FB/66E05452/CD_DS_SCD4x_Datasheet_D1.pdf
package scd4x
63 changes: 63 additions & 0 deletions scd4x/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build examples
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove, unnecessary.

// +build examples

// Copyright 2024 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

package scd4x_test

import (
"fmt"
"log"

"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/devices/v3/scd4x"
"periph.io/x/host/v3"
)

// basic example program for scd4x sensors using this library.
//
// To execute this as a stand-alone program:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will already be presented correctly on pkg.go.dev, this is unnecessary. Otherwise we'd have to put this on every single examples?

//
// Copy the file example_test.go to a new directory.
// rename the file to main.go
// rename the Example() function to main, and the package to main
//
// execute:
//
// go mod init mydomain.com/scd4x
// go mod tidy
// go build -o main main.go
// ./main
func Example() {
fmt.Println("scd4x example program")
if _, err := host.Init(); err != nil {
fmt.Println(err)
}
bus, err := i2creg.Open("")
if err != nil {
log.Fatal(err)
}
dev, err := scd4x.NewI2C(bus, scd4x.SensorAddress)
if err != nil {
log.Fatal(err)
}

env := scd4x.Env{}
err = dev.Sense(&env)
if err == nil {
fmt.Println(env.String())
} else {
fmt.Println(err)
}

cfg, err := dev.GetConfiguration()
if err == nil {
fmt.Printf("Configuration: %#v\n", cfg)
} else {
fmt.Println(err)
}
// Output: Temperature: 24.845°C Humidity: 32.3%rH CO2: 581 PPM
// Configuration: &scd4x.DevConfig{AmbientPressure:0, ASCEnabled:true, ASCInitialPeriod:158400000000000, ASCStandardPeriod:561600000000000, ASCTarget:400, SensorAltitude:0, SerialNumber:127207989525260, TemperatureOffset:4, SensorType:0}
}
Loading