Arduino Fan Speed Controll Using Pulse Width Modulation (PWM) with Code

In this tutorial I have shown how to control CPU Fan using PWM using Arduino Nano board. We will be using Arduino IDE for programming.

Part List


Connection

    Below Images show how connections are done using Bread Board.

    Push Buttons with Connecting Wires used in Arduino Project


    How to do all connection with Arduino Nano and ther component



    Circuit Diagram

      Following image shows circuit diagram of Project and Connection Details,
      Schematic diagram for Fan Speed Control using PWM with Arduino

      Working

      You can watch following video to see how project works. Here I have shown Set speed and Actual speed of DC Fan.



      Code

      
          
      #define MAX 2145
      #define MIN 1110
      #define MID 1500
      #define PWM_PIN 3
      #define SEN_PIN 2
      #define PLUS_PIN 7
      #define MINUS_PIN 8

      #define OFFSET 879.80769230769230769230769230769
      #define CONSTANT 4.9615384615384615384615384615385

      int PWM = 0;
      float Speed_Req = 0,RPM = 0 ;
      volatile int tick = 0, Speed = 0;

      void SEN_ISR()
      {
      tick++;
      }

      void pciSetup(byte pin)
      {
      *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
      PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
      PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
      }

      ISR (PCINT2_vect) // handle pin change interrupt for D0 to D7 here
      {
      if (digitalRead(PLUS_PIN) == LOW )
      {
      if (Speed <= 99)Speed += 1;
      //Speed += 1;
      while (digitalRead(PLUS_PIN) == LOW);
      }
      }

      ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
      {
      if (digitalRead(MINUS_PIN) == LOW )
      {
      if (Speed > 0)Speed -= 1;
      //Speed -= 1;
      while (digitalRead(MINUS_PIN) == LOW);
      }
      }

      void setup() {
      // put your setup code here, to run once:
      Serial.begin(115200);
      pinMode(PWM_PIN, OUTPUT);
      pinMode(SEN_PIN, INPUT_PULLUP);
      pinMode(PLUS_PIN, INPUT_PULLUP);
      pinMode(MINUS_PIN, INPUT_PULLUP);
      attachInterrupt(digitalPinToInterrupt(SEN_PIN), SEN_ISR, FALLING);
      pciSetup(PLUS_PIN);
      pciSetup(MINUS_PIN);
      TCCR2B = TCCR2B & B11111000 | B00000111; // for PWM frequency of 30.64 Hz
      }

      void loop() {
      // put your main code here, to run repeatedly:
      //Speed_Req = ((((MAX - MIN) * Speed) / 100) + MIN );// - OFFSET ) / CONSTANT);

      Speed_Req = (MAX - MIN);
      Speed_Req = ((Speed_Req * Speed) / 100);
      Speed_Req = Speed_Req + MIN ; //(((MAX - MIN)));// * Speed));// / 100) + MIN;

      PWM = (Speed_Req - OFFSET) / CONSTANT;

      analogWrite(PWM_PIN, PWM);
      delay(1000);
      RPM = (tick * 30);
      Serial.print("SP REQ = ");
      Serial.print(Speed_Req);
      Serial.print('\t');
      /*
      Serial.print("PWM = ");
      Serial.print(PWM);
      Serial.print('\t');
      */
      Serial.print("Speed % = ");
      Serial.print(Speed);
      Serial.print('\t');
      Serial.print("RPM=");
      Serial.println(RPM);
      tick = 0;
      RPM = 0;
      }


      Wrapping Up

      In this project we have controlled speed of 12V DC CPU Cooling Fan using PWM. Actual speed of Fan and Calculated speed of Fan has variation which can be eliminated using PID.

      I hope you found this helpful, please leave comment for any question, query or suggestion.

      Post a Comment

      0 Comments