Driving a 4 wires 12 volt fan

Hi all, I have a question; I have connected a fan with 4 wires to an Arduino Due.
Wiring on the fan should be like this:
Pin 1 - Ground - Black
Pin 2 - 12v - Yellow
Pin 3 - Sense - Green
Pin 4 - Control - Blue

I attached the yellow to the 5V of the board and the black to the GND of the board.
The fan starts and is ok.

What I need is to be able from the Arduino to switch on/off the fan based on temperature.
The only way I managed to do it is using the Blue wire and connecting to the Digital pins (like 22) or PWM pin (like 3) and giving HIGH to start and LOW to stop.
But doing like this even if I set the control to LOW, at every Arduino loop, even if I do not change any pin, I got the fan to move a little (like 3 times and then stops as it should)...
And I do not know why.

I tried to put the yellow directly to the digital or pwm pins but it will not start.

Thank you if You can help me more.

UPDATE1: I think I understood why the fan works only powered by the 5V pin; because every other pin on the Arduino Due can output 3.3V MAX; so 5V seems to power it up but 3.3V is too low. I still do not understand why setting the control to LOW every loop() start it gives a current peak to the fan that makes it turn 2 or 3 times.

This is the source code:

#include <DallasTemperature.h>
#include <OneWire.h>

#define TEMPERATURE_PIN 2
#define FAN_PIN 22

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(TEMPERATURE_PIN);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int myspeed = 0;
int countmsecs = 0;

void initFan(){
  pinMode(FAN_PIN, OUTPUT);   // sets the pin as output 
  stopFan();
}
void startFan(){
  Serial.println("starting fan"); 
  digitalWrite(FAN_PIN, HIGH);
}
void stopFan(){
  Serial.println("stopping fan"); 
  digitalWrite(FAN_PIN, LOW);
}

void setup()
{
  Serial.begin(9600);
  Serial.println("setup start"); 

  initFan();
    
  sensors.begin(); 
  Serial.println("setup end"); 
}

void loop()
{
  countmsecs++;
  
  //Serial.println("loop start");
  
  if (countmsecs>5000){
    countmsecs = 0;
    //delay(5000);

    Serial.print("Requesting temperatures...");
    sensors.requestTemperatures(); // Send the command to get temperatures
    Serial.println("DONE");
    
    Serial.print("Temperature for Device 1 is: ");
    // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
    float temp = sensors.getTempCByIndex(0); // from 27.34 to 273
    Serial.println(temp);
    Serial.print("myspeed="); 
    Serial.println(myspeed); 
    
    if (temp>26){
      if (myspeed!=255){
        startFan();
        myspeed = 255;
      }
    }
    else{
      if (myspeed!=0){
        stopFan();
        myspeed = 0;  
      }
    }
  }

  delay(1);
  //Serial.println("loop end");
}

Best regards,
Camillo

Can you share data sheet for temperature sensor and fan being used .
Delay time is too short.

you can try this , forget everything . you can do this keep setup as like in your program

void loop()
{
startFan();
delay(1000);
stopFan();
delay(3000);
}

whether u can control motor like this.

Fan is: ADDA AD0912UX-A7BGL
Temperature is: SHIELD DALLAS DS18B20

I tried Your script, but I do not know why, it seems on the stop, the fan turn 3 times and stops.
If I increase the timing it is ok:

void loop()
{
startFan();
delay(10000);
stopFan();
delay(10000);
}

But now I have to add the test on the temperature.
Do You have other hints?
Thank you!
Camillo

When you keep timing uniform . It stay for 10000ms on and off for 10000ms.
In my program on for 1000ms &off 3000ms that reason u find3 times since delay is too low.
From that we can understand that ur motor can be controller through blue wire itself,

Now can you share data sheet of temperature sensor u r using.

AMPS-N:
When you keep timing uniform . It stay for 10000ms on and off for 10000ms.
In my program on for 1000ms &off 3000ms that reason u find3 times since delay is too low.
From that we can understand that ur motor can be controller through blue wire itself,

