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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"machine"
     6  	"time"
     7  	"tinygo.org/x/drivers/bmp280"
     8  )
     9  
    10  func main() {
    11  	time.Sleep(5 * time.Second)
    12  
    13  	machine.I2C0.Configure(machine.I2CConfig{})
    14  	sensor := bmp280.New(machine.I2C0)
    15  	sensor.Configure(bmp280.STANDBY_125MS, bmp280.FILTER_4X, bmp280.SAMPLING_16X, bmp280.SAMPLING_16X, bmp280.MODE_FORCED)
    16  
    17  	connected := sensor.Connected()
    18  	if !connected {
    19  		println("\nBMP280 Sensor not detected\n")
    20  		return
    21  	}
    22  	println("\nBMP280 Sensor detected\n")
    23  
    24  	println("Calibration:")
    25  	sensor.PrintCali()
    26  
    27  	for {
    28  		t, err := sensor.ReadTemperature()
    29  		if err != nil {
    30  			println("Error reading temperature")
    31  		}
    32  		// Temperature in degrees Celsius
    33  		fmt.Printf("Temperature: %.2f °C\n", float32(t)/1000)
    34  
    35  		p, err := sensor.ReadPressure()
    36  		if err != nil {
    37  			println("Error reading pressure")
    38  		}
    39  		// Pressure in hectoPascal
    40  		fmt.Printf("Pressure: %.2f hPa\n", float32(p)/100000)
    41  
    42  		time.Sleep(5 * time.Second)
    43  	}
    44  }