# RPM control using PWM for blower ba5025h12b

**URL:** <https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273>\
**Category:** Programming\
**Created:** [June 19, 2024, 6:18am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273 "2024-06-19T06:18:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 19, 2024, 6:18am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/1 "2024-06-19T06:18:32Z")

</div>

**RPM control by varying PWM**  
**connections:**  
the Arduino's ground (GND) is connected to the 0V of an external 12V power supply, establishing a common ground. The Arduino's 5V pin is connected through a 10k ohm pull-up resistor to the fan's tacho output (yellow wire), and Arduino pin 2 is used to read this tacho signal for RPM measurement. Arduino pin 9 is connected to the fan's control input (blue wire) to provide PWM signals for speed control. The fan is powered directly from the 12V supply, with its power input (red wire) connected to 12V and its ground (black wire) connected to 0V. This configuration allows the Arduino to control and monitor the fan's speed.

**1st method:**  
-Arduino in default settings  
-pin 9 outputs 490hz pwm freq  
-digital pin2 acts as interrupt to launch the subroutine counter to keep track of pulses

- The motor typically has a built-in sensor ( Hall effect sensor) that generates pulses as the motor rotates. Each pulse corresponds to a fixed angular displacement of the motor shaft.
- the feedback provided by the tachometer (RPM sensor) allows the Arduino to monitor the motor's actual speed, enabling it to adjust the PWM signal accordingly to achieve and maintain the desired speed setpoint. This closed-loop control mechanism helps ensure precise speed control and regulation of the motor.  
-in default settings max pwm value is 255, where we increment 51 (0%,20%,40% and so on till 100%)  
-further we send the pwm value to fan using analogWrite command and wait 5 secs and further reset count to zero and count how many pulses occur in 1 sec and to convert it to min we multiply by 60 and divide by 2 as there are 4 pulse magnets

**Code:**  
//LOWSPEEDPWM

```arduino
cost int fan_control_pin = 9;
volatile unsigned long count = 0; 
unsigned long start_time;
int rpm;
const int pulses_per_revolution = 2;

void setup() {
  pinMode(fan_control_pin, OUTPUT);
  analogWrite(fan_control_pin, 0);
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), counter, RISING);
}

void loop() {
  for (int pwm = 0; pwm <= 255; pwm += 51) {
    analogWrite(fan_control_pin, pwm);
    delay(5000); 

    start_time = millis();
    count = 0;
    while ((millis() - start_time) < 1000) {
   
    }

    rpm = (count / pulses_per_revolution) * 60;  
    Serial.print("PWM = ");
    Serial.print(map(pwm, 0, 255, 0, 100));  
    Serial.print("%, Speed: ");
    Serial.print(rpm);
    Serial.println(" rpm");
  }
}

void counter() {
  count++;
}

```

**Output:**  
PWM = 20%, Speed: 32520 rpm  
PWM = 40%, Speed: -4756 rpm  
PWM = 60%, Speed: -16712 rpm  
PWM = 80%, Speed: -17868 rpm  
PWM = 100%, Speed: -19444 rpm

**2nd method:**  
-high freq pwm  
-pin 9 still used , outputs 25khz not 490hz  
-reconfigures pin 9 and 10 for high pwm output  
-to get 25khz we need to get max value to 320 and not 255  
-instead of analog output commands we use OCR1A and ORC1B to get pwm on these pins

- we increment 64 here and instead of analog write we set pwm=OCR1A

**Code:**

```arduino
//HIGHSPEEDPWM
const int fan_control_pin = 9;
volatile unsigned long count = 0; // volatile for interrupt use
unsigned long start_time;
int rpm;
const int pulses_per_revolution = 2; // Number of pulses per fan revolution

// Declare the counter function before its use in setup
void counter(); 

void setup() {
  // Timer1 configuration for PWM on pin 9
  TCCR1A = 0; // Clear Timer1 control registers
  TCCR1B = 0;
  TCNT1 = 0; // Clear Timer1 counter register
  TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11); // Mode 14: Fast PWM, ICR1 is top, non-inverting mode
  TCCR1B = _BV(WGM13) | _BV(CS10); // No prescaler, Fast PWM
  ICR1 = 320; // Top value for PWM resolution

  pinMode(fan_control_pin, OUTPUT);
  OCR1A = 0; // Ensure no PWM signal is sent at startup
  OCR1B = 0;

  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), counter, RISING); // Interrupt on pin 2 for tachometer signal
}

void loop() {
  for (int pwm = 0; pwm <= 320; pwm += 64) {
    OCR1A = pwm;  
    delay(5000);  

    start_time = millis();
    count = 0;
    while ((millis() - start_time) < 1000) {
    
    }

    rpm = (count * 60) / pulses_per_revolution; 
    Serial.print("PWM = ");
    Serial.print(map(pwm, 0, 320, 0, 100));  
    Serial.print("%, Speed: ");
    Serial.print(rpm);
    Serial.println(" rpm");
  }
}

void counter() {
  count++;  
}

```

**Output:**  
PWM = 0%, Speed: 6270 rpm  
PWM = 20%, Speed: 9732 rpm  
PWM = 40%, Speed: -26494 rpm  
PWM = 60%, Speed: 10751 rpm  
PWM = 80%, Speed: 4300 rpm  
PWM = 100%, Speed: -17824 rpm

**3rd method:**

- Control a 4-pin CPU fan using PWM at 25khz and then read back the fan speed using a 10kohm pullup resistor.  
-same connections

**Code:**

```arduino
#include <PWM.h> 

volatile int half_revolutions; // Allow half_revolutions to be accessed in interrupt
int rpm; // Set rpm as an integer
int current_pwm = 0; // Set initial PWM value (0% duty cycle)
const int pulses_per_revolution = 2; // Set the number of magnets per revolution
void setup() {
  InitTimersSafe(); // Initialize timers for PWM control
  bool success = SetPinFrequencySafe(9, 25000); // Set PWM frequency to 25kHz
  pwmWrite(9, current_pwm); // Set initial PWM value

  pinMode(2, INPUT); // Set RPM pin to digital input
  half_revolutions = 0;
  rpm = 0;

  Serial.begin(9600);
}

void loop() {
  for (int pwm = 0; pwm <= 255; pwm += 51) { // Vary PWM from 0 to 100%
    pwmWrite(9, pwm); // Set PWM value
    delay(5000); // Wait for 5 seconds at each PWM value

    // Measure RPM
    sei(); // Enable interrupts
    attachInterrupt(digitalPinToInterrupt(2), fan_rpm, RISING); // Record pulses as they rise
    delay(1000); // Wait for 1 second to measure RPM
    detachInterrupt(digitalPinToInterrupt(2));
    cli(); // Disable interrupts

    rpm = (half_revolutions * 60) / pulses_per_revolution; // Calculate RPM based on half revolutions

    Serial.print("PWM: ");
    Serial.print(map(pwm, 0, 255, 0, 100)); // Map PWM value to percentage
    Serial.print("%, SPEED: ");
    Serial.print(rpm);
    Serial.println(" rpm");
    Serial.println();

    rpm = 0;
    half_revolutions = 0;
  }

  // Reset PWM to 0% duty cycle at the end
  pwmWrite(9, 0);
}

void fan_rpm() {
  ++half_revolutions; // Increment half revolutions
}

```

**Output:**  
PWM: 0%, SPEED: 7860 rpm  
PWM: 20%, SPEED: 8085 rpm  
PWM: 40%, SPEED: 8145 rpm  
PWM: 60%, SPEED: 7905 rpm  
PWM: 80%, SPEED: 7740 rpm  
PWM: 100%, SPEED: 7860 rpm

PWM: 0%, Half-revolutions : 477 (rpm=7155)  
PWM: 20%, Half-revolutions : 14482(rpm=217230)  
PWM: 40%, Half-revolutions : 17080(rpm=256200)  
PWM: 60%, Half-revolutions : 21239(rpm=318585)  
PWM: 80%, Half-revolutions : 27077(rpm=406155)  
PWM: 100%, Half-revolutions : 23436(rpm=351840)

**Expected RPM values:**

2%  
2500  
RPM  
10%  
6700  
RPM  
20%  
12400  
RPM  
30%  
17700  
RPM  
40%  
22200  
RPM  
50%  
26200  
RPM  
60%  
29600  
RPM  
70%  
33200  
RPM  
80%  
36700  
RPM  
90%  
40000  
RPM  
100%  
40000  
RPM

**I'm getting varied values please help me as soon as possible**

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [June 19, 2024, 6:29am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/2 "2024-06-19T06:29:12Z")

</div>

Welcome to the forum

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum

> [@kaushik0\_0](#):
>
> Code:

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'  
  
Use [color = red] **code tags** [/color] (the \< CODE/ \> icon above the compose window)&nbsp;to make it easier to read and copy for examination   
  
[https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum)

In my experience the easiest way to tidy up the code and add the code tags is as follows  
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 19, 2024, 6:37am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/3 "2024-06-19T06:37:46Z")

</div>

pls tell me what to do to the code to make it run so i get correct rpm values

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [June 19, 2024, 8:30am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/4 "2024-06-19T08:30:22Z")

</div>

First do what @UKHeliBob suggested.  
We can't read your code as posted.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [June 19, 2024, 12:38pm UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/5 "2024-06-19T12:38:11Z")

</div>

Hi, @kaushik0_0  
Welcome to the forum.  
We need some basic information so we can help you.

Can you please tell us your electronics, programming, arduino, hardware experience?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?  
Hand drawn and photographed is perfectly acceptable.  
Please include ALL hardware, power supplies, component names and pin labels.

What model Arduino are you using?

Can you please post a link to specs/data on the fan?  
Does the data indicate the PWM frequency required?

Do you have a DMM? (Digital MultiMeter)

Thanks.. Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 24, 2024, 4:33am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/6 "2024-06-24T04:33:17Z")