Now can you share data sheet of temperature sensor u r using.

This is the temperature sensor I bought:
http://m.ebay.it/itm/330995666100?cmd=VIDESC&gxo=true

Thank you
Camillo

Can you share data sheet for the temperature sensor> i could not able to located datasheet in mentioned link

#include <OneWire.h>
#include <DallasTemperature.h>
 
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
 
// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}
 
 
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");

  Serial.print("Temperature for Device 1 is: ");
  Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? 
    // You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
 
}

use this code. Now just send me snap shot of output file.

Once u make ensure temprature sensor working.
Now tell me what are ranges of temperature u wanna control Motor action

Hi I have not found the data sheet but it is a simple DALLAS DS18B20 shield. The code is working, I already tested, the temperature decrease when I run the fan over the temperature probe.
I am just having that strange movement on the fan even if I put the control to zero. Maybe is the one wire library which reset the pins of the fan for a moment?
I tried like this:
Loop start:
Get temperature ;
If temperature more than 26.5 and fan not running: start fan;
ElseIf temperature less than 26 and fan is running: stop fan;
Wait 10 seconds ;
Loop end:

Obviously these temperatures are only for test, but to go from 26.5 to 26 and vice versa can take some minutes.

When fan is switched off I have every loop that strange little movement on the fan like as the control pin of the fan was reset to HIGH and then back to LOW.

any hint?
Thank you
Camillo

 if (temp>26){
      if (myspeed!=255){
        startFan();
        myspeed = 255;
      }
    }
    else{
      if (myspeed!=0){
        stopFan();
        myspeed = 0;  
      }
    }

replace with

if((temp>=26)&&(myspeed!=255))
{
 startFan();
        myspeed = 255;
}else
{
  stopFan();
        myspeed = 0;  
}

In your codedelay(1) represent 1ms not 1 s.

to wait for 10s you should set time to delay(10000)

Yes I changed the code as You advised but every time the loop is called the fan have a slight movement:

#include <DallasTemperature.h>
#include <OneWire.h>

#define TEMPERATURE_PIN 2
#define FAN_PIN 22

// Setup a oneWire instance to communicate with any OneWire devices 
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(TEMPERATURE_PIN);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int myspeed = 0;

void initFan(){
  pinMode(FAN_PIN, OUTPUT);   // sets the pin as output 
  stopFan();
}
void startFan(){
  Serial.println("starting fan"); 
  digitalWrite(FAN_PIN, HIGH);
  myspeed=255;
}
void stopFan(){
  Serial.println("stopping fan"); 
  digitalWrite(FAN_PIN, LOW);
  myspeed=0;
}
float getTemp(){
    Serial.print("Requesting temperatures...");
    sensors.requestTemperatures(); // Send the command to get temperatures
    Serial.println("DONE");
    
    Serial.print("Temperature for Device 1 is: ");
    // Why "byIndex"? You can have more than one IC on the same bus. 
    // 0 refers to the first IC on the wire
    float temp = sensors.getTempCByIndex(0); // from 27.34 to 273
    Serial.println(temp);
    return temp;
}

void setup()
{
  Serial.begin(9600);
  Serial.println("setup start"); 

  initFan();
    
  sensors.begin(); 
  Serial.println("setup end"); 
}

void loop()
{
  float temp = getTemp();
  if (temp>26.5 && myspeed==0){
    startFan();
  }
  else if (temp<26 && myspeed==255){
    stopFan();
  }
  delay(10000);
}

Here condition should be

 if (temp>26.5 &&( digitalread(FAN_PIN)==LOW)){
    startFan();
  }
  else if (temp<26 &&( digitalread(FAN_PIN)==HIGH)){
    stopFan();
  }

Becaus

AMPS-N:
Here condition should be

 if (temp>26.5 &&( digitalread(FAN_PIN)==LOW)){

startFan();
  }
  else if (temp<26 &&( digitalread(FAN_PIN)==HIGH)){
    stopFan();
  }



Becaus

I am sorry, why? how this can help?
Thank you!