Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dht20: add I2C driver for DHT20 temperature and humidity sensor #693

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions dht20/dht20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Package dht20 implements a driver for the DHT20 temperature and humidity sensor.
//
// Datasheet: https://cdn-shop.adafruit.com/product-files/5183/5193_DHT20.pdf

package dht20

import (
"time"

"tinygo.org/x/drivers"
)

// Device wraps an I2C connection to a DHT20 device.
type Device struct {
bus drivers.I2C
Address uint16
data [8]uint8
temperature float32
humidity float32
prevAccessTime time.Time
}

// New creates a new DHT20 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{
bus: bus,
Address: defaultAddress, // Using the address defined in registers.go
}
}

// Configure sets up the device for communication and initializes the registers if needed.
func (d *Device) Configure() {
// Get the status word
d.data[0] = 0x71
d.bus.Tx(d.Address, d.data[:1], d.data[:1])
if d.data[0] != 0x18 {
// Initialize registers
d.initRegisters()
}
// Set the previous access time to the current time
d.prevAccessTime = time.Now()
}

// initRegisters initializes the registers 0x1B, 0x1C, and 0x1E to 0x00.
func (d *Device) initRegisters() {
// Initialize register 0x1B
d.data[0] = 0x1B
d.data[1] = 0x00
d.bus.Tx(d.Address, d.data[:2], nil)

// Initialize register 0x1C
d.data[0] = 0x1C
d.data[1] = 0x00
d.bus.Tx(d.Address, d.data[:2], nil)

// Initialize register 0x1E
d.data[0] = 0x1E
d.data[1] = 0x00
d.bus.Tx(d.Address, d.data[:2], nil)
}

// Update reads data from the sensor and updates the temperature and humidity values.
func (d *Device) Update(which drivers.Measurement) error {
// Check if 80ms have passed since the last access
if d.prevAccessTime.Add(80 * time.Millisecond).Before(time.Now()) {
sago35 marked this conversation as resolved.
Show resolved Hide resolved
// Check the status word Bit[7]
d.data[0] = 0x71
d.bus.Tx(d.Address, d.data[:1], d.data[:1])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the returned error ignored here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the good suggestion. I have made the correction.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see related change. Error returned from d.bus.Tx is still ignored on several places in Update and Configure methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misread the suggestion.
The comment was regarding functions that return errors.
So, I have made the correction.
I verified it using kisielk/errcheck.

$ errcheck -verbose ./dht20/...
checking tinygo.org/x/drivers/dht20

if (d.data[0] & 0x80) == 0 {
// Read 7 bytes of data from the sensor
d.bus.Tx(d.Address, nil, d.data[:7])
rawHumidity := uint32(d.data[1])<<12 | uint32(d.data[2])<<4 | uint32(d.data[3])>>4
rawTemperature := uint32(d.data[3]&0x0F)<<16 | uint32(d.data[4])<<8 | uint32(d.data[5])

// Convert raw values to human-readable values
d.humidity = float32(rawHumidity) / 1048576.0 * 100
d.temperature = float32(rawTemperature)/1048576.0*200 - 50

// Trigger the next measurement
d.data[0] = 0xAC

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the measurement is only triggered here that means the data that was read before is from previous measurement. If the user cod is calling Update once a minute (or on even longer interval) it would be always getting stale data. Would it be better to just wait 80ms until update is finished and then read the values? Other option would be to change the API, for example to have separate methods for triggering update and reading the data.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of Update() is aligned with the interface in #345. I feel that waiting for 80ms within Update() is problematic. As you mentioned, the current code results in obtaining stale data. We would like to discuss how to proceed in a separate PR or other discussion.

For now, I have added a note in the comments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is marked as "outdated" by github. Is the issue you all were discussing still present?

My only gripe with the code is that Update does not actually update the sensor and returns no indication of such. We could have a sentinel error for this case or choose to block until update finishes. Both are not without their pros and cons.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, now come to think of it the sentinel error might be the most idiomatic way to solve this without requiring users to start using goroutines to manage several sensors which have a cooldown period.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soypat the issue I noted is that Update first reads the data from the sensor and stores it into Device object, and then triggers next measurement, so when user code reads the temperature it is the value that was not measured after last Update call, but after second to last Update call and hance stail. Maybe Update should just trigger the measurement and Temperature and Humidity methods should check if enough time has passed (or read the status byte) and if so, do the reading from the sensor and store the value in Device, all subsequent calls could just return stored values. Those methods should also return error in this case. I also agree that Update should return sentinel error if it is called before enough time has passed.
Another thing I just noticed is that which argument of Update is ignored and both Temperature and Humidity are always updated. What if Update is called with drivers.Humidity? Should the Temperature also be updated?

Copy link
Contributor

@soypat soypat Jul 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, that sounds like it could cause several headaches. I'd document the Sensor interface such that the function returns a nil error only after it starts and ends a new sensor measurment succesfully updating all requested measurements in which.

  • Temperature and Humidity need not check anything, those methods always return stale data (to some extent). If Staleness functionality should be desired on the Humidity/Temperature methods it probably be implemented in a type that contains a Sensor type and a callback to the Temperature/Humidity/Pressure/Distance method, that way it is readily reusable among different sensors.

  • which should not be ignored. The simplest implementation should perform I/O only if one or more of the corresponding measurements are set. It is OK for it to update both Temperature and Humidity if only Humidity or Temperature is passed. But if neither Temp nor Humidity are set Update should return immediately with a nil error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the check for which.

d.data[1] = 0x33
d.data[2] = 0x00
d.bus.Tx(d.Address, d.data[:3], nil)

// Update the previous access time to the current time
d.prevAccessTime = time.Now()
}
}
return nil
}

