EmbDev.net

Forum: ARM programming with GCC/GNU tools DHT22 Sensor readings unrealisitc with STM32F407 uC


von Bernhard Ebner (Guest)



Rate this post
useful
not useful
First of all i want to say hello

I have a problem with my DHT22 temperature and humidity sensor. The 
start signal is fine, the sensor response signal is fine, the bits that 
the sensor sends me are fine my problem is how can i read out the data 
that the sensor sends me because my values are 2700 for humidity and 
between 4500 and 5800 for temperature. As you will see in the attatched 
file, i hope it works this is my first post, i have tried it using a for 
loop and after the sensor pulls the data line to 0 for the 50us low 
voltage that comes before every bit, i will put the datasheet down 
below, i wait until the data line gets pulled high and because a 0 will 
be 26-28us high i wait for 40us and if it is still high i will write a 1 
otherwise i will write a 0 in the variable.

Data Sheet:
https://cdn-shop.adafruit.com/datasheets/Digital+humidity+and+temperature+sensor+AM2302.pdf

A bit more information about the protocol:
https://www.waveshare.com/wiki/DHT22_Temperature-Humidity_Sensor

I will also put the code in here in case my attatched file doesnt work:

I am using CodeBlocks to write my code
1
#include "SmartcontrolOS.h"
2
#include "Apps.h"
3
#include "CMSIS.h"
4
void Temperature_Humidity(ui32 dt){
5
6
    static bool ini_OK = false;
7
8
9
     LCD__write(_,"%2.0f",wf(dd));
10
11
      if(!ini_OK) Scheduler__wait(_,3*sec); // start signal block
12
      DO__write_open_drain(_,PA4,1);
13
      DO__write_open_drain(_,PA4,0);
14
      Scheduler__wait(_,10*ms);
15
      DO__write_open_drain(_,PA4,1);
16
      Scheduler__wait(_,40*us);
17
      DI__read(_,PA4);
18
19
20
21
Scheduler__wait(_,40*us);            //check sensor reply block
22
23
if(DI__read(_,PA4) == 0)  Scheduler__wait(_,90*us);
24
25
26
while(DI__read(_,PA4) == 0);
27
28
29
ui8 sum = 0;
30
ui8 temp1 = 0, temp2 = 0;
31
ui8 feucht1 = 0, feucht2 = 0;
32
int i;
33
34
35
for(i = 39; i >= 0; i = i - 1)  //read data block
36
  {
37
38
  while(DI__read(_,PA4) != 1);
39
40
  Scheduler__wait(_,40*us);
41
42
    if(DI__read(_,PA4) == 1)
43
     {
44
       if     (i >= 32) feucht1 |= 1 << (i - 32);
45
       else if(i >= 24) feucht2 |= 1 << (i - 24);
46
       else if(i >= 16) temp1   |= 1 << (i - 16);
47
       else if(i >= 8)  temp2   |= 1 << (i-8);
48
       else             sum     |= 1 << i;
49
     }
50
51
  while(DI__read(_,PA4) == 0);
52
53
  }
54
55
56
57
 ui16 Temperature = 0, RH = 0;
58
59
 RH = (feucht1 << 8) | feucht2;
60
61
 Temperature = (temp1 << 8) | temp2;
62
63
64
65
66
LCD__write(_,"%8.1f\n%8.1f   %5.0f",wf(RH/10.0f),wf(Temperature/10.0f),wf(sum));
67
68
69
70
Scheduler__wait(_,3*sec);
71
ini_OK = true;
72
}
73
74
75
  void CPE_App__Wetterstation_T_RH(){
76
77
   Resources__ExBoard_init(_,"");
78
79
   while(1){
80
     const ui32 dt = 1*ms;
81
    Scheduler__wait(_,dt);
82
83
84
85
   Temperature_Humidity(dt);
86
87
88
   }
89
90
  }

von Bernhard E. (maymes)


Rate this post
useful
not useful
>      LCD__write(_,"%2.0f",wf(dd));

I just realised this line, ignore it i tried something.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.