dhtnew_pulse_diag.ino (3715B)
1 // 2 // FILE: dhtnew_pulse_diag.ino 3 // AUTHOR: Rob Tillaart 4 // VERSION: 0.1.1 5 // PURPOSE: DHTNEW library pulse measurement tool - diagnostics 6 // URL: https://github.com/RobTillaart/DHTNew 7 // 8 // HISTORY: 9 // 0.1.0 2020-08-15 initial version 10 // 0.1.1 2020-09-23 commented noInterrupts as it corrupts timing on AVR. 11 12 // 13 // DHT PIN layout from left to right 14 // ================================= 15 // FRONT : DESCRIPTION 16 // pin 1 : VCC 17 // pin 2 : DATA 18 // pin 3 : Not Connected 19 // pin 4 : GND 20 21 #include "Arduino.h" 22 23 #define DHTLIB_DHT11_WAKEUP 18 24 #define DHTLIB_DHT_WAKEUP 1 25 26 uint8_t _dataPin = 16; 27 uint8_t _wakeupDelay = DHTLIB_DHT_WAKEUP; 28 29 uint16_t count = 0; 30 uint32_t times[100], t = 0; 31 uint8_t idx = 0; 32 33 void setup() 34 { 35 Serial.begin(115200); 36 Serial.println("dhtnew_pulse_diag.ino"); 37 Serial.println(); 38 39 // default should be HIGH 40 pinMode(_dataPin, OUTPUT); 41 digitalWrite(_dataPin, HIGH); 42 } 43 44 void loop() 45 { 46 measure(); 47 dump(); 48 delay(5000); 49 } 50 51 void dump() 52 { 53 int i = 1; 54 Serial.println(); 55 Serial.print("RUN:\t"); Serial.println(count); 56 Serial.print("IDX:\t"); Serial.println(idx); 57 58 Serial.println("WAKEUP"); 59 for (int n = 0; n < 6; n++) 60 { 61 Serial.print("\t"); 62 Serial.print(times[i++]); 63 } 64 Serial.println(); 65 66 Serial.println("HUM"); 67 for (int n = 0; n < 32; n++) 68 { 69 Serial.print("\t"); 70 Serial.print(times[i++]); 71 if ((n & 15) == 15) Serial.println(); 72 } 73 Serial.println(); 74 75 Serial.println("TEMP"); 76 for (int n = 0; n < 32; n++) 77 { 78 Serial.print("\t"); 79 Serial.print(times[i++]); 80 if ((n & 15) == 15) Serial.println(); 81 } 82 Serial.println(); 83 84 Serial.println("CRC"); 85 for (int n = 0; n < 16; n++) 86 { 87 Serial.print("\t"); 88 Serial.print(times[i++]); 89 if ((n & 7) == 7) Serial.println(); 90 } 91 Serial.println(); 92 93 Serial.println("BYE"); 94 for (int n = 0; n < 2; n++) 95 { 96 Serial.print("\t"); 97 Serial.print(times[i++]); 98 } 99 Serial.println(); 100 Serial.println(); 101 } 102 103 104 void measure() 105 { 106 count++; 107 yield(); // handle pending interrupts 108 109 // reset measurements table 110 idx = 0; 111 t = micros(); 112 for (int i = 0; i < 100; i++) times[i] = 0; 113 114 times[idx++] = micros(); 115 // REQUEST SAMPLE - SEND WAKEUP TO SENSOR 116 pinMode(_dataPin, OUTPUT); 117 digitalWrite(_dataPin, LOW); 118 // add 10% extra for timing inaccuracies in sensor. 119 delayMicroseconds(_wakeupDelay * 1100UL); 120 121 122 times[idx++] = micros(); 123 // HOST GIVES CONTROL TO SENSOR 124 pinMode(_dataPin, INPUT_PULLUP); 125 126 // DISABLE INTERRUPTS when clock in the bits 127 // noInterrupts(); // gives problems on AVR 128 129 130 times[idx++] = micros(); 131 // SENSOR PULLS LOW after 20-40 us => if stays HIGH ==> device not ready 132 while (digitalRead(_dataPin) == HIGH); 133 134 135 times[idx++] = micros(); 136 // SENSOR STAYS LOW for ~80 us => or TIMEOUT 137 while (digitalRead(_dataPin) == LOW); 138 139 140 times[idx++] = micros(); 141 // SENSOR STAYS HIGH for ~80 us => or TIMEOUT 142 while (digitalRead(_dataPin) == HIGH); 143 times[idx++] = micros(); 144 145 146 // SENSOR HAS NOW SEND ACKNOWLEDGE ON WAKEUP 147 // NOW IT SENDS THE BITS 148 149 // READ THE OUTPUT - 40 BITS => 5 BYTES 150 for (uint8_t i = 40; i != 0; i--) 151 { 152 times[idx++] = micros(); 153 // EACH BIT START WITH ~50 us LOW 154 while (digitalRead(_dataPin) == LOW); 155 156 times[idx++] = micros(); 157 // DURATION OF HIGH DETERMINES 0 or 1 158 // 26-28 us ==> 0 159 // 70 us ==> 1 160 while (digitalRead(_dataPin) == HIGH); 161 } 162 163 164 times[idx++] = micros(); 165 // After 40 bits the sensor pulls the line LOW for 50 us 166 // TODO: should we wait? 167 while (digitalRead(_dataPin) == LOW); 168 169 times[idx++] = micros(); 170 times[idx++] = micros(); 171 172 for (int n = 100; n > 0; n--) 173 { 174 times[n] -= times[n - 1]; 175 } 176 } 177 178 179 // -- END OF FILE --