Monday, December 17, 2012

Reading Temperature and Humidity on AVR Atmega using DHT11 library 01

updated version: http://davidegironi.blogspot.it/2013/02/reading-temperature-and-humidity-on-avr.html

The DHT11 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed).


This library use DHT11 sensor to read temperature and humidity on on Atmega micro.
Setup the data port connected to the micro pin, and call the function to get humidity and / or temperature.

DHT11
  • Power supply 3.5-5.5V DC
  • Measuring range Humidity: 20-90%RH
  • Measuring range Temperature: 0-50 Celsius.

Setup parameter are stored in file dht11.h

Code

Notes
  • read risk disclaimer
  • excuse my bad english

44 comments:

  1. Thank you for the code!
    It's usefull.

    For others: Look for the newest version.
    Now it's avr_lib_dht_03.zip

    ReplyDelete
  2. the code can not work for me ...

    ReplyDelete
    Replies
    1. to me this code works on most ATmega family. if it does not works, double check wiring, check that your DHT11 works with other devices and libraries (like arduino), then use some kind of debugging tecnique (like uart output), to check where the code stucks.
      good work.

      Delete
  3. Salam,
    I am using DHT11 with atmega 16 on 3.3v supply. The microcontroller gives control signal between (3.3V and 0V i.e correct) But dht11 responds by giving signals between 3.3V (high value) and 1.5V(low value). Any idea why this might happening?
    Thank u in advance for ur help!

    ReplyDelete
    Replies
    1. It could be your power suppy, try to supply all your devices at 5V.
      DHT11 datasheet, Electrical Characteristics: Power supply: DC 3.5~5.5V.

      Delete
  4. Thanks for the quick response.
    I tried doing as u suggested. Now i run everything on 5 V but the problem has persisted.
    Now the dht11 responds between 5.0 (high) and 3.2V (low).
    Any other ideas ?

    ReplyDelete
    Replies
    1. you should check using a scope the response and compare your DHT11 to atmega communication line with the on contained in the datasheet.
      a simpler method should be use the uart to debug the code, to check where it stucks.

      Delete
  5. Hi,
    I test this code with atmega128 but I always have checksum error. Do you have any Idea what is wrong?
    thanks in advance

    ReplyDelete
    Replies
    1. Hello.
      Are you using avrgcc? Have you check your wiring? What kind of checksum error do you mean?

      Delete
    2. yes, my wiring seems right. exactly in this line of code:
      if ((uint8_t)(bits[0] + bits[1] + bits[2] + bits[3]) == bits[4])
      my program return always -1.
      and it is my output example:
      output example:
      1st 8 bit, 2nd 8 bit, 3rd 8 bit, 4th 8 bit, checksum 8 bit
      27 109 128 6 198
      27 108 0 27 24

      Delete
    3. I think I write in a wrong page. I am using this library:
      http://davidegironi.blogspot.de/2013/02/reading-temperature-and-humidity-on-avr.html#.Vm7L_dIrJpg

      Delete
    4. Can you check the bus lines with a scope or a logic analyzer?
      May vladan vesý October 3, 2015 comment of the post you have linked help?

      Delete
    5. I dont have the tools but I checked it with reading the signal with input capture. I think i recieve 38 pulse and the pulses should be more than 40 .
      has it any meaning?

      Delete
    6. here is example of dh11 header and inputcapture:

      bits[0]= 21(00010101) ic= 00011111
      bits[1]= 80(01010000) ic= 00000000
      bits[2]= 0 (00000000) ic = 00011001
      bits[3]= 162(10100010) ic= 0000000-
      bits[4] = 0 ic= -

      Delete
    7. You have to measure the pulse time, which must fit the datasheet timing table.

      Delete
    8. thanks for the time you gave to me.
      I exactly did that. I wrote here the result.
      0=20clk =50+27 micro second(almost...)

      Delete
    9. What pulse are you speaking of? If you take as example the Aosong DHT11 datasheet, the first Host signal must have a length of almost 18ms (milliseconds).

      Delete
    10. yes I have that. I write the example pulse queue I recieved. I have 250000 clk per second.and these are clk between each 2 raising edge:
      2-29503-(handshaking)24-35-(40 bit data)19-20-31-19-20-19-20-19-19-20-19-20-19-20-19-20-19-20-19-31-31-20-19-20-19-19-20-19-20-19-20-19-20-19-31-19
      I think all should be more than 40 but they are 39.

      Delete
    11. I send the 18 ms signal with dht11/12 header version 3 you wrote.
      and recieve the information both by inputcapture and this header.
      the give back diffreent result and any way I have checksum error.

      Delete
    12. Most of the time the DHT sensor comunication errors comes to incorrect signal timing. But if you have checked that the bus fits the datasheet, it could be your sensor. Check it with another micro, or even check it with another library.
      It happens to me to get checksum error sometimes, but it was due to emf noise.

      Delete
    13. Hey Davide !! I am also getting -1 for both the values !! Please Help !! I dont have pulse n all Knowledge...!!

      Delete
    14. Hello. Can you check the bus lines with a scope or a logic analyzer?

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. my code stucks on while loop after storing the result...please help

    ReplyDelete
  8. what do you think the problem is ???

    ReplyDelete
    Replies
    1. Hello. Double check your wiring. Check you F_CPU. Check your ATmega fuses. Check if the sensor is working using other hardware.

      Delete
  9. Can you explain me please.. I am using DHT11 with atmega16, which file I have to use from your uploads?

    ReplyDelete
  10. Hi Davide, I was wondering if your library would work on Atmega1284?

    Thank you!

    ReplyDelete
    Replies
    1. Sorry I didn't give a reason. I'm working on my final project for a class of mine in college. And I want to try to get the DHT11 to communicate with my Atmega1284 in order to create an Automated Windows/Shutter Opener/Closer

      Thank you again!

      Delete
    2. Hello, It should work. I've compiled it for ATmega1284P, it compiles except for the uart library, i suggest you to check the new P.Fleury library or find a ATmega1284 working one. Unluckly I've not any ATmega1284 to try it on. If i doesn't work double check your F_CPU. Let us know how it goes. Good work.

      Delete
    3. Hi Davide, thank you for the response. It doesn't seem to work for my Atmega1284. I tried including P.Fleury library for UART too. It doesn't seem to work still. Do you have any other suggestion? Thank you

      Delete
    4. Actually it does work, but I get a warning for my F_CPU. Do you happen to know what to do for this?

      Here is an attach picture to let you know what I mean.

      http://i.imgur.com/r6iY1TB.png

      Also when I get it working, how would I know that it is working correctly without having to display it on an LCD screen? I want to check if it is working like on a serial monitor just to test it out. Thank you

      Delete
    5. F_CPU, have you defined it in your compiler? What do you use as compiler and IDE? Take a look here: http://www.avrfreaks.net/forum/fcpu-0
      For the serial monitor you could use the uart output, something like the P.Fleury uart library should do the work.

      Delete
    6. Hey Davide, thank you for the help I have another question now.

      How do you display the temperature and humidity on an LCD Screen?

      http://i.imgur.com/Xn9h08U.png

      I am also using Atmel Stuido as my IDE.

      I wanted to know how I can display the output on an LCD Screen. Thank you.

      Delete
    7. Hello, you can use the P.Fleury LCD library, you can find it here http://homepage.hispeed.ch/peterfleury/avr-software.html. Then you can simply use lcd_puts instead of uart_puts. Note that the uart library you are using there is from P.Fleury too. He's a "cool kid!" :)

      Delete
  11. Hey david I'm using an Atmega 328P and your library but it seems as if the value for temperatures and Humidity never get updated. And I seem to stay in the while loop forever. Any idea why this would happen? Thanks again.

    ReplyDelete
    Replies
    1. Hello, have you check the updated version of this library?

      Delete
    2. Apologies, I didnt mention it in my comment. Yeah I'm using dht 03 your updated version and I'm using a DHT 11 with 3 pins(Like here http://imgur.com/9BQAi0y). Any ideas? Thanks again.

      Delete
  12. This comment has been removed by the author.

    ReplyDelete
  13. Hey David
    I am working on Atmega16 and using an LCD to show the data. But while compiling using Atmel STudio 7.0 there is always an error of "dht/dht.h" not found.
    I also tried to remove the header file and copied the entire thing to main.c
    However now I am getting un referenced error on dht.gettemperatureandhumidity code...
    Can you help me please..

    ReplyDelete
    Replies
    1. Hello, you have to add the dht to your liker. Eclipse just do it when you add it to the project. Try it.

      Delete
  14. This comment has been removed by a blog administrator.

    ReplyDelete