While trying to read RPM from a computer fan using the 4-wire protocol, I am getting strange readings. +12 and GND from the fan are connected to a power supply, and the tachometer to the PIN-0 or a Teensy 3.2.
I wrote code to read the tachometer pulse using the internal pull-up and interrupts. The strange thing happening is that the number of recorded revolutions is increasing as I manually slow the fan using my fingers.
Here is the code :
#include <avr/io.h>
#include <avr/interrupt.h>
#include <elapsedMillis.h>
const int fanPulse = 0;
volatile long half_revolutions;
elapsedMillis timeElapsed;
void setup() {
Serial.begin(9600);
while(!Serial);
pinMode(fanPulse, INPUT_PULLUP);
attachInterrupt(fanPulse, rpm_fun, FALLING);
half_revolutions = 0;
// analogWriteFrequency(20, 25000); // Teensy 3.0 pin 3 also changes to 375 kHz
}
void rpm_fun() {
half_revolutions++;
}
void loop() {
if (timeElapsed < 1000)
return;
Serial.printf("%d revolutions in %d miliseconds\n", half_revolutions / 2, (unsigned long)timeElapsed);
timeElapsed = 0;
half_revolutions = 0;
}
Here is the strange output :
0 revolutions in 1000 miliseconds // Start
0 revolutions in 1000 miliseconds
82 revolutions in 1000 miliseconds
394 revolutions in 1000 miliseconds // "normal" reading
412 revolutions in 1000 miliseconds
576 revolutions in 1000 miliseconds
456 revolutions in 1000 miliseconds
554 revolutions in 1000 miliseconds
585 revolutions in 1000 miliseconds
799 revolutions in 1000 miliseconds // Slowing the fan
712 revolutions in 1000 miliseconds
691 revolutions in 1000 miliseconds
683 revolutions in 1000 miliseconds
625 revolutions in 1000 miliseconds
531 revolutions in 1000 miliseconds
239 revolutions in 1000 miliseconds
0 revolutions in 1000 miliseconds
0 revolutions in 1000 miliseconds // End