DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Komfibrot
Posts: 7
Joined: Wed Oct 27, 2021 2:08 pm

DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by Komfibrot » Wed Oct 27, 2021 2:30 pm

I'm about to code Datalogger on a DEV-17745 or also known as the SparkfunThingsPlus. The Datalogger include an SDCard, RTC and the DHT11 Temperture and Humidty Sensor. I work with the dht.py library.

After i added more code (Check if Filename already exists on SDCard, otherwise the File on the SDCard wont be overwritten) I got the error message: "InvalidPulseCount: Expected 84 but got 76 pulses" and no Data can be collected by the DHT11.
I tried different wiring, different time of utime.sleep to give the DHT11 more time to cool down and refresh, but nothing worked out.

My Code:

Code: Select all

from machine import Pin, RTC
import machine
from dht import DHT11, InvalidChecksum
import sdcard
import uos
import utime


# DHT 11 setup
# Defining the Pin of the DHT11 Sensor
dhtPIN = 28
sensor = DHT11(Pin(dhtPIN, Pin.IN, Pin.PULL_UP))

# Real Time Clock (RTC) Setup
# The logged Data get a Time Reference
rtc = RTC()
rtc.datetime((2017, 8, 23, 2, 12, 48, 0, 0)) # set a specific date and
                                             # time, eg. 2021/10/18 11:21:48

# SD Card setup
# On the SD Card, the collected Data will be stored.
# Assign chip select (CS) pin (and start it high)
cs = machine.Pin(9, machine.Pin.OUT)


# Intialize SPI peripheral (start with 1 MHz)
spi = machine.SPI(1,
                  baudrate=1000000,
                  polarity=0,
                  phase=0,
                  bits=8,
                  firstbit=machine.SPI.MSB,
                  sck=machine.Pin(14),
                  mosi=machine.Pin(15),
                  miso=machine.Pin(12))

# Initialize SD card
sd = sdcard.SDCard(spi, cs)

# Mount filesystem
vfs = uos.VfsFat(sd)
uos.mount(vfs, "/sd")

# Check if Filename already exists on SDCard
fname = "/sd/Datalog.txt"
i = 0

while True:
    try:
        f = open(fname, "r")
        exists = True
        f.close()
        print ("File " +str(fname)+ " exists")
        i +=1
        fname = "/sd/DataLog_"+str(i)+".txt"
        utime.sleep(0.01)
    except:
        exists = False
        print("File Datalog_" + str(i) +".txt does not exists")
        break




    
# Create the file with the non-existing Filename and write the Headline 
with open(fname, "w") as file:
    file.write("Date" + "\t"+ "Temperature" + "\t" + "Humidity" + "\r\n")


    while True:
       
        # Temperature will be saved on the Variable temp        
        temp  = (sensor.temperature)
        
        # Humidity will be saved on the Variable hum 
        hum = (sensor.humidity)
        
        print(rtc.datetime())
        
        file.write(str(rtc.datetime()) + "\t"+ str(temp) +"" + "\t" + str(hum) +"%" +"\r\n") # \t produces a tab between the Values. With the Function read.delim() in RStudio: for reading “tab-separated value” files (“.txt”). By default, point (“.”) is used as decimal points.
        
        # The Intervall for DHT11 has to be greater than 4 Seconds
        utime.sleep(4)


I would be grateful, if you can give me any hint. Thank you!

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by scruss » Wed Oct 27, 2021 7:21 pm

You need to call sensor.measure() before trying to read the temperature and humidity

Komfibrot
Posts: 7
Joined: Wed Oct 27, 2021 2:08 pm

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by Komfibrot » Thu Oct 28, 2021 8:32 am

Thank you for your advice!

Unfortunately, it doesnt fix the problem. It seems to have problem with the creating the file on the sd and then start to write. Because if the file already exists on the sd card, the DHT11 is able to collect the data.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by scruss » Thu Oct 28, 2021 9:33 pm

That's a problem with your file code. You'll still need to call measure() to actually get any results, and I think that's where your error message is coming from.