// Temperature returns the last measured temperature.
func (d *Device) Temperature() float32 {
return d.temperature
}

// Humidity returns the last measured humidity.
func (d *Device) Humidity() float32 {
return d.humidity
}
Comment on lines +137 to +144
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I'm just noticing this now- you are using floats! This would present a problem on certain devices with no FPU, causing lots of CPU cycles to be spent on processing the data.

It was not explicit in the Sensor PR, but I'd expect these methods to return an integer lest the sensor send back a binary floating point representation.

Copy link
Contributor

@soypat soypat Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using integer arithmetic and representing the humidity and temperature as fixed point integers the code could take the following form (have not tested, did some basic arithmetic to get the numbers):

rawHumidity := uint32(d.data[1])<<12 | uint32(d.data[2])<<4 | uint32(d.data[3])>>4
rawTemperature := uint32(d.data[3]&0x0F)<<16 | uint32(d.data[4])<<8 | uint32(d.data[5])

// humidity contains millipercent
d.humidity = int32(rawHumidity / 10) 
// temperature contains millicelsius
d.temperature = int32(rawTemperature/5) - 50000

If users need floating point representations (which they do) we could create a sensor package that contains something of this sort of logic:

type Thermometer interface {
    drivers.Sensor
    // Temperature returns fixed point representation of temperature in milicelsius [mC]. 
    Temperature() int32
}

func ReadKelvin32(t Thermometer) (float32, error) {
     err := t.Update(drivers.Temperature)
     if err != nil {
        return err
     }
     v := t.Temperature()
     return float32(v)/1000 + 273.15
}

// more advanced ADC configurations...
type sensorConversion struct {
    s drivers.Sensor
    gets []func() int32
    conversions []func(v int32) float32
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #332 for a previous attempt

6 changes: 6 additions & 0 deletions dht20/registers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dht20

// Constants/addresses used for I2C.

// The I2C address which this device listens to.
const defaultAddress = 0x38
36 changes: 36 additions & 0 deletions examples/dht20/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"machine"
"strconv"
"time"

"tinygo.org/x/drivers"
"tinygo.org/x/drivers/dht20"
)

var (
i2c = machine.I2C0
)

func main() {
i2c.Configure(machine.I2CConfig{})
sensor := dht20.New(i2c)
sensor.Configure()

// Trigger the first measurement
sensor.Update(drivers.AllMeasurements)

for {
time.Sleep(1 * time.Second)

// Update sensor dasta
sensor.Update(drivers.AllMeasurements)
temp := sensor.Temperature()
hum := sensor.Humidity()

// Note: The sensor values are from the previous measurement (1 second ago)
println("Temperature:", strconv.FormatFloat(float64(temp), 'f', 2, 64), "°C")
println("Humidity:", strconv.FormatFloat(float64(hum), 'f', 2, 64), "%")
}
}
1 change: 1 addition & 0 deletions smoketest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ tinygo build -size short -o ./build/test.hex -target=hifive1b ./examples/ssd1351
tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis2mdl/main.go
tinygo build -size short -o ./build/test.hex -target=arduino-nano33 ./examples/max72xx/main.go
tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/dht/main.go
tinygo build -size short -o ./build/test.hex -target=feather-m0 ./examples/dht20/main.go
# tinygo build -size short -o ./build/test.hex -target=arduino ./examples/keypad4x4/main.go
tinygo build -size short -o ./build/test.hex -target=feather-rp2040 ./examples/pcf8523/
tinygo build -size short -o ./build/test.hex -target=xiao ./examples/pcf8563/alarm/
Expand Down
Loading