Temperature and Humidity Sensor Experiments

After completing the Temperature and Humidity Sensor project, test yourself by trying these experiments.

1 Create a high temperature alarm. When the temperature is at least one degree above the current starting room temperature it will sound an audible alarm. The alarm will stop when the temperature drops back below the starting temperature.
#include <DHT.h>       // include the temperature library
int DHTpin = 2;        // input pin
DHT dht(DHTpin, DHT11);
int buzzer = 8;
float start_temperature;

void setup() {
  pinMode(buzzer, OUTPUT);
  
  dht.begin();        // initialize the device
  // get current temperature
  start_temperature = dht.readTemperature(false);
}

void loop() {
  float temperature = dht.readTemperature(false);
  if (temperature > start_temperature+1) {
    tone(buzzer, 440); // sound alarm
  } else {
    noTone(buzzer);    // stop alarm
  }
}
2 Create a high and low temperature alarm. When the temperature is either one degree above or one degree below the current starting room temperature it will sound an audible alarm. The alarm will stop when the temperature goes back inside the range.
#include <DHT.h>       // include the temperature library
int DHTpin = 2;        // input pin
DHT dht(DHTpin, DHT11);
int buzzer = 8;
float start_temperature;

void setup() {
  pinMode(buzzer, OUTPUT);  // buzzer connected to pin 8
  
  dht.begin();        // initialize the device
  // get current temperature
  start_temperature = dht.readTemperature(false);
}

void loop() {
  float temperature = dht.readTemperature(false);
  if ((temperature > start_temperature+1) or (temperature < start_temperature-1)) {
    tone(buzzer, 440); // sound alarm
  } else {
    noTone(buzzer);    // stop alarm
  }
}
3 Create both an audible alarm and a visual alarm when the temperature is one degree above the current starting temperature. The audible alarm will make a sound, and the visual alarm will flash a led.