</div>

i did that can you please help

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 24, 2024, 4:35am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/7 "2024-06-24T04:35:59Z")

</div>

yeah sure  
i am a beginner in arduino programming  
using arduino IDE with arduino uno

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/a/7/d/a7d825a41682f8680759a20da1f71b80d7d12ba4.png)  
the above is my circuit diagram  
the fan is blower ba5025h12b  
i basically want to control RPM using PWM  
but i am getting negative values of rpm when i power it with 12v external supply and run the code  
and yes i have DMM

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 24, 2024, 5:18am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/9 "2024-06-24T05:18:36Z")

</div>

```arduino
//method 1:
const int pwm_pin = 9;
const int tach_pin = 2;
volatile unsigned long pulse_count = 0;
unsigned long start_time;
int rpm;
const int pulses_per_revolution = 2;

void setup() {
  pinMode(pwm_pin, OUTPUT);
  analogWrite(pwm_pin, 0);

  pinMode(tach_pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(tach_pin), pulseCounter, RISING);

  Serial.begin(9600);
}

void loop() {
  for (int pwm = 0; pwm <= 255; pwm += 51) {
    analogWrite(pwm_pin, pwm);
    delay(5000);

    pulse_count = 0;
    start_time = millis();
    delay(1000);

    rpm = (pulse_count * 60) / pulses_per_revolution;

    Serial.print("PWM is: ");
    Serial.print(map(pwm, 0, 255, 0, 100));
    Serial.print("%, RPM: ");
    Serial.print(rpm);
    Serial.println(" rpm");
  }
}

void pulseCounter() {
  pulse_count++;
}

```

