Rei Vilo 695 Posted June 15, 2012 Share Posted June 15, 2012 SOLVED! Jump to library below Hi!I bought from Adafruit a DHT22 sensor that provides temperature and relative humidity. It uses a one-wire connection.DHT22_main.zip Quote Link to post Share on other sites
Genesisfactor 2 Posted June 16, 2012 Share Posted June 16, 2012 I happen to have that sensor and am playing with that board. Will read the tutorial and try to reconstruct the experiment with what i have here (about an hour away from home for the weekend). ***EDIT (continually)*** I don't think i have a 10K resistor to play with. It is really interesting that you are getting what seem to be 16 bit results with errors (the 65535). When you used the pull up resistor, you either got high or low, so that didn't help. It seems like the sampling isn't being done correctly. BTW, did you try removing the atmega stuff from the h file? Quote Link to post Share on other sites
Rei Vilo 695 Posted June 17, 2012 Author Share Posted June 17, 2012 Please find a LaunchPad-only version of the sketch (attached). I've cleaned the code as much as I could, swiped out the float and added extra prints for debugging. I tried changing the value of the resistor. 10 k? is for 5 V, so I tried 4.7 for k? is for 3.3 V, to no avail. I suspect the sampling, and the millis() and micros() used. Any idea to make it work? Good luck! DHT22_430_main.zip bluehash 1 Quote Link to post Share on other sites
bluehash 1,581 Posted June 17, 2012 Share Posted June 17, 2012 Does it work well now? Quote Link to post Share on other sites
Rei Vilo 695 Posted June 17, 2012 Author Share Posted June 17, 2012 Unfortunately, no I still need help from more experienced LaunchPad users... Quote Link to post Share on other sites
energia 485 Posted June 17, 2012 Share Posted June 17, 2012 Looking at your code the problem probably is the delayMicroseconds() not being accurate. I thought I hooked it up to a scope and confirmed that it was indeed close to 1us. Will hook it up again and run some tests on the delayMicroseconds(). Robert Quote Link to post Share on other sites
Rei Vilo 695 Posted June 17, 2012 Author Share Posted June 17, 2012 Thank you for your help and for the precise measures! Problem is, I don't have an oscilloscope :roll: The LaunchPad has a place for the optional crystal. Does the quartz provide a better accuracy? Quote Link to post Share on other sites
energia 485 Posted June 19, 2012 Share Posted June 19, 2012 We should be able to get an accurate enough delayMicroseconds() without a crystal. Will revisit the delayMicroseconds() implementation this week. Quote Link to post Share on other sites
zeke 693 Posted June 19, 2012 Share Posted June 19, 2012 1-wire protocol works best with a 16MHz DCO frequency. It does not work with a 1MHz DCO frequency unless oPossum waves his magic ASM wand bluehash and Rei Vilo 2 Quote Link to post Share on other sites
oPossum 1,083 Posted June 19, 2012 Share Posted June 19, 2012 That is sometimes true. I used asm to do Dallas/Maxim one wire @ 1 MHz+, 115200 bps serial @ 1 MHz and 2400 bps serial at 32 kHz. The timing of those was very demanding. The DHT11/DHT22 protocol can be written in C and run at 1 MHz - I have done that. It requires careful coding to get the necessary efficiency. Each bit has to be processed in 70 clock cycles or less. zeke and Rei Vilo 2 Quote Link to post Share on other sites
energia 485 Posted June 19, 2012 Share Posted June 19, 2012 I have compared the delayMicroseconds() for Arduino uno and LaunchPad using the following snippet of code. Both boards yield the same result. I have attached the scope shots. It will be hard to get to the bottom of this without having the actually sensor. Will order one from Adafruit. To be continued... void setup() { pinMode(GREEN_LED, OUTPUT); } void loop() { noInterrupts(); delayMicroseconds(1); digitalWrite(GREEN_LED, HIGH); delayMicroseconds(1); digitalWrite(GREEN_LED, LOW); } Quote Link to post Share on other sites
bluehash 1,581 Posted June 20, 2012 Share Posted June 20, 2012 Will order one from Adafruit. To be continued... 43oh can sponsor one DHT22. Just let me know the amount, I'll paypal it to you. Quote Link to post Share on other sites
energia 485 Posted June 20, 2012 Share Posted June 20, 2012 @bluehash, thank you for the offer but it's OK, I'll take one for the team :-) Quote Link to post Share on other sites
Rei Vilo 695 Posted June 20, 2012 Author Share Posted June 20, 2012 I have the DHT22 and it runs fine with the Arduino but I lack an oscilloscope. My logic analyser doesn't feature adjustable trigger value, so I can't use it for 3.3V signals. Sorry, I can't help! Quote Link to post Share on other sites
energia 485 Posted June 26, 2012 Share Posted June 26, 2012 Conclusion is that the original sketch depends on delays caused by digitalRead(). On MSP430 a digitalRead takes about 2.5 us and on AVR about 3.8 us. This difference + more efficient code on MSP430 caused the below counting loop to be about 3 us shorter on MSP430 vs AVR resulting in more counts than the code anticipates. Replacing the delayMicroseconds(1) with delayMicroseconds(3) compensates for the difference in timing and makes it all work. while (digitalRead(_pin) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } An alternative work-around would be to compare counter to a higher number in the if-statement below. if ((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes data[j/8] <<= 1; if (counter > 6) data[j/8] |= 1; j++; } } Robert xv4y, bluehash, Rei Vilo and 1 other 4 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.