| Author |
Message |
|
|
Posted: Jul 12, 2012 - 05:52 AM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
Hi guys
I have been playing around with an AVR chip, now I want to read a digital data from a sensor.
say this one for example:
http://www.micro4you.com/files/sensor/DHT11.pdf
On section 5 of the document, it explain the communication process, but have no idea how to read tho data except program the chip to read the data in the time it defined.
I haven't tried any of it because I thought there might be a better way to do it.
And here am I, asking for help/advice/info/idea.
General information and specify answer to this sensor are all welcome
Thanks |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 07:34 AM |
|

Joined: Dec 30, 2004
Posts: 8789
Location: Melbourne,Australia
|
|
There is a Arduino library for this device done by Adafruit. This is done in c++ for the Arduino but shouldn't be too much work to extract it and convert to normal C. There is not much code involved.
I bought one of these last week and connected it up to my Arduino. Worked as expected. I compared the readings against my other weather station and they were close enough.
These devices seem to be the best price/performance for the money. There's also the sensirion sht devices but they're a little more expensive. |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 09:15 AM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
I know nothing about C++ to be honest with you o_o, but if someone point me to the right direction, I am happy to try to convert it to C  |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 03:15 PM |
|

Joined: Dec 30, 2004
Posts: 8789
Location: Melbourne,Australia
|
|
| What happens if you Google arduino dht11 |
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 03:34 PM |
|


Joined: Sep 04, 2002
Posts: 21274
Location: Orlando Florida
|
|
| I have an arduino uno and I use my regular old c compiler to write regular old c programs and I have a little avrdude.bat file that runs avrdude and tells it to program the hex file using the arduino bootloader. One of the avrfreaks showed me how to do that. You should write a half a dozen little one page programs that just do one thing... like init the uart to 38400 and send UUUUUU in a loop. Next program can receive uart chars and do something like a and z for on and off of an led. At this point, you have a framework for a dht11 tester program with a little menu with an item 1) read humidity 2) read temperature etc. |
_________________ Imagecraft compiler user
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 04:08 PM |
|


Joined: Jul 18, 2005
Posts: 62371
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
Quote:
I use my regular old c compiler to write regular old c
Then you miss out on all the Arduino (C++) library code that is available for all kinds of ready made solutions
(which is kind of the point of Arduino). |
_________________
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 10:51 PM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
Hi bobgardner,
Thanks for you advice, from what I can understand is, AVR can talk to dht11 through uart at 38400?
if that's what you mean, that might get myself busy for a day or two.
bobgardner wrote:
I have an arduino uno and I use my regular old c compiler to write regular old c programs and I have a little avrdude.bat file that runs avrdude and tells it to program the hex file using the arduino bootloader. One of the avrfreaks showed me how to do that. You should write a half a dozen little one page programs that just do one thing... like init the uart to 38400 and send UUUUUU in a loop. Next program can receive uart chars and do something like a and z for on and off of an led. At this point, you have a framework for a dht11 tester program with a little menu with an item 1) read humidity 2) read temperature etc.
|
|
|
| |
|
|
|
|
|
Posted: Jul 12, 2012 - 11:59 PM |
|

Joined: Dec 30, 2004
Posts: 8789
Location: Melbourne,Australia
|
|
| The dht11 has its own special protocol and its documentation is not quite as good as it could be. That's why i'm suggesting you look at the Arduino code for this device. |
|
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 12:14 AM |
|


Joined: Sep 04, 2002
Posts: 21274
Location: Orlando Florida
|
|
| Well Cliff, C++ has been around for a couple of decades. Perhaps you think its time I learned a little bit about it? You would think I already knew all about it if you saw my bookshelf. I have the Soustroup book, C++ for C programmers, etc etc. All I know is I'm approximately 20 years behind the State Of The Art. You want to give me some C++ assignments or mini lessons? |
_________________ Imagecraft compiler user
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 12:32 AM |
|

Joined: Dec 30, 2004
Posts: 8789
Location: Melbourne,Australia
|
|
| Bob, write and/or modify a library for Arduino might be a good start. |
|
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 01:39 AM |
|


Joined: Sep 04, 2002
Posts: 21274
Location: Orlando Florida
|
|
| I had a Big Think about this when I got the Arduino. Lets say I have a class with some data declarations and some methods and a constructor. I guess I can access the methods in a class with the dot notation just like I can access the fields in a struct. I guess I can make all the data global and all the methods functions with a prefix similar to the class name and sort of manually edit it into c. But Johan told me this path is full of problems, so maybe I ought to just bite the bullet and actually write a c++ console app in vc6? Like an airplane simulator where I have an engine class with data like rpm and fuel flow and oil pressure and temperature and methods like gets and sets for the data params (I always thought it was nuts to have to call a subroutine to fetch a variable. Just load it up into a register from an absolute ram address). Now I want 4 engines so I new the engine class (instantiate?) 4 times. I have no idea how to do this in c. I guess it would be 4 engine structs with records for rpm, temp, oil pressure yada yada. As you can see, I know just about enough to get all tangled up in a big mess. |
_________________ Imagecraft compiler user
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 04:20 AM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
|
Kartman wrote:
The dht11 has its own special protocol and its documentation is not quite as good as it could be. That's why i'm suggesting you look at the Arduino code for this device.
As much as I don't want to, it looks like I have to dig into it and learn some c++ along the way. |
|
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 05:09 AM |
|

