antonfisher / scd30

Go/TinyGo driver for Sensirion SCD30: ambient CO2, humidity, and temperature sensor module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SCD30 (CO2 sensor)

GoDoc Go Report Card Conventional Commits

Go/TinyGo driver for Sensirion SCD30: ambient CO2, humidity, and temperature sensor module.

photo of SCD30 sensor and led matrix showing CO2 reading

Driver

Driver implements all commands described in the official interface description document.

Hardware details

Example

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.

Inspired by

License

MIT License

About

Go/TinyGo driver for Sensirion SCD30: ambient CO2, humidity, and temperature sensor module

License:MIT License


Languages

Language:Go 97.7%Language:Makefile 2.3%