user16810: I am using a PC 12V fan (40x40mm), which I control speed with voltage (with adjustable power supply). I would like to measure the frequency of the fan with Arduino and read it on serial monitor.
I've googled over some results and I came across pulseIn(), which I don't quite understand. I've also used this code, which I found here, but it doesn't work for me. I've added serial.print and delay line.
My output of this program is: "0000000" etc. and nothing else.
My goal is to set the frequency of the fan to around 50-500 Hz somewhere.
int pin = 7;
unsigned long duration;

void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
  Serial.print(duration);
  delay(1);
}

My schematics:


user16085: The pulseIn measures the duration of a high pulse on the pin.  The reference page explains it as well as I ever could.  If there is a specific thing you don't understand from it, please ask.  However, I don't think it will achieve what you want.
The circuitry in the fan connects the third (yellow?) wire to ground twice per revolution of the fan.  To make this into a "pulse" you need to pull the pin high.  Twice per revolution, the fan will pull it low.
What you now have is a signal which, for every revolution of the fan, goes HIGH, LOW, HIGH, LOW.  We want to measure the time from one rising edge to the next.  This is the time for half a rotation of the fan.
Here's some untested code:
// Wait for signal to go low
while (HIGH==digitalRead(pin)) { /* Do nothing */ }

// Start timing
unsigned long start_time = micros();

// Wait for signal to go high and low again
while (LOW==digitalRead(pin)) { /* Do nothing */ }
while (HIGH==digitalRead(pin)) { /* Do nothing */ }

// Stop timing
unsigned long end_time = micros();

// How long did that take?
unsigned long duration  = end_time - start_time;

That code can be improved in various ways but I hope it will let you understand what's going on.
That tells us how LONG half a cycle takes.  You can easily find how long a whole cycle would take -- double it.  If you want the frequency in revolutions-per-minute, you need to work out how many whole cycles you can get in a minute, which is the same as the number of half-cycles in half a minute:
unsigned long rpm = 30000000L / duration;


16810: <p>I am using a PC 12V fan (40x40mm), which I control speed with voltage (with adjustable power supply). I would like to measure the frequency of the fan with Arduino and read it on serial monitor.</p>

<p>I've googled over some results and I came across <code>pulseIn()</code>, which I don't quite understand. I've also used this code, which I found <a href="https://www.arduino.cc/en/Serial/Print" rel="nofollow noreferrer">here</a>, but it doesn't work for me. I've added <code>serial.print</code> and delay line.</p>

<p>My output of this program is: "0000000" etc. and nothing else.</p>

<p>My goal is to set the frequency of the fan to around 50-500 Hz somewhere.</p>

<pre><code>int pin = 7;
unsigned long duration;

void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
  Serial.print(duration);
  delay(1);
}
</code></pre>

<p>My schematics:</p>

<p><a href="https://i.stack.imgur.com/3vesi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3vesi.png" alt="enter image description here"></a></p>
 16085: <p>The <code>pulseIn</code> measures the duration of a high pulse on the pin.  The <a href="https://www.arduino.cc/en/Reference.PulseIn" rel="nofollow noreferrer" title="reference">reference page</a> explains it as well as I ever could.  If there is a specific thing you don't understand from it, please ask.  However, I don't think it will achieve what you want.</p>

<p>The circuitry in the fan connects the third (yellow?) wire to ground twice per revolution of the fan.  To make this into a "pulse" you need to pull the pin high.  Twice per revolution, the fan will pull it low.</p>

<p>What you now have is a signal which, for every revolution of the fan, goes <code>HIGH</code>, <code>LOW</code>, <code>HIGH</code>, <code>LOW</code>.  We want to measure the time from one rising edge to the next.  This is the time for half a rotation of the fan.</p>

<p>Here's some untested code:</p>

<pre><code>// Wait for signal to go low
while (HIGH==digitalRead(pin)) { /* Do nothing */ }

// Start timing
unsigned long start_time = micros();

// Wait for signal to go high and low again
while (LOW==digitalRead(pin)) { /* Do nothing */ }
while (HIGH==digitalRead(pin)) { /* Do nothing */ }

// Stop timing
unsigned long end_time = micros();

// How long did that take?
unsigned long duration  = end_time - start_time;
</code></pre>

<p>That code can be improved in various ways but I hope it will let you understand what's going on.</p>

<p>That tells us how LONG half a cycle takes.  You can easily find how long a whole cycle would take -- double it.  If you want the frequency in revolutions-per-minute, you need to work out how many whole cycles you can get in a minute, which is the same as the number of half-cycles in half a minute:</p>

<pre><code>unsigned long rpm = 30000000L / duration;
</code></pre>
