mirror of
https://github.com/dhrubasaha08/DHT11.git
synced 2023-10-23 22:17:45 +03:00
Created DHT11 sensor library
This commit is contained in:
32
examples/ReadTemperature/ReadTemperature.ino
Normal file
32
examples/ReadTemperature/ReadTemperature.ino
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <DHT11.h>
|
||||
|
||||
// Create an instance of the DHT11 class and set the digital I/O pin.
|
||||
DHT11 dht11(2);
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize serial communication at 115200 baud.
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Read the temperature from the sensor.
|
||||
float temperature = dht11.readTemperature();
|
||||
|
||||
// If the temperature reading was successful, print it to the serial monitor.
|
||||
if (temperature != -1)
|
||||
{
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(temperature);
|
||||
Serial.println(" C");
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the temperature reading failed, print an error message.
|
||||
Serial.println("Error reading temperature");
|
||||
}
|
||||
|
||||
// Wait for 2 seconds before the next reading.
|
||||
delay(2000);
|
||||
}
|
Reference in New Issue
Block a user