Advertisement
microrobotics

DHT11

Mar 30th, 2023 (edited)
1,729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The DHT11 is a digital temperature and humidity sensor that communicates over a 1-wire protocol. Here's an example code in Arduino that reads the temperature and humidity from the sensor:
  3.  
  4. Note: This code assumes that the DHT11 sensor is connected to pin 2 of the Arduino board. If you're using a different pin, you'll need to modify the DHTPIN definition accordingly. Also, the sensor may take up to 2 seconds to provide an accurate reading, so a delay is added before each reading.
  5. */
  6.  
  7. #include <DHT.h>
  8.  
  9. #define DHTPIN 2     // the pin connected to the sensor
  10. #define DHTTYPE DHT11   // specify the sensor type
  11.  
  12. DHT dht(DHTPIN, DHTTYPE);
  13.  
  14. void setup() {
  15.   Serial.begin(9600);
  16.   dht.begin();
  17. }
  18.  
  19. void loop() {
  20.   delay(2000);  // wait 2 seconds between readings
  21.  
  22.   // read the temperature and humidity from the sensor
  23.   float temperature = dht.readTemperature();
  24.   float humidity = dht.readHumidity();
  25.  
  26.   // print the temperature and humidity
  27.   Serial.print("Temperature: ");
  28.   Serial.print(temperature);
  29.   Serial.println(" °C");
  30.   Serial.print("Humidity: ");
  31.   Serial.print(humidity);
  32.   Serial.println(" %");
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement