dht11 temperature and humidity sensor arduino code

Here’s the dht11 temperature and humidity sensor arduino code, you’ll need to use a library that simplifies communication with the sensor.

Below is a simple Arduino sketch to read data from a dht11 sensor and display the temperature and humidity on the Serial Monitor.

Components Needed for dht11 temperature and humidity sensor arduino code :

  • Arduino (e.g., Uno)
  • dht11 sensor
  • Jumper wires
  • Breadboard (optional)
  • Resistor 4.7kΩ – 10kΩ (optional if using just the dht11 chip)

Aliexpress

ItemImageCost ($USD)
dht11 sensor$1.25
arduino uno$3.27
jumper wires$1.87
breadboard$1.53
resistors$1.11 (50 pieces)

dht11 arduino Wiring and diagram:

  1. DHT11 Pin 1 (Data) to Digital Pin 2 on Arduino (or any other digital pin)
  2. DHT11 Pin 2 (VCC) to 5V on Arduino
  3. DHT11 Pin 3 (GND) to GND on Arduino
  1. Optional if using just using the dht11 chip; Using a breadboard, place a resistor between VCC and Data pin (4.7kΩ or 10kΩ)

dht11 temperature and humidity sensor arduino code

First, you’ll need to install the DHT library. You can do this by going to Sketch > Include Library > Manage Libraries…, searching for “DHT sensor library” by Adafruit, and installing it.

// dht11 temperature and humidity sensor arduino code
#include "DHT.h"

#define DHTPIN 2     // Pin where the data pin of DHT11 is connected
#define DHTTYPE DHT11   // Define DHT type as DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600); 
  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature(); // For Fahrenheit use dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print temperature and humidity values to the Serial Monitor
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");

  delay(2000); // Wait a few seconds between measurements.
}

1. Reading Humidity and Temperature from dht11 temperature and humidity sensor arduino code

float humidity = dht.readHumidity();
float temperature = dht.readTemperature(); // For Fahrenheit use dht.readTemperature(true);
  • Purpose: The first thing the loop does is read the humidity and temperature values from the DHT11 sensor.
  • dht.readHumidity(): This function reads the humidity from the sensor and returns it as a floating-point number (e.g., 50.5 for 50.5% humidity).
  • dht.readTemperature(): This function reads the temperature from the sensor and returns it as a floating-point number in Celsius (e.g., 23.7 for 23.7°C). If you want the temperature in Fahrenheit, you would pass true to this function like this: dht.readTemperature(true).

2. Checking for Errors in the dht11 temperature and humidity sensor arduino code

if (isnan(humidity) || isnan(temperature)) {
  Serial.println("Failed to read from DHT sensor!");
  return;
}
  • Purpose: After attempting to read the sensor, the code checks if the readings were successful.
  • isnan() Function: This function checks if the value returned from the sensor is “NaN” (Not a Number). If the sensor fails to provide a valid reading (due to a wiring issue or sensor malfunction), it might return NaN.
  • Condition Check: The code checks if either the humidity or temperature variable contains NaN.
  • If there’s an error: If any of the readings failed, a message is printed to the Serial Monitor saying “Failed to read from DHT sensor!”, and the loop ends early using return;. This prevents the code from trying to print invalid data.

3. Displaying the dht11 Results

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
  • Purpose: This section is responsible for displaying the temperature and humidity readings on the Serial Monitor.
  • Serial.print() and Serial.println() Functions:
  • Serial.print("Humidity: "): Prints the string “Humidity: ” to the Serial Monitor.
  • Serial.print(humidity): Prints the value of the humidity variable to the Serial Monitor.
  • Serial.print(" %\t"): Prints the percentage symbol % followed by a tab (\t) for spacing.
  • Serial.print("Temperature: "): Prints the string “Temperature: ” to the Serial Monitor.
  • Serial.print(temperature): Prints the value of the temperature variable to the Serial Monitor.
  • Serial.println(" *C"): Prints the string ” *C” (for Celsius) to the Serial Monitor and moves the cursor to a new line.

4. Delay Between Readings

delay(2000); // Wait a few seconds between measurements.
  • Purpose: This line pauses the program for 2000 milliseconds (2 seconds) before the loop starts again.
  • Why 2 Seconds?: The DHT11 sensor has a rate limit on how often it can provide readings, so adding a delay ensures that the sensor has enough time to stabilize before the next reading.
  • and that the end of the dht11 temperature and humidity sensor arduino code!

dht sensor Summary:

  • Read Data: The loop reads the humidity and temperature from the DHT11 sensor.
  • Check for Errors: It checks if the readings are valid.
  • Display Data: If the readings are valid, it prints the humidity and temperature values to the Serial Monitor.
  • Wait: The program pauses for 2 seconds before taking the next reading.
  • All elements combined this makes the dht11 temperature and humidity sensor arduino code work!

