Go/TinyGo driver for Sensirion SCD30: ambient CO2, humidity, and temperature sensor module.
Driver implements all commands described in the official interface description document.
- driver communicates over I2C interface (address is
0x61
) - product page
- datasheet
- interface description
- buy sensor from SparkFun.
Driver can work with any Go program that provides I2C interface like this:
type I2C interface {
Tx(addr uint16, w, r []byte) error
}
This is TinyGo example that uses
machine
package's I2C to control SCD30:
package main
import (
"time"
"machine"
"github.com/antonfisher/scd30"
)
func main() {
bus := machine.I2C0
err := bus.Configure(machine.I2CConfig{})
if err != nil {
println("could not configure I2C:", err)
return
}
// Create driver for SCD30.
co2sensor := scd30.New(bus)
// Read sensor's firmware version.
version, err := co2sensor.GetSoftwareVersion()
if err != nil {
println("failed to get software version:", err)
return
}
println("software version:", version)
// Start continuous measurement without provided ambient pressure
// (should be ON by default on a new chip with 2 seconds interval).
err = co2sensor.StartContinuousMeasurement(uint16(0))
if err != nil {
println("ERROR: co2 sensor: failed to trigger continuous measurement:", err)
return
}
// Check is the sensor has data, and read it every 2 seconds.
for {
hasDataReady, err := co2sensor.HasDataReady()
if err != nil {
println("failed to check for data:", err)
} else if hasDataReady {
measurement, err := co2sensor.ReadMeasurement()
if err != nil {
println("failed to read measurement:", err)
} else {
//TODO can use println?
fmt.Printf("measurement: %s\n\r", measurement.String())
}
} else {
println("skipping, no data...")
}
time.Sleep(time.Second * 2)
}
}
This example was tested on nRF52840 controller. Command to flash it:
tinygo flash -target=feather-nrf52840 main.go
Show the output in the terminal:
# find out dev to use
ls -l /dev/cu.*
# use `Control+A Control+\ y [Enter]` to exit
screen /dev/cu.usbmodem144101 19200
For more configuration options see sensor's official interface description document and driver reference.
- Sensirion's official driver: https://github.com/Sensirion/embedded-scd/
- SparkFun SCD30 Arduino driver: https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library
- @pvainio's Go driver: https://github.com/pvainio/scd30
MIT License