Sensor with piezo speaker
The materials used are Arduino UNO R3, DHT11 Temperature, and Humidity sensor, Jumper wires, Breadboard, Arduino USB 2.0 cable, LED (generic), Resistor (221 ohms), buzzer.
Process
Place the sensor on the breadboard.
Connect the sensor and Arduino UNO with the help of Jumper Wires.
There are three pins in the sensor, the S pin is for signal, the middle one is for voltage and the last one is for ground.
Now, the LEDs and resistors are to be connected to the breadboard
In order to run the code successfully, the DHT library and Adafruit Sensor library needs to be added to Arduino software. Then, write the code and compile it to get the output.
About the sensor
This sensor will measure the temperature and humidity. Once the temperature and humidity are measured, the buzzer will buzz either if the temperature or humidity goes below the specific value or if the temperature or humidity goes above the specific value.
Breadboard View
PCB(Printed circuit board) View
Schematic view for the sensor
Prototype 2
Code
#include <Adafruit_Sensor.h>
#include <DHT.h>
int speakerPin = 9;
int length = 1;
#define DHTPIN 5
#define DHTTYPE DHT11
#define LED_TOO_COLD A0
#define LED_PERFECT A1
#define LED_TOO_HOT A2
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 test!");
dht.begin();
}
void loop() {
pinMode(speakerPin, OUTPUT);
pinMode (A0 , OUTPUT);
pinMode (A1 , OUTPUT);
pinMode (A2 , OUTPUT);
delay(5000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
// The below if statement will execute when the temperature goes below 28C or humidity is below 50%. In this case, the buzzer will buzz.
if (t <= 28 || h <= 50) {
Serial.println("Warning! - Either the temperature or humidity is low.");
digitalWrite(A0, HIGH);
digitalWrite(speakerPin, HIGH);
delay (1000);
digitalWrite(speakerPin, LOW);
digitalWrite(A0, LOW);
}
// The below if statement will execute when the temperature goes above 33C or humidity is above 80%. In this case, the buzzer will buzz.
if (t >= 33 || h >= 80) {
Serial.println("Warning! - Either the temperature or humidity is high.");
digitalWrite(A2, HIGH);
digitalWrite(speakerPin, HIGH);
delay (1000);
digitalWrite(speakerPin, LOW);
digitalWrite(A2, LOW);
}
// The below if statement will execute when the temperature goes above 28C and when the temperature goes below 33C.
if (28 < t && t < 33) {
Serial.println("Perfect temperature and humidity!");
digitalWrite(A1, HIGH);
delay (1000);
digitalWrite(A1, LOW);
}
}