This loop repeats indefinitely, providing continuous temperature and humidity readings.

Usage:

  • Upload this sketch of the dht11 temperature and humidity sensor arduino code to your Arduino by ensuring the arduino is selected if it isn’t, use the drop down in the top left of the ide app, then select the option ‘Select other board and port’ scroll down to arduino uno and the port number your arduino is connected to.

Then press upload

  • Open the Serial Monitor (Ctrl + Shift + M) to view the temperature and humidity readings.

Datasheet for dht11 temperature and humidity sensor arduino code

DHT11 Features & Specs
No.ParameterValue
1MeasuresHumidity & Temperature
2Sensors IncludedCapacitive Humidity Sensor & Thermistor
3Humidity Range20% to 80% with ±5% accuracy
4Temperature Range0°C to 50°C with ±2°C accuracy
5Package4 Pins in a single row
6Operating Voltage3.0V to 5.5V
7Operating Current0.3mA(measuring), 60uA(idle)
8Resolution1°C, 1%RH (8-Bit)
9Response Time6s-15s
10Repeatability±1°C, ±1%RH
11Sampling Frequency1Hz
12Dimensions27mm x 59mm x 13.5mm (1.05″ x 2.32″ x 0.53″)
dht11 specifications

309 thoughts on “dht11 temperature and humidity sensor arduino code

  1. With havin so much content and articles do you ever run into any problems of plagorism or copyright violation? My website has a lot of unique content I’ve either created myself or outsourced but it looks like a lot of it is popping it up all over the internet without my agreement. Do you know any methods to help reduce content from being stolen? I’d definitely appreciate it.

  2. at web, except I know I am getting familiarity all the time by reading thes pleasant posts.|Fantastic post. I will also be handling some of these problems.|Hello, I think this is a great blog. I happened onto it;) I have bookmarked it and will check it out again. The best way to change is via wealth and independence. May you prosper and never stop mentoring others.|I was overjoyed to find this website. I must express my gratitude for your time because this was an amazing read! I thoroughly enjoyed reading it, and I’ve bookmarked your blog so I can check out fresh content in the future.|Hi there! If I shared your blog with my Facebook group, would that be okay? I believe there are a lot of people who would truly value your article.|منشور رائع. سأتعامل مع بعض هذه|

  3. ) سأعيد زيارتها مرة أخرى لأنني قمت بوضع علامة كتاب عليها. المال والحرية هي أفضل طريقة للتغيير، أتمنى أن تكون غنيًا و

  4. ) Vou voltar a visitá-lo uma vez que o marquei no livro. O dinheiro e a liberdade são a melhor forma de mudar, que sejas rico e continues a orientar os outros.

  5. I think this is among the most vital information for me. And i’m glad reading your article. But wanna remark on few general things, The site style is perfect, the articles is really excellent : D. Good job, cheers

  6. at web, except I know I am getting familiarity all the time by reading thes pleasant posts.|Fantastic post. I will also be handling some of these problems.|Hello, I think this is a great blog. I happened onto it;) I have bookmarked it and will check it out again. The best way to change is via wealth and independence. May you prosper and never stop mentoring others.|I was overjoyed to find this website. I must express my gratitude for your time because this was an amazing read! I thoroughly enjoyed reading it, and I’ve bookmarked your blog so I can check out fresh content in the future.|Hi there! If I shared your blog with my Facebook group, would that be okay? I believe there are a lot of people who would truly value your article.|منشور رائع. سأتعامل مع بعض هذه|

  7. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike.

  8. I needed to thank you for this very good read!! I absolutely enjoyed every little bit of it.I have you book-marked to look at new stuff you post?my blog … ky.sgz8.com

  9. I don’t like pubs minoxidil dischem “We were warned in June (about the program) and we reacted strongly but obviously we need to go further,” Fabius said. “We must quickly assure that these practices aren’t repeated.”

  10. I was wondering if you ever thought of changing the page layout of your website? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 images. Maybe you could space it out better?

  11. Have you ever considered writing an e-book or guest authoring on other blogs? I have a blog based upon on the same topics you discuss and would really like to have you share some stories/information. I know my audience would value your work. If you’re even remotely interested, feel free to send me an email.

  12. Appreciating the time and effort you put into your website and in depth information you present. It’s good to come across a blog every once in a while that isn’t the same outdated rehashed material. Great read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  13. at web, except I know I am getting familiarity all the time by reading thes pleasant posts.|Fantastic post. I will also be handling some of these problems.|Hello, I think this is a great blog. I happened onto it;) I have bookmarked it and will check it out again. The best way to change is via wealth and independence. May you prosper and never stop mentoring others.|I was overjoyed to find this website. I must express my gratitude for your time because this was an amazing read! I thoroughly enjoyed reading it, and I’ve bookmarked your blog so I can check out fresh content in the future.|Hi there! If I shared your blog with my Facebook group, would that be okay? I believe there are a lot of people who would truly value your article.|منشور رائع. سأتعامل مع بعض هذه|

  14. An interesting discussion is definitely worth comment. I do believe that you ought to write more about this subject, it might not be a taboo subject but usually people don’t speak about such topics.To the next! Best wishes!!

  15. Have you got any ? tretinoin 0.0025 NEW YORK, Sept 25 (Reuters) – The dollar fell on Wednesdayafter four sessions of gains, weighed down by worries aboutgridlock in Washington on the U.S. budget that could lead to agovernment shutdown next week.

  16. I’m not sure where you are getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for great information I was looking for this information for my mission.

  17. A fascinating discussion is worth comment. I assume that you should create much more on this subject, it may not be a forbidden subject however usually individuals are inadequate to speak on such subjects. To the next. Thanks

  18. Oh my goodness! Amazing article dude! Thanks,However I am encountering difficulties with your RSS.I don’t know why I can’t join it. Is there anyone else getting identical RSS issues?Anyone who knows the answer can you kindly respond?Thanx!!

  19. I’m extremely impressed with your writing skills as well as with the layout on your blog.Is this a paid theme or did you customize it yourself? Either way keep up theexcellent quality writing, it’s rare to see a nice blog like this one nowadays.

  20. It’s actually a great and helpful piece of info. I’m happy that you shared this helpful information with us. Please stay us informed like this. Thank you for sharing.

  21. What’s Going down i am new to this, I stumbled upon this I’ve foundIt absolutely useful and it has aided me out loads. I’m hoping to contribute &help different users like its aided me. Good job.

  22. I’ll right away grasp your rss as I can not in finding your email subscription hyperlink or e-newsletter service.Do you’ve any? Kindly let me recognize so that I could subscribe.Thanks.

  23. Aw, this was a really nice post. Finding the time and actual effort to generate a great articleÖ but what can I sayÖ I procrastinate a whole lot and never manage to get nearly anything done.

  24. Greetings! I know this is somewhat off topic but I was wonderingif youu knew where I could get a captcha plugin for my comment form?I’m using the same blog platform as yours and I’m having difficulty finding one?Thanks a lot!

  25. Admiring the hard work you put into your blog and detailed information you present. It’s great to come across a blog every once in a while that isn’t the same outdated rehashed material. Wonderful read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  26. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something enlightening to read?

  27. Hello! This is my 1st comment here so I just wanted to give a quickshout out and tell you I genuinely enjoy reading your posts.Can you recommend any other blogs/websites/forums that deal with the samesubjects? Thanks!

  28. Hi there are using WordPress for your blog platform? I’mnew to the blog world but I’m trying to get started andcreate my own. Do you require any html codingexpertise to make your own blog? Any help would be really appreciated!

  29. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something informative to read?

  30. We are looking for some people that might be interested in from working their home on a full-time basis. If you want to earn $100 a day, and you don’t mind creating some short opinions up, this might be perfect opportunity for you!

  31. We are searching for some people that are interested in from working their home on a part-time basis. If you want to earn $200 a day, and you don’t mind creating some short opinions up, this might be perfect opportunity for you!

  32. Hello. Such a nice post! I’m really appreciate it. It will be great if you’ll read my first article on mycollegeessaywriter.com)

  33. Hello there! Would you mind if I share your blog with my myspace group?
    There’s a lot of folks that I think would really appreciate your content.

    Please let me know. Thanks

    Feel free to surf to my webpage … nordvpn coupons inspiresensation (t.co)

  34. Howdy! I know this is kind of off-topic but I needed to ask.
    Does running a well-established website such as yours require a lot of work?
    I am completely new to operating a blog however I do write in my journal everyday.
    I’d like to start a blog so I will be able to share my experience and feelings online.
    Please let me know if you have any kind of recommendations or tips for brand
    new aspiring bloggers. Thankyou!

    My site: nordvpn coupons inspiresensation (easyurl.cc)

  35. Excellent web site you have here.. It’s hard to find high quality writing like yours these days.
    I seriously appreciate people like you! Take care!!

    Also visit my blog post :: nordvpn coupons inspiresensation (t.co)

  36. What i do not realize is in truth how you are no longer actually much more neatly-favored than you might be now.
    You’re very intelligent. You already know thus significantly
    relating to this subject, produced me individually believe it from a
    lot of various angles. Its like men and women are not involved unless
    it is one thing to do with Lady gaga! Your personal stuffs outstanding.
    Always deal with it up!

    Here is my web page :: nordvpn coupons inspiresensation

  37. Hello I am so grateful I found your web site, I really
    found you by error, while I was looking on Bing for something
    else, Nonetheless I am here now and would just like to say thank you
    for a tremendous post and a all round enjoyable blog (I also love the theme/design), I
    don’t have time to read it all at the minute but I have
    bookmarked it and also included your RSS feeds, so when I have time I will be back
    to read a great deal more, Please do keep up the great work.

    Feel free to visit my web page – Nordvpn Coupons Inspiresensation (Abc.Li)

  38. I love what you guys are up too. Such clever work and coverage!
    Keep up the superb works guys I’ve included you guys to my own blogroll.

    Check out my webpage nordvpn coupons inspiresensation (t.co)

  39. Hi! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in trading links or maybe guest
    writing a blog post or vice-versa? My site goes over a lot of the same topics as yours and I
    think we could greatly benefit from each other.
    If you’re interested feel free to send me an email.
    I look forward to hearing from you! Excellent blog by the
    way!

    Visit my webpage; nordvpn coupons inspiresensation (da.Gd)

  40. great submit, very informative. I’m wondering why the other
    experts of this sector do not notice this.
    You must continue your writing. I am confident, you’ve a
    huge readers’ base already!

    Have a look at my blog … nordvpn coupons inspiresensation (http://easyurl.cc)

  41. Today, I went to the beachfront with my children. I found a sea shell
    and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell
    to her ear and screamed. There was a hermit crab inside and it
    pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell
    someone!

    Feel free to visit my homepage … nordvpn coupons inspiresensation

  42. Very nice post. I just stumbled upon your blog and
    wished to mention that I have really loved browsing your blog posts.
    After all I’ll be subscribing in your rss feed and I
    am hoping you write again very soon!

    My blog :: nordvpn coupons inspiresensation (tinyurl.com)

  43. Howdy! I know this is kinda off topic however , I’d figured
    I’d ask. Would you be interested in trading links or maybe guest writing a blog article or vice-versa?

    My website covers a lot of the same topics as yours and I believe we could greatly benefit from each
    other. If you might be interested feel free to send me an e-mail.
    I look forward to hearing from you! Great blog by the way!

    Also visit my webpage :: nordvpn coupons inspiresensation (t.co)

  44. Very good blog! Do you have any tips for aspiring writers?
    I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m completely confused
    .. Any suggestions? Appreciate it!

    Here is my homepage … nordvpn coupons inspiresensation –
    cia.sh,

  45. Hmm it looks like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
    I too am an aspiring blog writer but I’m still new to everything.
    Do you have any recommendations for newbie blog writers?
    I’d genuinely appreciate it.

    My homepage; nordvpn coupons inspiresensation (come.ac)

  46. We’re a group of volunteers and opening a brand new scheme
    in our community. Your web site offered us with useful
    info to work on. You’ve done an impressive process and our
    entire group might be grateful to you.

    My web site :: nordvpn coupons inspiresensation [in.mt]

  47. Definitely believe that which you stated. Your favorite reason appeared to be on the web the simplest thing to be aware of.

    I say to you, I certainly get annoyed while people think about worries that they
    plainly don’t know about. You managed to hit the nail upon the top and also defined out
    the whole thing without having side-effects , people could take a signal.
    Will likely be back to get more. Thanks

    Here is my blog post :: nordvpn coupons inspiresensation – shorter.me

  48. This is the perfect website for everyone who would like to understand this topic.
    You know a whole lot its almost hard to argue with you (not that I actually would want to…HaHa).
    You definitely put a brand new spin on a
    subject that has been written about for many years.
    Wonderful stuff, just wonderful!

    Here is my homepage – nordvpn coupons inspiresensation (cia.sh)

  49. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter
    updates. I’ve been looking for a plug-in like this for quite some time and was
    hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look
    forward to your new updates.

    Feel free to visit my site: Nordvpn coupons Inspiresensation

  50. Simply want to say your article is as amazing. The clearness in your post
    is simply nice and i could assume you’re an expert on this subject.
    Well with your permission allow me to grab your RSS feed to keep updated with forthcoming
    post. Thanks a million and please carry on the rewarding work.

    Also visit my website :: nordvpn coupons inspiresensation (come.ac)

  51. You can certainly see your skills within the work you write.
    The world hopes for more passionate writers like you who aren’t afraid to say how they believe.
    Always follow your heart.

    Also visit my homepage nordvpn coupons inspiresensation – easyurl.cc

  52. Tremendous things here. I am very happy to see your post.Thank you so much and I’m taking a look ahead to touch you.Will you please drop me a mail?

Leave a Reply

Your email address will not be published. Required fields are marked *