I’m trying to use an RPi Pico W as a temp/humidity sensor using a DHT20.
It kind of works - at least sometimes, but I keep “losing” sensors more or less randomly.
I connected everything up like here (using MicroPython): https://github.com/flrrth/pico-dht20 There are currently 4 sensor-boards, 3 soldered, one on a breadboard.
The error modes I could observe are:
-
DHT20 fails to init - sometimes after the first read, sometimes after days. Resetting the machine works sometimes, if not, power cycling usually does the trick
-
The board just “stops” after about 5min - the serial console just says “device disconnected”. Power cycling is the only option.
My measurement work by having a timer fire every minute, connect to wifi, read from the sensor, and then send an mqtt message (either the values or an error message) and shutdown wifi again.
My current ideas why it could fail (but I’m not an electronics guy at all):
- There is some kind of “rogue current” messing with some IC.
- Some component is broken
- Maybe the power draw is too low or issuing sleep() messes with the USB-power connection somehow?
For me the problem is, I don’t really know where to look for errors. The software works in principle, the soldering seems to be good enough to sometimes work for days, and looking too deep into the whole electronics side is beyond my capabilities.
Perhaps slightly adjust your logic a little and see what it does.
Read from the sensors first, then enable and connect to wifi, send the data, then disconnect. That would reduce the maximum power draw as only one function is active at once.
Small edit: I have a MagTag ESP32 board with circuitpython that can’t read onewire devices while the wifi is active. Whether that’s because of supply instabilities when wifi is transmitting, or interrupt conflicts, or just plain poor programming in the onewire drivers or the wifi drivers, I don’t know. But reading the devices first and then connecting to wifi and sending the data afterwards works.
I tried that. The loop was:
- Read sensor
- Prepare message
- Wifi on
- Send message
- Wifi off
That should nicely separate everything, but it still ran into similar issues.
Hmm I’m not sure of the pin drive currents on the Pico, but can you power the sensors off a pin? At least then you can programmatically power cycle them if you need to.
The Pico also has a watchdog, you could set it up to give it a reboot if things don’t respond in time. It doesn’t solve the issues of course but at least it gets it back to a workable state. And if the watchdog fails, or it works but there’s still no USB serial, then that would point towards power instabilities or somesuch.
can you power the sensors off a pin? At least then you can programmatically power cycle them if you need to.
Not really, at least I wouldn’t really know how. The sensor needs +3V, GND and two data pins, and the pico only has one 3V out, that seems to connect directly to the voltage regulator. I guess, I could try to use a GPIO pin as a switchable 3V out, but that seems rather hacky.
The Pico also has a watchdog, you could set it up to give it a reboot if things don’t respond in time. It doesn’t solve the issues of course but at least it gets it back to a workable state
Unfortunately, a simple machine.reset() seems not to be enough. I didn’t use “real” watchdogs, but what a Java dev knows: try/catch. I wrapped the entire loop in a try/except block and simply call machine.reset() if something goes wrong. That does work sometimes, but not always. Of course the possibilities are a) some exception isn’t getting caught b) there is some electronic issue, maybe the SPI interface to the DHT is “stuck” or not properly reset. I can resolve the problems by completely disconnecting the device for a while from power.
I had something similar happen in one of my ESP8266 projects (also running MicroPython). What I wound up doing was, every five wall clock minutes (maybe a bit sooner than that, for your case) I had my firmware do a
local_networks = wifi.scan()
just to exercise the wifi functionality. If that failed I have the code dogc.collect()
followed bysys.exit(1)
, which causes the 8266 to reboot automatically.Give that a try.
I’ll give it try!
Do you have any idea, what’s causing the issue? Is it specifically the scanning part that’s relevant here? I’m starting/stopping wifi each minute, so the chip shouldn’t just idle around all the time.
No, I don’t. My best informed guess is that the wifi connection’s state machine gets stuck once in a while, it misses a couple of packets, and then sits there doing nothing. So, by kicking it a little it doesn’t get a chance to freeze up.
I “kind of” solved at least parts of the problem by simply not turning wifi off. It’s more stable than before, but not stable as such.
Edit: I played around a bit and I think the problem is somewhere in the power supply. My workbench PC can drive the board just fine for hours, just plug it into the USB port, connect serial monitor and let it do its thing. However, if I’m plugging it into my router or a usb power supply, it stops working after a while again.
My theory is, that the PSU/router thinks the device is dead, because it draws too little power? That would explain, why not shutting down wifi improves the stability - it draws more power.
Now, that is just my naive theory, I’m not entirely sure, how to validate it. Maybe I’ll just add a useless busy-loop to increase power draw or add some power consuming components.
Do you have a pull up on the DHT20? If not that could explain the reliability issues. 4.7k is needed according to the datasheet.
Not too familiar with the RPi Pico but you might need to disable the internal pull ups if you do that but they are likely too weak so I wouldn’t rely on them.
I looked it up here: https://aqicn.org/air/sensor/spec/asair-dht20.pdf (I guess that’s the same datasheet you used)
There it says:
To avoid signal conflicts, the microprocessor (MCU) must only drive SDA and SCL at low level. An external pull-up resistor (for example: 4.7kΩ) is required to pull the signal to a high level. The pull-up resistor has been included in the I/O circuit of the DHT20 microprocessor.
This sounds to me like it’s already present in the package? I also haven’t seen any tutorial using any resistors (though that may be just an “error” made by all of them to keep it simple).
Yeah on that specific board it looks like it’s included . I was just going from experience. I just wired such a sensor to an Arduino the other day and I was having problems without the pull up. I was seeing garbled data packets on the data line.
edit: you can easily confirm this by measuring the resistance between VCC and Data on the sensor.