tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/scd4x/main.go (about)

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  
     7  	"tinygo.org/x/drivers/scd4x"
     8  )
     9  
    10  var (
    11  	i2c    = machine.I2C0
    12  	sensor = scd4x.New(i2c)
    13  )
    14  
    15  func main() {
    16  	time.Sleep(1500 * time.Millisecond)
    17  
    18  	i2c.Configure(machine.I2CConfig{})
    19  	if err := sensor.Configure(); err != nil {
    20  		println(err)
    21  	}
    22  
    23  	time.Sleep(1500 * time.Millisecond)
    24  
    25  	if err := sensor.StartPeriodicMeasurement(); err != nil {
    26  		println(err)
    27  	}
    28  
    29  	time.Sleep(1500 * time.Millisecond)
    30  
    31  	for {
    32  		co2, err := sensor.ReadCO2()
    33  		if err != nil {
    34  			println(err)
    35  		}
    36  		println("CO2", co2)
    37  		time.Sleep(time.Second)
    38  	}
    39  }