# PWM fan speed control

**URL:** <https://forum.arduino.cc/t/pwm-fan-speed-control/994661>\
**Category:** Programming\
**Created:** [May 22, 2022, 9:44pm UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661 "2022-05-22T21:44:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![EGUVEN](https://avatars.discourse-cdn.com/v4/letter/e/ac8455/32.png) [@EGUVEN](https://forum.arduino.cc/u/EGUVEN)\
**Post date:** [May 22, 2022, 9:44pm UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661/1 "2022-05-22T21:44:53Z")

</div>

While i was searching code for control my fan's speed with PWM i found this code. It is working partically but not working properly but i couldn't find what is wrong. I want that when i push any button that predefined fan speed will continously will work with that speed.

```cpp
  #define PIN_MEMBRAN_RIGHT 13
  #define PIN_MEMBRAN_UP 12
  #define PIN_MEMBRAN_DOWN 11
  #define PIN_MEMBRAN_SELECT 10
  #define PIN_MEMBRAN_LEFT 9
  #define PIN_MEMBRAN_START 8
  
  uint8_t membran_pin_list[] = {
    PIN_MEMBRAN_UP,
    PIN_MEMBRAN_DOWN,
    PIN_MEMBRAN_RIGHT,
    PIN_MEMBRAN_LEFT,
    PIN_MEMBRAN_SELECT,
    PIN_MEMBRAN_START
  };
  
  #define PIN_FAN 6 
  int PushButtonInput_1 = 13; // Push button - set PWM Fan speed %25
  int PushButtonInput_2 = 12; // Push button - set PWM Fan speed %50
  int PushButtonInput_3 = 11; // Push button - set PWM Fan speed %75
  int PushButtonInput_4 = 9; // Push button - set PWM Fan speed %100
  
  void setup() {
    pinMode(PIN_FAN, OUTPUT);
    digitalWrite(PIN_FAN, HIGH);
    pinMode(PushButtonInput_1,INPUT_PULLUP);
    pinMode(PushButtonInput_2,INPUT_PULLUP);
    pinMode(PushButtonInput_3,INPUT_PULLUP);
    pinMode(PushButtonInput_4,INPUT_PULLUP);
  }
  
  void loop() {
   //-----------------25%----------------------
   if(digitalRead(PushButtonInput_1)==HIGH){
     analogWrite(PIN_FAN, 64);
     //Serial.print(PushButtonInput_1);
   }
   
   //----------------50%---------------------
   if(digitalRead(PushButtonInput_2)==HIGH){
    analogWrite(PIN_FAN, 128);
   }
   
   //------------------75%-----------------------
   if(digitalRead(PushButtonInput_3)==HIGH) {
     analogWrite(PIN_FAN, 192);
   }
   //---------------------------------------------

   //------------------100%-----------------------
   if(digitalRead(PushButtonInput_4)==HIGH){
     analogWrite(PIN_FAN, 255);
   }
   //---------------------------------------------
}

```

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 22, 2022, 9:52pm UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661/2 "2022-05-22T21:52:02Z")

</div>

> [@EGUVEN](#):
>
> `if(digitalRead(PushButtonInput_1)==HIGH){`

Hi,  
from what I understand from your code, the buttons are connected between the pins (13,12,11 and 9) and the GND. Correct?  
If so, change all lines lines  
if(digitalRead(PushButtonInput\_X)==HIGH){  
to  
if(digitalRead(PushButtonInput\_X)==LOW){

---

<div class="post-metadata">

**Author:** ![EGUVEN](https://avatars.discourse-cdn.com/v4/letter/e/ac8455/32.png) [@EGUVEN](https://forum.arduino.cc/u/EGUVEN)\
**Post date:** [May 22, 2022, 10:00pm UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661/3 "2022-05-22T22:00:19Z")

</div>

> Hi,  
> from what I understand from your code, the buttons are connected between the pins (13,12,11 and 9) and the GND. Correct?

Yes. My membran has 7 buttons but i just defined 4 of them to control speed.

---

<div class="post-metadata">

**Author:** ![EGUVEN](https://avatars.discourse-cdn.com/v4/letter/e/ac8455/32.png) [@EGUVEN](https://forum.arduino.cc/u/EGUVEN)\
**Post date:** [May 22, 2022, 11:09pm UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661/4 "2022-05-22T23:09:10Z")

</div>

When i changed the code like this i got the result that i want.

```cpp
void loop() {
   //-----------------25%----------------------
   if(digitalRead(PushButtonInput_1)==LOW){
     analogWrite(PIN_FAN, 64);
     //Serial.print(PushButtonInput_1);
   }
   
   //----------------50%---------------------
   if(digitalRead(PushButtonInput_2)==LOW){
    analogWrite(PIN_FAN, 128);
   }
   
   //------------------75%-----------------------
   if(digitalRead(PushButtonInput_3)==LOW) {
     analogWrite(PIN_FAN, 192);
   }
   //---------------------------------------------

   //------------------100%-----------------------
   if(digitalRead(PushButtonInput_4)==LOW){
     analogWrite(PIN_FAN, 255);
   }
   //---------------------------------------------
}

```

I want to ask 1 more question.  
If i want to run my fan with %25 of it's power dividing 255/4=64 than, **analogWrite(PIN\_FAN, 64);**  
is this right ?

---

<div class="post-metadata">

**Author:** ![ruilviana](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ruilviana/32/722429_2.png) [@ruilviana](https://forum.arduino.cc/u/ruilviana)\
**Post date:** [May 22, 2022, 11:24pm UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661/5 "2022-05-22T23:24:10Z")

</div>

Hi,  
theoretically yes;  
but applying 25% of the pulse time to HIGH on the fan will make it spin at 25% of its speed?

---

<div class="post-metadata">

**Author:** ![EGUVEN](https://avatars.discourse-cdn.com/v4/letter/e/ac8455/32.png) [@EGUVEN](https://forum.arduino.cc/u/EGUVEN)\
**Post date:** [May 23, 2022, 7:20am UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661/6 "2022-05-23T07:20:18Z")

</div>

It is spinning, it worked but i am sure, it is not the %25 of it's total power. It is still working loudly but more less than %100. I want that it works with it's minimum capacity.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 19, 2022, 7:21am UTC](https://forum.arduino.cc/t/pwm-fan-speed-control/994661/7 "2022-11-19T07:21:14Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
