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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  
     7  	"tinygo.org/x/drivers"
     8  	"tinygo.org/x/drivers/ndir"
     9  )
    10  
    11  var (
    12  	ndirBus = machine.I2C0
    13  )
    14  
    15  func main() {
    16  	err := ndirBus.Configure(machine.I2CConfig{
    17  		Frequency: 100_000,
    18  	})
    19  	if err != nil {
    20  		panic("i2c config fail:" + err.Error())
    21  	}
    22  	// Set the address based on how the resistors are soldered.
    23  	// True means the left and middle pads are joined.
    24  	ndirAddr := ndir.Addr(true, false)
    25  	dev := ndir.NewDevI2C(ndirBus, ndirAddr)
    26  	err = dev.Init()
    27  	if err != nil {
    28  		panic("ndir init fail:" + err.Error())
    29  	}
    30  	// Datasheet tells us to wait 12 seconds before reading from the sensor.
    31  	time.Sleep(12 * time.Second)
    32  	for {
    33  		time.Sleep(time.Second)
    34  		err := dev.Update(drivers.AllMeasurements)
    35  		if err != nil {
    36  			println(err.Error())
    37  			continue
    38  		}
    39  		println("PPM:", dev.PPMCO2())
    40  	}
    41  }