i tried the above code also but I'm getting huge varying outputs can you please help me out asap  
output:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/5/7/05788afb902cbd417d904e5e6aed0118a8be8a44.png)

but according to datasheet output is supposed to be this:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/b/5/4/b544ef3016b2b0b781a7f78420b3a02d35068a6d.jpeg)

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 24, 2024, 5:41am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/10 "2024-06-24T05:41:13Z")

</div>

i also used the below code to count the pulseper revolution as it is required for rpm : that also gives me varies values:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/d/a/9daedf337e30baa4f1d41d5d87a81c81eed3f792.png)  
code:

```arduino
const int tach_pin = 2;
volatile unsigned long pulse_count = 0;
unsigned long start_time;
unsigned long elapsed_time;
int known_rpm = 1200;
float pulses_per_revolution;

void setup() {
  pinMode(tach_pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(tach_pin), pulseCounter, RISING);
  Serial.begin(9600);
}

void loop() {
  pulse_count = 0;
  start_time = millis();
  delay(1000);

  elapsed_time = millis() - start_time;

  pulses_per_revolution = (pulse_count * 60.0) / known_rpm;

  Serial.print("Pulses counted in ");
  Serial.print(elapsed_time);
  Serial.print(" ms: ");
  Serial.println(pulse_count);

  Serial.print("Pulses per revolution: ");
  Serial.println(pulses_per_revolution);

  delay(5000);
}

void pulseCounter() {
  pulse_count++;
}

```

