0% found this document useful (0 votes)
11 views2 pages

Gloire

The document describes code for a DHT sensor to read temperature and humidity and control a fan relay based on the temperature threshold. It also includes code for a PIR motion sensor to detect motion and control an LED.

Uploaded by

yawolosenan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Gloire

The document describes code for a DHT sensor to read temperature and humidity and control a fan relay based on the temperature threshold. It also includes code for a PIR motion sensor to detect motion and control an LED.

Uploaded by

yawolosenan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <DHT.

h>
#define DHTPIN 5 // Digital pin connected to the DHT sensor

// Uncomment whatever type you're using!


#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define FAN_PIN 2 // FAN RELAY

//WidgetLED FAN (VO) ;

float humDHT = 0;
float tempDHT = 0;

// Initialize DHT sensor.


DHT dht(DHTPIN, DHTTYPE);
#define timeSeconds 2

// Set GPIOs for LED and PIR Motion Sensor


const int led = 26;
const int motionSensor = 27;

// Timer: Auxiliary variables


unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;

// Checks if motion was detected, sets LED HIGH and starts a timer
void IRAM_ATTR detectsMovement() {
Serial.println("MOTION DETECTED!!!");
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}

void setup() {
Serial.begin(115200);
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
Serial.println(F("DHTxx test!"));
dht.begin();
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set
RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement,
RISING);

// Set LED to LOW


pinMode(led, OUTPUT);
digitalWrite(led, LOW);

void loop() {

delay(250);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
humDHT = dht.readHumidity();
// Read temperature as Celsius (the default)
tempDHT = dht.readTemperature();

// Check if any reads failed and exit early (to try again).
if (isnan(humDHT) || isnan(tempDHT))
{
Serial.println("Failed to read from DHT sensor!");
return;
}

Serial.print(F("Temperature: "));
Serial.print(tempDHT);
Serial.print(F("°C "));
Serial.println();
Serial.print(F("Humidity: "));
Serial.print(humDHT);
Serial.print(F("%"));
Serial.println();

Serial.println("***********************");
Serial.println();

// Compare Threshold value from Blynk and DHT Temperature value.


if (tempDHT>31)
{
digitalWrite(FAN_PIN, HIGH);

}
else {
digitalWrite(FAN_PIN, LOW);

}
// Current time
now = millis();
// Turn off the LED after the number of seconds defined in the timeSeconds
variable
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
startTimer = false;
}
}

You might also like