PROJECT - MEASURE FAN SPEED
Objective: measure the rotational speed of a three wire DC fan
You will need: Raspberry Pi, three-wire DC fan, 10kΩ resistor, breadboard and patch cables.
Many small DC brushless fans have a third wire that transmits pulses that can be used to calculate the rotational speed of the fan. The output lead is connected to a 10kΩ resistor and then to a GPIO pin set up as input. The Pi waits for the pin's state to change and calculates the fan speed from the number of pulses it counts per second. Fans usually generate two pulses per revolution so to convert pulses to RPM you need to divide by 120.
HOW IT WORKS
The Pi does not have interrupt pins as such, but you can configure a GPIO pin to detect a rising edge signal and then use the poll() function to wait for an event on the file descriptor for that pin - in this case when the file is ready to read. A bare-bones program is available here - it has no error checking at all and uses a hard-coded GPIO pin number for simplicity. This method of measuring frequency is used at mainsfrequency.uk.
TESTING AND LIMITATIONS
You can connect one of these R-102A low frequency square wave oscillators to your Pi to check that it's working and how it performs at various frequencies.
I have removed the link to Ebay as this item is no longer available, though there are alternatives.
The frequency generator can generate square waves from 2 to 2048Hz - just set the jumper on the board. The technical specification for this board has been removed by Ebay but you can find a copy here. I've started keeping copies of any specifications that are provided by a seller in Ebay, just in case the item is discontinued or Ebay removes the link. A Model 3 B+ measures up to 2048Hz perfectly well, which for a fan, equates to over 60,000RPM!
The Fritzing diagram is below. The 10kΩ resistor protects the Pi's GPIO. The negative side of the fan supply must be connected to the Pi's GND pin.