the pulses per reV according to many fans are around 2 right?

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [June 24, 2024, 9:30am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/11 "2024-06-24T09:30:52Z")

</div>

What does the datasheet say about the tach output?  
Is it open collector, NPN, PNP, 12V pulse?

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [June 24, 2024, 1:05pm UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/12 "2024-06-24T13:05:57Z")

</div>

Hi, @kaushik0_0

Can you post some images of your project?  
So we can see your component layout.  
Please show your 12V power supply.

Thanks Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 25, 2024, 5:48am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/13 "2024-06-25T05:48:41Z")

</div>

not sure have asked the vendors will let you know soon nothing is mentioned in data sheet though but it is powered by 12v and the board attached to the blower has hall sensor integrated

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 25, 2024, 5:49am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/14 "2024-06-25T05:49:54Z")

</div>

yeah sure will do

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 25, 2024, 5:56am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/15 "2024-06-25T05:56:48Z")

</div>

![WhatsApp Image 2024-06-25 at 11.23.40_f785175c](https://europe1.discourse-cdn.com/arduino/original/4X/3/4/0/3407a8724c5350cab959f5b374ea8ad285f22754.jpeg)  
 ![WhatsApp Image 2024-06-25 at 11.24.38_75c4e030](https://europe1.discourse-cdn.com/arduino/original/4X/6/5/6/656a3d3668186460652dad82a2f62273eaac5cb7.jpeg)

the above is the 12v power supply i used

the connections are:  
The Arduino's 5V pin is connected through a 10k ohm pull-up resistor to the fan's tacho output (yellow wire), and Arduino pin 2 is used to read this tacho signal for RPM measurement. Arduino pin 9 is connected to the fan's control input (blue wire) to provide PWM signals for speed control. The fan is powered directly from the 12V supply, with its power input (red wire) connected to 12V and its ground (black wire) connected to 0V. This configuration allows the Arduino to control and monitor the fan's speed.

 ![WhatsApp Image 2024-06-25 at 11.23.39_363c661b](https://europe1.discourse-cdn.com/arduino/original/4X/2/4/4/24487a4b69825609c26e7226f3091f62978de28c.jpeg)  
 ![WhatsApp Image 2024-06-25 at 11.23.39_4e3de2f5](https://europe1.discourse-cdn.com/arduino/original/4X/f/f/d/ffd0ce94ebcdab5f0b5933f8d1cd2991fe84df7f.jpeg)  
 ![WhatsApp Image 2024-06-25 at 11.26.28_af92f1f2](https://europe1.discourse-cdn.com/arduino/original/4X/f/5/8/f58cc926ef129f8439eac028ddf8be23a54366b1.jpeg)

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 25, 2024, 6:47am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/16 "2024-06-25T06:47:21Z")

</div>

sir it is an open collector NPN circuit where i have pulled up the tach output with a 10k resistor and connected to 5V of the arduino

---

<div class="post-metadata">

**Author:** ![JCA34F](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jca34f/32/340148_2.png) [@JCA34F](https://forum.arduino.cc/u/JCA34F)\
**Post date:** [June 25, 2024, 6:58am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/17 "2024-06-25T06:58:45Z")

</div>

How many pulses per revolution?  
PPR is fixed in hardware and does not vary, so if the rotor turns 1 revolution, how many pulses are produced?

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 25, 2024, 7:27am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/18 "2024-06-25T07:27:26Z")

</div>

there are 2 Pulses per revolution when fan runs for one round but when i used 2 in the code it still was giving me negative value of rpm and like when i reverse engineered it to find the pulses per revolution from a fixed rpm i was getting varied values of ppr

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 25, 2024, 7:39am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/19 "2024-06-25T07:39:08Z")

</div>

I have set the pwm duty cycle as 100% and tried but I'm not getting rpm closer to 40,000 as mentioned in datasheet

```arduino
const int pwm_pin = 9;
const int tach_pin = 2;
volatile unsigned long pulse_count = 0;
unsigned long start_time;
int rpm;
const int pulses_per_revolution = 2;  

void setup() {
  pinMode(pwm_pin, OUTPUT);
  analogWrite(pwm_pin, 0);

  pinMode(tach_pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(tach_pin), pulseCounter, RISING);

  Serial.begin(9600);
}

void loop() {

  int pwm = 255;
  analogWrite(pwm_pin, pwm);
  delay(5000);

  pulse_count = 0;
  start_time = millis();
  delay(1000);

  rpm = (pulse_count * 60) / pulses_per_revolution;

  Serial.print("PWM is: ");
  Serial.print(map(pwm, 0, 255, 0, 100));
  Serial.print("%, RPM: ");
  Serial.print(rpm);
  Serial.println(" rpm");
}

void pulseCounter() {
  pulse_count++;
}

```

output:  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/d/3/5/d35aecb1d937bceab30ad5a72a23a2d4c21e67aa.png)

---

<div class="post-metadata">

**Author:** ![JCA34F](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jca34f/32/340148_2.png) [@JCA34F](https://forum.arduino.cc/u/JCA34F)\
**Post date:** [June 25, 2024, 8:09am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/20 "2024-06-25T08:09:22Z")

</div>

See what this does:

```arduino
const int pwm_pin = 9;
const int tach_pin = 2;
volatile unsigned long pulse_count = 0;
unsigned long pulseCopy;
unsigned int rpm;
const int pulses_per_revolution = 2;  

void setup() {
  pinMode(pwm_pin, OUTPUT);
  analogWrite(pwm_pin, 0);

  pinMode(tach_pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(tach_pin), pulseCounter, FALLING);

  Serial.begin(9600);
}

void loop() {

  int pwm = 255;
  analogWrite(pwm_pin, pwm);
  delay(1000);
  noInterrupts();
  pulseCopy = pulse_count;
  pulse_count = 0;
  interrupts();
  rpm = (pulseCopy * 60) / pulses_per_revolution;

  Serial.print("PWM is: ");
  Serial.print(map(pwm, 0, 255, 0, 100));
  Serial.print("%, RPM: ");
  Serial.print(rpm);
  Serial.println(" rpm");
}

void pulseCounter() {
  pulse_count++;
}
```

---

<div class="post-metadata">

**Author:** ![kaushik0\_0](https://avatars.discourse-cdn.com/v4/letter/k/71c47a/32.png) [@kaushik0\_0](https://forum.arduino.cc/u/kaushik0_0)\
**Post date:** [June 25, 2024, 8:50am UTC](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273/21 "2024-06-25T08:50:36Z")

</div>

![image](https://europe1.discourse-cdn.com/arduino/original/4X/5/7/9/5793c52abec20ef0a1e56a95c3f3d25c6269587d.png)  
i tried didnt make a difference its still varying too much

[Next page](https://forum.arduino.cc/t/rpm-control-using-pwm-for-blower-ba5025h12b/1273273.md?page=2)
