DHT11/AM2301 does not work

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
aik
Posts: 4
Joined: Sun Oct 07, 2018 9:07 am

DHT11/AM2301 does not work

Post by aik » Sun Oct 07, 2018 10:03 am

I have ESP12E devkit and AM2301 sensor, a while ago tried that with LUA firmware and it worked well, now trying micropython and it does not, reports timeout on .measure(), debugging (built my own + printf) points to: https://github.com/micropython/micropyt ... /dht.c#L67

The very first (after the am2301 gets powered) call to .measure() actually does not time out and seems succeeded even though .temperature() shows 0 (which it wrong) and consequent calls give "tmeout". Why is that?

Tried different pins, verified with LED that the pin is correct:
>>> p = machine.Pin(13, machine.Pin.OUT)
>>> p.on()
>>> p.off()

Then replaced LED with the am2301 + pullup, reset the esp, and:

>>> import machine, dht
>>> d=dht.DHT11(machine.Pin(13))
>>> d.measure()
>>> d.temperature()
0
>>> d.measure()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "dht.py", line 16, in measure
OSError: [Errno 110] ETIMEDOUT

update:
I added some printfs into the 40bit loop, the ticks (%d) are (but timeout always occurs in 38th or 39th iteration):
25 5 25 25 26 73 25 25 73 74 75 26 73 27 72 25 26 27 27 27 25 25 25 74 26 73 25 26 27 74 25 26 25 26 73 74 26 26 72
which translates to:
04 ea 01 44 99
which does not seem right. Hmmm.

User avatar
Roberthh
Posts: 1074
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: DHT11/AM2301 does not work

Post by Roberthh » Sun Oct 07, 2018 10:40 am

The AM2301 is equivalent to the DHT22.
So try:
d=dht.DHT22(machine.Pin(13))
That works for me.

aik
Posts: 4
Joined: Sun Oct 07, 2018 9:07 am

Re: DHT11/AM2301 does not work

Post by aik » Sun Oct 07, 2018 11:32 am

Tried DHT22 too, same problem. The problem code is common for both so it could not have made a difference really.

User avatar
emtee
Posts: 3
Joined: Thu Jun 14, 2018 4:55 pm
Location: Alberta, Canada

Re: DHT11/AM2301 does not work

Post by emtee » Sun Oct 07, 2018 4:58 pm

Try the following code:

from machine import Pin
import dht

# Use GPIO14 (D5 - NodeMCU)
my_dht = dht.DHT22(Pin(14, Pin.IN, Pin.PULL_UP))

# Read and show the data from DHT
my_dht.measure()
my_dht.temperature()
my_dht.humidity()

This is working on a NodeMCU board.

aik
Posts: 4
Joined: Sun Oct 07, 2018 9:07 am

Re: DHT11/AM2301 does not work

Post by aik » Mon Oct 08, 2018 11:11 am

emtee wrote:
Sun Oct 07, 2018 4:58 pm
Try the following code
Thanks, this is definitely some progress - now measure() works once and the readings are correct but the consequent call to measure() fails after timeout, only reconnecting power to am2301 helps. It is not a deal breaker though as I can power it via another gpio but kinda ugly :) What is the problem now?

User avatar
emtee
Posts: 3
Joined: Thu Jun 14, 2018 4:55 pm
Location: Alberta, Canada

Re: DHT11/AM2301 does not work

Post by emtee » Mon Oct 08, 2018 4:19 pm

How quickly are you using .measure() to read the AM2301?

I believe these sensors work best when read no more than once every two seconds.

aik
Posts: 4
Joined: Sun Oct 07, 2018 9:07 am

Re: DHT11/AM2301 does not work

Post by aik » Tue Oct 09, 2018 1:03 am

Yeah, I know that "feature", I tried with more than 10 seconds, same thing. The driver in nodemcu/lua firmware does some dance before talking to dht, may be mpython should too, I'll give it a try.

Post Reply