This is my first Arduino project for a class. I am a little further along with Arduino than a complete Newbie, but still just scratching the surface. I am a teacher, but taking an engineering education class that I'm loving. I have successfully accomplished using an Arduino Uno to turn a fan off and on (with a PowerSwitch Tail II) with input from a AM2302 (DHT211) temperature sensor.
My question is why. I coded the Arduino to turn off and on according to a certain temp threshold, but it works in reverse of what I intended. Here is how I programmed it: if the temp is above the threshold, it turns ON, serendipitously, I reversed the sign and found that it worked. The entire program is below, but this is what is working - but opposite of what I would expect:
if (t < maxTemp) //if sensor value is less than the threshold (originally I had the sign reversed!)
{
digitalWrite (fanPin, HIGH); //turn fan on
}
else
{
digitalWrite (fanPin, LOW); //turn fan off
}
}
I am puzzled as to why it works in reverse. Could it be something in the way I wired it?
Thanks!
// #include "/Users/iLab/Documents/Arduino/arduinosketchlibrary/libraries/DHT/DHT.h"
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//const int fanPin = A0; //should this pin be a digital only pin?
const int fanPin = 10;
int t = 0;
DHT dht(DHTPIN, DHTTYPE);
int maxTemp;
void setup() {
Serial.begin(9600);
pinMode(fanPin, OUTPUT);
pinMode(DHTPIN, INPUT);
pinMode(13, OUTPUT); // Set pin D13 as an OUTPUT
dht.begin();
maxTemp = 70.0;
}
void loop() {
digitalWrite(13, HIGH); //included the light to make sure program was running
delay(1000);
digitalWrite(13, LOW);
delay(1000);
t = dht.readTemperature(true); //these lines of code read the sensor and report the results in the serial monitor
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *F\t");
maxTemp = 70.0; //this is the temperature threshold that determines whether the fan is on or off
delay(3000); //give the sensor 3 seconds to return a value
if (t < maxTemp)
{
digitalWrite (fanPin, HIGH); //turn fan on
}
else
{
digitalWrite (fanPin, LOW); //turn fan off
}
}