We are now going to use the sensor. Again, remember that we are using the Arduino IDE, so we can code just like we would do using an Arduino board. Here, we will simply print the value of the temperature inside the Serial monitor of the Arduino IDE. If it has not been done yet, install the Adafruit DHT sensor library using the Arduino IDE library manager.
This is the complete code for this part:
// Libraries #include "DHT.h" // Pin #define DHTPIN 5 // Use DHT11 sensor #define DHTTYPE DHT11 // Initialize DHT sensor DHT dht(DHTPIN, DHTTYPE, 15); void setup() { // Start Serial Serial.begin(115200); // Init DHT dht.begin(); } void loop() { // Reading temperature and humidity float h = dht.readHumidity(); // Read temperature as Celsius float t = dht.readTemperature(); // Display data Serial.print("Humidity: "); Serial.print(h); Serial.print(" % "); Serial.print("Temperature: "); Serial.print(t); Serial.println(" *C "); // Wait 2 seconds between measurements. delay(2000); }
Let's see the details of the code. You can see that all the measurement part is contained inside the loop()
function, which makes the code inside it repeat every 2 seconds.
Then, we read data from the DHT11 sensor, and print the value of the temperature and humidity on the Serial port.
Now let's start with the steps to test the sensor:
You should immediately see the temperature and humidity readings inside the Serial monitor. My sensor was reading around 24 degrees Celsius when I tested it, which is a realistic value.
18.189.186.109