Sensirion Sht4x Support (#597)

sht4x: implemented driver
This commit is contained in:
Thomas
2023-09-19 08:31:43 +02:00
committed by GitHub
parent a9b36f8bd4
commit 5888bb2ded
3 changed files with 100 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
package main
import (
"fmt"
"machine"
"time"
"tinygo.org/x/drivers/sht4x"
)
func main() {
machine.I2C0.Configure(machine.I2CConfig{})
sensor := sht4x.New(machine.I2C0)
for {
temp, humidity, _ := sensor.ReadTemperatureHumidity()
t := fmt.Sprintf("%.2f", float32(temp)/1000)
h := fmt.Sprintf("%.2f", float32(humidity)/100)
println("Temperature: ", t, "°C")
println("Humidity: ", h, "%")
time.Sleep(2 * time.Second)
}
}
+76
View File
@@ -0,0 +1,76 @@
// Package sht4x provides a driver for the SHT4x digital humidity sensor series by Sensirion.
// Datasheet: https://www.sensirion.com/media/documents/33FD6951/64D3B030/Sensirion_Datasheet_SHT4x.pdf
package sht4x
import (
"time"
"tinygo.org/x/drivers"
)
const DefaultAddress = 0x44
const (
// single-shot, high-repeatability measurement
commandMeasurement = 0xfd
)
// Device represents a SHT4x sensor
type Device struct {
bus drivers.I2C
Address uint8
}
// New creates a new SHT4x connection. The I2C bus must already be
// configured.
func New(bus drivers.I2C) Device {
return Device{
bus: bus,
Address: DefaultAddress,
}
}
// ReadTemperatureHumidity starts a measurement and then reads out the results. This function blocks
// while the measurement is in progress.
//
// Temperature is returned in [degree Celsius], multiplied by 1000,
// and relative humidity in [percent relative humidity], multiplied by 1000.
func (d *Device) ReadTemperatureHumidity() (temperatureMilliCelsius int32, relativeHumidityMilliPercent int32, err error) {
rawTemp, rawHum, err := d.rawReadings()
if err != nil {
return 0, 0, err
}
// from the reference driver: https://github.com/Sensirion/embedded-sht/blob/fcc8a523210cc1241a2750899ff6b0f68f3ed212/sht4x/sht4x.c#L81
temperatureMilliCelsius = ((21875 * int32(rawTemp)) >> 13) - 45000
relativeHumidityMilliPercent = ((15625 * int32(rawHum)) >> 13) - 6000
return temperatureMilliCelsius, relativeHumidityMilliPercent, err
}
// rawReadings returns the sensor's raw values of the temperature and humidity
func (d *Device) rawReadings() (uint16, uint16, error) {
err := d.bus.Tx(uint16(d.Address), []byte{commandMeasurement}, nil)
if err != nil {
return 0, 0, err
}
// max time for measurement according to datasheet
time.Sleep(10 * time.Millisecond)
var data [6]byte
err = d.bus.Tx(uint16(d.Address), nil, data[:])
if err != nil {
return 0, 0, err
}
tTicks := readUint(data[0], data[1])
rhTicks := readUint(data[3], data[4])
return tTicks, rhTicks, nil
}
// readUint converts two bytes to uint16
func readUint(msb byte, lsb byte) uint16 {
return (uint16(msb) << 8) | uint16(lsb)
}
+1
View File
@@ -63,6 +63,7 @@ tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544
tinygo build -size short -o ./build/test.hex -target=arduino ./examples/servo
tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/shifter/main.go
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht4x/main.go
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go