Problems reading the DHT11 sensor

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Oderlando
Posts: 37
Joined: Tue Jul 17, 2018 11:30 am
Has thanked: 7 times
Been thanked: 3 times
Contact:

Problems reading the DHT11 sensor

Post by Oderlando »

Hi. I'm having trouble reading a DHT11 sensor. I was able to read the sensor normally in other flowcharts, but in this particular flowchart I did not succeed. The purpose of this flowchart is to make a Loop work until the sensor returns with the humidity value. After this, the humidity value is shown on the display and the required loop number for this is also shown on the display. Can anyone tell me what could be wrong here? Thanks for any help. I use flowcode 8. I am sending the attached flowchart.
Attachments
Detector_DHT11_Loop.fcfx
(10.28 KiB) Downloaded 233 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Problems reading the DHT11 sensor

Post by mnf »

There is no way out of this loop:
Capture.PNG
(17.66 KiB) Downloaded 2748 times
Humidity is always >0 (except maybe in the Atacama?) Do you mean

Code: Select all

Until Humidity > 0
or use a flag and set to true if sensor read ok?

Martin

Happy New Year

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Problems reading the DHT11 sensor

Post by chipfryer27 »

Hi

I agree with Martin (and I only know of that desert thanks to a recent rerun of a Top Gear Special :D ).

If you set your "loop while" value to something more reasonable instead of "0", like perhaps your actual target value then it will escape.

Also, you are running at 20Mhz. I haven't used the DHT11 but I don't think it will take very long to sample. Therefore it will loop very quickly indeed (guessing many, many thousands of times per second) and you will soon exceed the Uint range of your variable "x". It will start counting all over again from 0 every time it reaches it's limit so this means that your count "if" it escapes your loop will be incorrect.

I don't know your intended application but do you really need to sample so quickly? Perhaps consider including a delay in your loop?

Hope this is of some help.

Regards

EDIT..>
I just read this very informative post in the forum which explains the operation of this sensor in detail even giving operational limits. I suspect your "loop while" value may perhaps be a typo? Also whilst you won't be looping thousands of times each second you will exceed your "x" range in minutes.
viewtopic.php?f=7&t=11555
Last edited by chipfryer27 on Wed Jan 02, 2019 2:12 pm, edited 1 time in total.

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Problems reading the DHT11 sensor

Post by LeighM »

Perhaps you intended while Sensor > 0 ?

Oderlando
Posts: 37
Joined: Tue Jul 17, 2018 11:30 am
Has thanked: 7 times
Been thanked: 3 times
Contact:

Re: Problems reading the DHT11 sensor

Post by Oderlando »

Happy New Year to all !. I'm putting together a controller for incubators. Several functions are performed at the same time, including a stopwatch for a clock. This way, I can not enter loop. I am trying to get the sensor to read and can determine how much time has been spent for this by correcting the timer's time measurement. I am trying to make the loop terminate when the sensor returns with some nonzero value.

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Problems reading the DHT11 sensor

Post by chipfryer27 »

Hi

From the post I referenced earlier, the sensor can only read humidity between 20% and 95%. Not having played with this I don't know what that will correspond to as a returned value. Maybe someone that has used the sensor can provide details? However from the component macro, it states that if the value is 15.8% then it will return 15%.

As you are unlikely to get 0% humidity even in your incubator we can assume that all readings will be above zero as the sensor will always return with a non zero value (from what I understand, but I stress I haven't used it). Therefore as your "loop while" is set to >0 it will be in a continuous loop as every reading is greater than zero. As Martin pointed out, no escape.

You said:

"I am trying to make the loop terminate when the sensor returns with some nonzero value."

As you will always have a non zero value you don't need this loop at all, simply read the sensor. If however you are using this value to measure a change in humidity, escaping when a predetermined value has been reached then you will need your loop. Using your flowchart, if your predetermined target was 30% humidity and you set your "loop while" to >30, then it will loop until humidity drops to 30 or below.

Hope this helps a little.

Regards

Oderlando
Posts: 37
Joined: Tue Jul 17, 2018 11:30 am
Has thanked: 7 times
Been thanked: 3 times
Contact:

Re: Problems reading the DHT11 sensor

Post by Oderlando »

I have corrected my flowchart. In the flow chart, I used humidity equal to 0. The loop operates until the sensor shows some humidity value above 20%, ie the loop works until the sensor can be read and "x" shows how many loops were needed to read the sensor. In actual hardware testing the lcd shows that it took around 4000 loop to read the sensor. I was very hungry. Does the sensor take so long to read? I am sending the new flowchart..
Attachments
Detector_DHT11_Loop2.fcfx
(10.39 KiB) Downloaded 227 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Problems reading the DHT11 sensor

Post by chipfryer27 »

Hi

Full disclosure.... I have just came back from the pub, so any attempt at assistance may not necessarily be correct, especially as the keys on my keyboard aren't where I left them :)

From what I see in your chart you will loop until the sensor reports that humidity is greater than 20%, and from your post it took 4000 iterations to come back with that value.

You don't capture what the initial humidity value was before entering your loop, and whilst in the loop we don't know if you do anything that would physically alter the humidity, so it is difficult to know what is happening. Can you provide any further info regarding such?

It may be helpful to break the program down and check what is happening and when. If you capture and display the humidity before you enter the loop for a reference, that would be helpful. Also, you don't know if it took 4000 iterations because variable "sensor" =1 for part of the time or if variable "humidity" was less than 20%.

Assuming you personally are not doing anything to change the humidity it leaves two possibilities. First is that sensor = 1 meaning it failed to read the sensor, or that your humidity was below 20% and rising. The tolerance is +/-5% so you could have been reading slightly lower than actual. Maybe try averaging your readings?

Perhaps add in some delays to allow you to read / display values whilst in the loop? You certainly don't need speed whilst testing and being able to read / display can be very beneficial in debugging.

Regards

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Problems reading the DHT11 sensor

Post by mnf »

It's been mentioned before, the DHT11 is quite a slow sensor- so add a 2s delay to the read loop...

Oderlando
Posts: 37
Joined: Tue Jul 17, 2018 11:30 am
Has thanked: 7 times
Been thanked: 3 times
Contact:

Re: Problems reading the DHT11 sensor

Post by Oderlando »

I have a problem that I still can not solve. I'm trying in every possible way. I need to read the sensor at regular intervals, and at the same time make the timing of a clock. I am using a 16F877A and a crystal of 20MHZ (with very low frequencies it was not possible to read the sensor). I want a precise time measurement so I'm using 1: 1 interrupts. Soon, 1 second will have 19531 interruptions. The problem is that when I choose the ratio of 1: 1 on interrupts, there are errors in reading the sensor. Only by using 1: 256 interrupts can I read the sensor, but doing so there is loss of timing accuracy on account of the decimal number of frequencies. What can I do to get a good timing and at the same time I can read the sensor?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Problems reading the DHT11 sensor

Post by Benj »

Hello,

We would need to see your program to be able to advise really. The fast interrupt is likely effecting the timing of the DHT11 comms. So maybe check that the code in the interrupt macro is as tight as possible, ideally you should be incrementing a counter and that's it.

Can you disable the interrupt before sampling the DHT11 and then re-enable afterwards or would this mess things up?

It's possible to use the timer interrupt on a higher prescaler and then use a C icon to collect the timer count value and use this to increase your timing accuracy.

Post Reply