I made this project because I wanted a way to automatically control the speed of a DC fan according to the temperature read by a LM35 sensor. I had a few problems with the PWM part mainly because the fan made a disturbing noise so I had to add a simple RC filter at the output of the PWM pin on the Arduino board.
Arduino Sketch
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(7,6,5,4,3,2);
- int tempPin = A1; // the output pin of LM35
- int fan = 11; // the pin where fan is
- int led = 8; // led pin
- int temp;
- int tempMin = 30; // the temperature to start the fan
- int tempMax = 70; // the maximum temperature when fan is at 100%
- int fanSpeed;
- int fanLCD;
- void setup() {
- pinMode(fan, OUTPUT);
- pinMode(led, OUTPUT);
- pinMode(tempPin, INPUT);
- lcd.begin(16,2);
- }
- void loop() {
- temp = readTemp(); // get the temperature
- if(temp < tempMin) { // if temp is lower than minimum temp
- fanSpeed = 0; // fan is not spinning
- digitalWrite(fan, LOW);
- }
- if((temp >= tempMin) && (temp <= tempMax)) { // if temperature is higher than minimum temp
- fanSpeed = map(temp, tempMin, tempMax, 32, 255); // the actual speed of fan
- fanLCD = map(temp, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
- analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
- }
- if(temp > tempMax) { // if temp is higher than tempMax
- digitalWrite(led, HIGH); // turn on led
- } else { // else turn of led
- digitalWrite(led, LOW);
- }
- lcd.print(“TEMP: “);
- lcd.print(temp); // display the temperature
- lcd.print(“C “);
- lcd.setCursor(0,1); // move cursor to next line
- lcd.print(“FANS: “);
- lcd.print(fanLCD); // display the fan speed
- lcd.print(“%”);
- delay(200);
- lcd.clear();
- }
- int readTemp() { // get the temperature and convert it to celsius
- temp = analogRead(tempPin);
- return temp * 0.48828125;
- }
I used an LCD shield to display the current temperature and speed of the fan, but you can use the circuit without the LCD display. You also need to select the transistor by the type of fan that you use. In my case I used the well-known BD139 transistor and a 9V battery to provide power to the fan and transistor. The LM35 temperature sensor and red led are powered with 5V from the Arduino board.
How does the circuit works?
As you can see in the sketch on the first line I included the LiquidCrystal library (header) that includes useful functions to use when an LCD is connected to the Arduino board. Then I set the pins for the sensor, led and fan.
The most important part is to set the variables tempMin and tempMax with your desired values. tempMin is the temperature at which the fan starts to spin andtempMax is the temperature when the red led lights warning you that the maximum temp was reached. For example if you set tempMin at 30 and tempMax at 35 then the fan will start spinning at 30°C and reach its maximum speed at 35°C.
We store the temperature value in the temp variable and then use some if() functions to check if temp is lower than tempMin and if so let the fan OFF (LOW). The next if() is to check if temperature is higher than the minTemp and lower than the tempMax and if so then use the map() function to re-map the temp value from one value to another. In our case fanSpeed will have a value of 32 at tempMin and 255 at tempMax. These values are used to control the speed of the fan using PWM and the analogWrite().
The fanLCD re-maps the temp to allow the display of fanSpeed in a 0 to 100% range so you can say that the speed of the fan is directly dependent of the LM35′s temperature. When the temperature reaches the value set in tempMax the fan will be at its maximum spinning velocity and the LCD will display FANS: 100% even though the temperature might increase above tempMax.
The rest of the explanation can be read in the comments area of the Arduino sketch.
0 comments:
Post a Comment