mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-26 10:38:41 +00:00
shtc3: Sensirion SHTC3 Relative Humidity / Temperature i2c sensor
This commit is contained in:
committed by
Ron Evans
parent
121e8147f7
commit
025a66655f
@@ -117,6 +117,8 @@ smoke-test:
|
||||
@md5sum ./build/test.hex
|
||||
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/sht3x/main.go
|
||||
@md5sum ./build/test.hex
|
||||
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/shtc3/main.go
|
||||
@md5sum ./build/test.hex
|
||||
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/i2c_128x32/main.go
|
||||
@md5sum ./build/test.hex
|
||||
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/ssd1306/spi_128x64/main.go
|
||||
|
||||
@@ -52,7 +52,7 @@ func main() {
|
||||
|
||||
## Currently supported devices
|
||||
|
||||
The following 74 devices are supported.
|
||||
The following 77 devices are supported.
|
||||
|
||||
| Device Name | Interface Type |
|
||||
|----------|-------------|
|
||||
@@ -114,6 +114,7 @@ The following 74 devices are supported.
|
||||
| [Shift register (PISO)](https://en.wikipedia.org/wiki/Shift_register#Parallel-in_serial-out_\(PISO\)) | GPIO |
|
||||
| [Shift registers (SIPO)](https://en.wikipedia.org/wiki/Shift_register#Serial-in_parallel-out_(SIPO)) | GPIO |
|
||||
| [SHT3x Digital Humidity Sensor](https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/2_Humidity_Sensors/Datasheets/Sensirion_Humidity_Sensors_SHT3x_Datasheet_digital.pdf) | I2C |
|
||||
| [SHTC3 Digital Humidity Sensor (RH/T)](https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/2_Humidity_Sensors/Datasheets/Sensirion_Humidity_Sensors_SHTC3_Datasheet.pdf) | I2C |
|
||||
| [SPI NOR Flash Memory](https://en.wikipedia.org/wiki/Flash_memory#NOR_flash) | SPI/QSPI |
|
||||
| [SPI SDCARD/MMC](https://en.wikipedia.org/wiki/SD_card) | SPI |
|
||||
| [SSD1306 OLED display](https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf) | I2C / SPI |
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/shtc3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
machine.I2C0.Configure(machine.I2CConfig{})
|
||||
sensor := shtc3.New(machine.I2C0)
|
||||
|
||||
for {
|
||||
|
||||
sensor.WakeUp()
|
||||
|
||||
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, "%")
|
||||
|
||||
sensor.Sleep()
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package shtc3
|
||||
|
||||
// Constants used for I2C.
|
||||
|
||||
const (
|
||||
SHTC3_ADDRESS = 0x70
|
||||
SHTC3_CMD_WAKEUP = "\x35\x17" // Wake up
|
||||
SHTC3_CMD_MEASURE_HP = "\x7C\xA2" // Read sensor in high power mode with clock stretching
|
||||
SHTC3_CMD_SLEEP = "\xB0\x98" // Sleep
|
||||
SHTC3_CMD_SOFT_RESET = "\x80\x5D" // Soft Reset
|
||||
)
|
||||
@@ -0,0 +1,79 @@
|
||||
// Package shtc3 provides a driver for the SHTC3 digital humidity sensor
|
||||
// series by Sensirion.
|
||||
//
|
||||
// Datasheet:
|
||||
// https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/2_Humidity_Sensors/Datasheets/Sensirion_Humidity_Sensors_SHTC3_Datasheet.pdf
|
||||
//
|
||||
package shtc3 // import "tinygo.org/x/drivers/shtc3"
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
// Device wraps an I2C connection to a SHT31 device.
|
||||
type Device struct {
|
||||
bus drivers.I2C
|
||||
}
|
||||
|
||||
// New creates a new SHTC3 connection. The I2C bus must already be
|
||||
// configured.
|
||||
//
|
||||
// This function only creates the Device object, it does not initialize the device.
|
||||
// You must call Configure() first in order to use the device itself.
|
||||
func New(bus drivers.I2C) Device {
|
||||
return Device{
|
||||
bus: bus,
|
||||
}
|
||||
}
|
||||
|
||||
// Read returns the temperature in celsius milli degrees (°C/1000).
|
||||
func (d *Device) ReadTemperature() (tempMilliCelsius int32, err error) {
|
||||
tempMilliCelsius, _, err = d.ReadTemperatureHumidity()
|
||||
return tempMilliCelsius, err
|
||||
}
|
||||
|
||||
// Read returns the relative humidity in hundredths of a percent.
|
||||
func (d *Device) ReadHumidity() (relativeHumidity int16, err error) {
|
||||
_, relativeHumidity, err = d.ReadTemperatureHumidity()
|
||||
return relativeHumidity, err
|
||||
}
|
||||
|
||||
// Read returns both the temperature and relative humidity.
|
||||
func (d *Device) ReadTemperatureHumidity() (tempMilliCelsius int32, relativeHumidity int16, err error) {
|
||||
var rawTemp, rawHum, errx = d.rawReadings()
|
||||
if errx != nil {
|
||||
err = errx
|
||||
return
|
||||
}
|
||||
tempMilliCelsius = ((21875 * int32(rawTemp)) >> 13) - 45000
|
||||
relativeHumidity = int16((1250 * int32(rawHum)) >> 13)
|
||||
return tempMilliCelsius, relativeHumidity, err
|
||||
}
|
||||
|
||||
// rawReadings returns the sensor's raw values of the temperature and humidity
|
||||
func (d *Device) rawReadings() (uint16, uint16, error) {
|
||||
var data [6]byte
|
||||
d.bus.Tx(SHTC3_ADDRESS, []byte(SHTC3_CMD_MEASURE_HP), data[:])
|
||||
// ignore crc for now
|
||||
return readUint(data[0], data[1]), readUint(data[3], data[4]), nil
|
||||
}
|
||||
|
||||
// WakeUp makes device leave sleep mode
|
||||
func (d *Device) WakeUp() error {
|
||||
d.bus.Tx(SHTC3_ADDRESS, []byte(SHTC3_CMD_WAKEUP), nil)
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sleep makes device go to sleep
|
||||
func (d *Device) Sleep() error {
|
||||
d.bus.Tx(SHTC3_ADDRESS, []byte(SHTC3_CMD_SLEEP), nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
// readUint converts two bytes to uint16
|
||||
func readUint(msb byte, lsb byte) uint16 {
|
||||
return (uint16(msb) << 8) | uint16(lsb)
|
||||
}
|
||||
Reference in New Issue
Block a user