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

     1  package main
     2  
     3  // Example for the SGP30 to be used on a Raspberry Pi pico.
     4  // Connect the sensor I2C pins to GP26 and GP27 to test.
     5  
     6  import (
     7  	"machine"
     8  	"time"
     9  
    10  	"tinygo.org/x/drivers/sgp30"
    11  )
    12  
    13  func main() {
    14  	time.Sleep(time.Second)
    15  	println("start")
    16  
    17  	// Configure the I2C bus.
    18  	bus := machine.I2C1
    19  	err := bus.Configure(machine.I2CConfig{
    20  		SDA:       machine.GP26,
    21  		SCL:       machine.GP27,
    22  		Frequency: 400 * machine.KHz,
    23  	})
    24  	if err != nil {
    25  		println("could not configure I2C:", bus)
    26  		return
    27  	}
    28  
    29  	// Configure the sensor.
    30  	sensor := sgp30.New(bus)
    31  	if !sensor.Connected() {
    32  		println("sensor not connected")
    33  		return
    34  	}
    35  	err = sensor.Configure(sgp30.Config{})
    36  	if err != nil {
    37  		println("sensor could not be configured:", err.Error())
    38  		return
    39  	}
    40  
    41  	// Measure every second, as recommended by the datasheet.
    42  	for {
    43  		time.Sleep(time.Second)
    44  
    45  		err := sensor.Update(0)
    46  		if err != nil {
    47  			println("could not read sensor:", err.Error())
    48  			continue
    49  		}
    50  		println("CO₂ equivalent:", sensor.CO2())
    51  		println("TVOC           ", sensor.TVOC())
    52  	}
    53  }