Files
drivers/examples/sgp30/main.go
T
Ayke van Laethem 92050d90da sgp30: add the now-obsolete SGP30 air quality sensor
Tested with a rp2040.

TODO: add the ability to set the absolute humidity for more accurate
sensor details. The formula for that is rather complex, so I've left
this as a future addition.
2023-12-01 07:44:49 +01:00

54 lines
1.0 KiB
Go

package main
// Example for the SGP30 to be used on a Raspberry Pi pico.
// Connect the sensor I2C pins to GP26 and GP27 to test.
import (
"machine"
"time"
"tinygo.org/x/drivers/sgp30"
)
func main() {
time.Sleep(time.Second)
println("start")
// Configure the I2C bus.
bus := machine.I2C1
err := bus.Configure(machine.I2CConfig{
SDA: machine.GP26,
SCL: machine.GP27,
Frequency: 400 * machine.KHz,
})
if err != nil {
println("could not configure I2C:", bus)
return
}
// Configure the sensor.
sensor := sgp30.New(bus)
if !sensor.Connected() {
println("sensor not connected")
return
}
err = sensor.Configure(sgp30.Config{})
if err != nil {
println("sensor could not be configured:", err.Error())
return
}
// Measure every second, as recommended by the datasheet.
for {
time.Sleep(time.Second)
err := sensor.Update(0)
if err != nil {
println("could not read sensor:", err.Error())
continue
}
println("CO₂ equivalent:", sensor.CO2())
println("TVOC ", sensor.TVOC())
}
}