You know you can do

Code: Select all

f=open("file.txt", "a")
to create a new file if it doesn't exist, or append if it does?

Komfibrot
Posts: 7
Joined: Wed Oct 27, 2021 2:08 pm

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by Komfibrot » Tue Nov 02, 2021 3:01 pm

I changed the code following your advices and added the the append and the sensor.measure() function. Now i often get 80 or 82 pulses of the needed 84. A little improvement, but it's still not working.

Code: Select all

while True:
    try:
        f = open(fname, "r")
        exists = True
        f.close()
        print ("File " +str(fname)+ " exists")
        i +=1
        fname = "/sd/DataLog_"+str(i)+".txt"
    
    except:
        exists = False
        print("File Datalog_" + str(i) +".txt does not exists")
        # Create the file with the non-existing Filename and write the Headline 
        with open(fname, "w") as file:
            file.write("Date" + "\t"+ "Temperature" + "\t" + "Humidity" + "\r\n")
            file.close()
            break


while True:
    # The Intervall for DHT11 has to be greater than 4 Seconds
    utime.sleep(5)
    
    sensor.measure()
    # Temperature will be saved on the Variable temp        
    temp  = (sensor.temperature)
        
    # Humidity will be saved on the Variable hum 
    hum = (sensor.humidity)
        
    print(rtc.datetime())
    
    file = open(fname, "a")
    file.write(str(rtc.datetime()) + "\t"+ str(temp) +"" + "\t" + str(hum) +"%" +"\r\n") # \t produces a tab between the Values. With the Function read.delim() in RStudio: for reading “tab-separated value” files (“.txt”). By default, point (“.”) is used as decimal points.
    file.close()   

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

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by Roberthh » Tue Nov 02, 2021 4:05 pm

Do you have a 4.7k Pull-up resistor at the DHT11 data pin? The internal pull-up is not strong enough to ensore a good signal quality.

Komfibrot
Posts: 7
Joined: Wed Oct 27, 2021 2:08 pm

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by Komfibrot » Thu Nov 04, 2021 10:22 am

Thank you for your advice,
I tried it with the pull-up and without and nothing changed regarding the problems with the recieved pulses.

If i try a simple code like the following, It works out fine.

Code: Select all

from machine import Pin
from dht import DHT11, InvalidChecksum
import utime


# Defining the Pin of the DHT11 Sensor
pin = Pin(26, Pin.OUT, Pin.PULL_DOWN)
sensor = DHT11(pin)


while True:
#     The Sensor refreshes every 2 Seconds, therefore a faster cycle would not bring a better outcome.
    utime.sleep(2)
    
    sensor.measure()

    # Save Temperature-Data on variable t
    t  = (sensor.temperature)
    # Save Humidity-Data on variable h
    h = (sensor.humidity)
    
    # Show Temperature and Humidity in the Shell:
    print("Temperature: {}".format(sensor.temperature))
    print("Humidity: {}".format(sensor.humidity))
    
But when I extend the code with the code above for Datalogging, then I recieve Error Messages.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by scruss » Thu Nov 04, 2021 4:03 pm

DHT sensors are unusual in that they'll sometimes fail a measure() for really no good reason. Reading them in a loop without checking for exceptions will mostly work, then fail for no reason

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

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by Roberthh » Thu Nov 04, 2021 4:27 pm

The standard implemenation of DHT uses a polling loop to time the pulses. That can be interfered by other activities. There have been at least two DHT reading implementations using the PIO for that task, which cannot be interrupted by other code. One complete solution is here: https://github.com/danjperron/PicoDHT22

Komfibrot
Posts: 7
Joined: Wed Oct 27, 2021 2:08 pm

Re: DHT11: InvalidPulseCount: Expected 84 but got 76 pulses

Post by Komfibrot » Fri Nov 05, 2021 12:10 pm

Thanks to scruss and Roberthh for you helpf! The DHT-Library with PIO works out pretty good.

Unfortunately I get no decimal number for Temperature anymore, which would be helpful for a datalogger.

Post Reply