smart room

Hi there,

i have a project that could control light intensity as well as temperature in a room using Pir sensor, an LDR and a DHT11.

however even if the light intensity is enough, my LED switches on.

help please.

I am using the following as my code :

#include "DHT.h"

#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11 (AM2302)
#define fan 4
#define photocellPin 0 // photocell attach to A0
#define pirPin 36 //PIR attach to pin 36
#define lightPin 13 // attach light to pin 13

int lightState = 0;
int maxHum = 40;
int maxTemp = 35;
int Threshold = 400;
int isLightOn = LOW;
int isFanOn = LOW;
int val = 0; // variable for reading the pin status
int sensorValue = analogRead(photocellPin); // Read the sensor pin

#define uint8 unsigned char

DHT dht(DHTPIN, DHTTYPE);
uint8 pirValue = 0; //store the PIR state

void setup() {
pinMode(fan, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(photocellPin,INPUT);
pinMode(lightPin,OUTPUT);

Serial.begin(9600);
dht.begin();
}

void loop() {
// Wait a few seconds between measurements.

val = digitalRead(pirPin); // read input value
if (val == HIGH) { // check if motion

if (isLightOn == LOW) {
if (sensorValue > Threshold) // If light level is low is detected
{
digitalWrite(lightPin, HIGH); //switch light on
isLightOn = HIGH;

}
}

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

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

if(h > maxHum || t > maxTemp) {
digitalWrite(fan, HIGH);
isFanOn = HIGH;
} else {

if (val == LOW) // check if no motion
if(isLightOn == true) {
digitalWrite(lightPin, LOW); //switch off light
isLightOn = LOW;
}
if (isFanOn == HIGH) {
}
digitalWrite(fan, LOW);
isFanOn = LOW;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
pirValue = digitalRead(pirPin);
Serial.println("Pir State: ");
Serial.println(pirValue);
digitalWrite(lightPin, pirValue);
Serial.print("Photocell Value: ");
Serial.println(analogRead(photocellPin));
}
}

smart_room.ino (2.28 KB)

Please read #7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

sorry. so here is the code i am using:

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11  (AM2302)
#define fan 4
#define photocellPin 0  // photocell attach to A0 
#define pirPin 36  //PIR attach to pin 36
#define lightPin 13 // attach light to pin 13

int lightState = 0;
int maxHum = 40;
int maxTemp = 35;
int Threshold = 400;
int isLightOn = LOW;
int isFanOn = LOW;
int val = 0;  // variable for reading the pin status
int sensorValue = analogRead(photocellPin);  // Read the sensor pin

#define uint8 unsigned char

DHT dht(DHTPIN, DHTTYPE);
uint8 pirValue = 0;  //store the PIR state


void setup() {
  pinMode(fan, OUTPUT);
   pinMode(pirPin, INPUT);
   pinMode(photocellPin,INPUT);
     pinMode(lightPin,OUTPUT);
    
  Serial.begin(9600); 
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  

   val = digitalRead(pirPin);  // read input value
  if (val == HIGH) {            // check if motion

      if (isLightOn == LOW) {
       if (sensorValue > Threshold)  // If light level is low is detected
       {
        digitalWrite(lightPin, HIGH); //switch light on
         isLightOn = HIGH;
         
           }
  }

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  if(h > maxHum || t > maxTemp) {
      digitalWrite(fan, HIGH);
       isFanOn = HIGH;
  } else {

     if (val == LOW) // check if no motion
     if(isLightOn == true) {
      digitalWrite(lightPin, LOW);  //switch off light
        isLightOn = LOW;
     }
      if (isFanOn == HIGH) {
  }
     digitalWrite(fan, LOW);
     isFanOn = LOW; 
  }
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C ");
   pirValue = digitalRead(pirPin);
    Serial.println("Pir State: ");
    Serial.println(pirValue);
    digitalWrite(lightPin, pirValue);
        Serial.print("Photocell Value: ");
    Serial.println(analogRead(photocellPin));
  }
  }

it looks to me like you've only made 1 reading before entering into the IF statements. I believe you need to insert an analogRead into your loop at the beginning that will be read each time. It is then acted upon when it goes through the IF statements. For example, you may want to write this:

void loop() {
  // Wait a few seconds between measurements.
 

   val = digitalRead(pirPin);  // read input value
   sensorValue = analogRead(photocellPin)

  if (val == HIGH) {            // check if motion

      if (isLightOn == LOW) {

          Serial.print(sensorValue);

       if (sensorValue > Threshold)  // If light level is low is detected
       {
        digitalWrite(lightPin, HIGH); //switch light on
         isLightOn = HIGH;
         
           }
  }

Also, it is useful to insert Serial.print into certain places in your code so you can use the serial monitor for debugging. It will help you see what the values are at certain times. This helps determine whether you're getting the expected result.

Later in your code, you have if(isLightOn == true). "true" is wrong. This should be either HIGH, based on your logical test.

Lastly, don't include the temperature and humidity readings yet. Work through the motion sensor and ambient light functions first, then add more code after you've worked the kinks out of the first part.

This should get you off to a better start.

thank you for your advice, but i am still having the same problem.

I really can`t fix the problem.

below is my updated code:

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11  (AM2302)
#define fan 4
#define photocellPin 0  // photocell attach to A0 
#define pirPin 36  //PIR attach to pin 36
#define lightPin 13 // attach light to pin 13

int lightState = 0;
int maxHum = 45;
int maxTemp = 35;
int Threshold = 900;
int isLightOn = LOW;
int isFanOn = LOW;
int val = 0;  // variable for reading the pin status
int sensorValue = 0;

#define uint8 unsigned char

DHT dht(DHTPIN, DHTTYPE);
uint8 pirValue = 0;  //store the PIR state


void setup() {
  pinMode(fan, OUTPUT);
   pinMode(pirPin, INPUT);
   pinMode(photocellPin,INPUT);
     pinMode(lightPin,OUTPUT);
    
  Serial.begin(9600); 
  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  

        sensorValue = analogRead(photocellPin);  // Read the sensor pin
        val = digitalRead(pirPin);  // read input value
        
  if (val == HIGH) {            // check if motion

      if (isLightOn == false) {
        
 Serial.print(sensorValue);
 
       if (sensorValue > Threshold)  // If light level is low is detected
       {
        digitalWrite(lightPin, HIGH); //switch light on
         isLightOn = true;
         
           }
  }

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  if(h > maxHum || t > maxTemp) {
      digitalWrite(fan, HIGH);
       isFanOn = true;
  } else {

     if (val == LOW)
     if(isLightOn == true) {
      
      digitalWrite(lightPin, LOW);  //switch off light
        isLightOn = false;
     }
      if (isFanOn == HIGH) {
  }
     digitalWrite(fan, LOW);
     isFanOn = false; 
  }
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C ");
   pirValue = digitalRead(pirPin);
    Serial.println("Pir State: ");
    Serial.println(pirValue);
    digitalWrite(lightPin, pirValue);
        Serial.print("Photocell Value: ");
    Serial.println(analogRead(photocellPin));
  }
  }

The way it is currently written there is no way to turn off the light because the "if (val == LOW)" clause is contained within the "if (val == HIGH)" clause and so cant be executed.

Suggest rewrite something like

    sensorValue = analogRead(photocellPin);  // Read the sensor pin
    val = digitalRead(pirPin);  // read input value

    if (val == HIGH) {            // check if motion

        if (isLightOn == false) {

            Serial.print(sensorValue);

            if (sensorValue > Threshold)  // If light level is low is detected
            {
                digitalWrite(lightPin, HIGH); //switch light on
                isLightOn = true;

            }
        }
    } else {
        if(isLightOn == true) {

            digitalWrite(lightPin, LOW);  //switch off light
            isLightOn = false;
        }


    }

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

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

    if(h > maxHum || t > maxTemp) {
        digitalWrite(fan, HIGH);
        isFanOn = true;
    } else {

        if (isFanOn == HIGH) {
            digitalWrite(fan, LOW);
            isFanOn = false;
        }
    }

If you use Tools->Auto Format in the IDE it make these kind of errors easier to spot.