Joined: Dec 30, 2004
Posts: 8789
Location: Melbourne,Australia
|
|
Look here:
http://arduino.cc/playground/Main/DHT11Lib
There's not much code to translate. Effectively, most of it is C, the real issue is the Arduino specific functions like DigitalWrite and the delay functions which can be translated fairly readily into 'normal' avr-gcc. As you can see there is only about a page of code and it is pretty straightforward.
Pretty much any device worth interfacing to for hobbyists - there'a an Arduino example for it with a nice little write up. Don't get me wrong - the Arduino is not the be all and end all of embedded systems, but it has its place and can get you moving much quicker than going bare metal especially for beginners.
Bob - whilst the concept of 'accessors' is a bit alien to those who would count each cycle when writing in assembler, it all comes down to a balance of performance vs utility. Having a block of code that is not intimately tied to other code makes for easier maintenance and testing. You may have to pay for this in run-time performance. If it is something that is not accessed at a high rate, then the penalty will be minimal - what's 1us extra for something accessed every second? Besides, the compiler might pull some tricks to inline the code to make it near identical to if we accessed it directly. In this instance it is win-win as we get the niceness of information hiding for us humans and the run time performance of direct access. |
|
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 10:19 AM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
Thanks Kartman,
I pretty much stop reading as soon as I read the title C++, but it looks like it's not so bad in this case. And I will look into it.
And the reason I want to learn AVR chip is, I simply like electronics, I want to find out how it works, and I find it interesting. But thanks for you nice advice, I really appreciate it!
Kartman wrote:
Look here:
http://arduino.cc/playground/Main/DHT11Lib
There's not much code to translate. Effectively, most of it is C, the real issue is the Arduino specific functions like DigitalWrite and the delay functions which can be translated fairly readily into 'normal' avr-gcc. As you can see there is only about a page of code and it is pretty straightforward.
Pretty much any device worth interfacing to for hobbyists - there'a an Arduino example for it with a nice little write up. Don't get me wrong - the Arduino is not the be all and end all of embedded systems, but it has its place and can get you moving much quicker than going bare metal especially for beginners.
|
|
|
| |
|
|
|
|
|
Posted: Jul 13, 2012 - 02:12 PM |
|

Joined: Dec 30, 2004
Posts: 8789
Location: Melbourne,Australia
|
|
|
|
|
|
|
Posted: Jul 16, 2012 - 03:07 PM |
|

Joined: Mar 12, 2010
Posts: 22
|
|
|
|
|
|
|
Posted: Jul 17, 2012 - 09:32 AM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
Hi cybeerhawk
although I don't have time to try using those library, the files you provided looks promising.
I will report back how it works after I try it out.
Thanks
cyberhawk wrote:
|
|
|
| |
|
|
|
|
|
Posted: Jul 20, 2012 - 03:46 AM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
It looks like the code for AVR is using a different IDE, so the code is not compatible with AVR Studio.
bug13avr wrote:
I will report back how it works after I try it out.
Thanks
cyberhawk wrote:
|
|
|
| |
|
|
|
|
|
Posted: Aug 05, 2012 - 12:11 PM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
thanks for everyone's help.
so I finally figure how to read the data from the sensor, here is my code, any feedback/criticizes to improve it is welcome!
I take it as my learning exercise
Zhuhua |
|
|
| |
|
|
|
|
|
Posted: Aug 05, 2012 - 12:17 PM |
|

Joined: Dec 30, 2004
Posts: 8789
Location: Melbourne,Australia
|
|
| As an output, you only need to pull the data pin low. Therefore if you set the port bit to 0, you only need to set/clear the ddrb bit. Forcing the output high might cause contention with the dht11 device. |
|
|
| |
|
|
|
|
|
Posted: Aug 06, 2012 - 12:18 AM |
|

Joined: Mar 18, 2012
Posts: 138
|
|
Hi Kartman
Thanks for your feedback, but I don't quite follow.
If you are talking about the dht11StartSignal() function, the data sheet say pulling up is the MCU signal.
Zhuhua
Kartman wrote:
As an output, you only need to pull the data pin low. Therefore if you set the port bit to 0, you only need to set/clear the ddrb bit. Forcing the output high might cause contention with the dht11 device.
|
|
|
| |
|
|
|
|
|