Neeed help with my project, fan and digital tempsensor not working properly

Hi

I have started a little project for my room where im having a fan that will start spinning at a certain speed and at a certain temperatur. At first a use this (1) method but my lm35 wasnt showing that accurate temp (but the fan spinned faster if the temps was higher) so i bought a DS18B20. I found that it was way more precisely and it showed the real temperatur but the thing is i didnt get a working code that could controlled a fan so it started slow and when the temps got higher the fans spinns faster. I was only able got it so it turned on 100% power. I used a dallas temperatur example and then change it a bit. (2)

(1) http://www.electroschematics.com/9540/arduino-fan-speed-controlled-temperature/
(2) http://www.instructables.com/id/Temperature-with-DS18B20/

/BarackoArne :smiley:

#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(2);  // on pin 10 (a 4.7K resistor is necessary)

float temp;

int tempMin = 24;

int tempMax = 29;

int fan = 4;

int fanSpeed = 0;

void setup(void) {
 Serial.begin(9600);

 pinMode(fan, OUTPUT);
}

void loop() {
 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 the minimum range

{

fanSpeed = map(temp, tempMin, tempMax, 100, 255); // the actual speed of fan

analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed

}

 byte i;
 byte present = 0;
 byte type_s;
 byte data[12];
 byte addr[8];
 float celsius, fahrenheit;
 
 if ( !ds.search(addr)) {
  
 }
 

 for( i = 0; i < 8; i++) {
  
 }

 if (OneWire::crc8(addr, 7) != addr[7]) {
     Serial.println("CRC is not valid!");
     return;
 }


 


 Serial.println();

 // the first ROM byte indicates which chip
 switch (addr[0]) {
  
 } 

 ds.reset();
 ds.select(addr);
 ds.write(0x44, 1);        // start conversion, with parasite power on at the end
 
 delay(1000);     // maybe 750ms is enough, maybe not
 // we might do a ds.depower() here, but the reset will take care of it.
 
 present = ds.reset();
 ds.select(addr);    
 ds.write(0xBE);         // Read Scratchpad


 for ( i = 0; i < 9; i++) {           // we need 9 bytes
   data = ds.read();
  
 }


 // Convert the data to actual temperature
 // because the result is a 16 bit signed integer, it should
 // be stored to an "int16_t" type, which is always 16 bits
 // even when compiled on a 32 bit processor.
 int16_t raw = (data[1] << 8) | data[0];
 if (type_s) {
   raw = raw << 3; // 9 bit resolution default
   if (data[7] == 0x10) {
     // "count remain" gives full 12 bit resolution
     raw = (raw & 0xFFF0) + 12 - data[6];
   }
 } else {
   byte cfg = (data[4] & 0x60);
   // at lower res, the low bits are undefined, so let's zero them
   if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
   else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
   else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
   //// default is 12 bit resolution, 750 ms conversion time
 }
 temp = (float)raw / 16.0;
 fahrenheit = celsius * 1.8 + 32.0;
 
 Serial.print(temp);
 Serial.print(" C ");

}

Hi,
Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

I see you are using pin4 as fan control with analogWrite, pin4 is not a PWM pin. 3,5,6,9,10 and 11 on the UNO are.
If you look at the UNO PCB you will see ~ signs next to pins that support PWM.

The output is not an analog voltage level, it is a PWM signal with variable duty cycle.

Tom… :slight_smile:

Im using a arduino nano and im using a none pwm fan.

Do i have to connect the fan to a output with a ~ symbol on it?

I think that i put the fan output on a none pwm output beacuse on arduinos product page the arduino nano shows that the pwm output is 3, 5, 6, 9, 10, and 11. i will try to change output port.

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What sort of fan are you trying to control?

Tom.. :slight_smile:

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Your other thread deleted.

  • Moderator

TomGeorge:
Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What sort of fan are you trying to control?

Tom.. :slight_smile:

there is a schematic on this website http://www.electroschematics.com/wp-content/uploads/2014/01/arduino-temperature-fan-speed-control-550x494.png

i used this schematic but i used a DS18B20 temp sensor instead of a lm35. Im using a 3 pin computer fan. i think that the problem is that im using a output that is not pwm beacuse im using output 2 and its not an pwm output so i will try to change it later.

Hi,
If you are using a computer fan it may not control very well with PWM or variable DC.
If it is a Brushless DC Motor, it has its own controller built in.
The third wire is usually a tacho OUTPUT, pulsing once per fan revolution.

Tom.... :slight_smile:

At first I tried using a lm35 and it worked pretty good but the readings from it wasn't accurate. I got the (with the lm35) fan to spinning at a sertan temperature but when I'm using the DS18B20 I won't get it to work properly( the fan turns on at max speed when then temp is over 27 or something)....