I am having trouble reading the RPM of a PWM controllable fan to an LCD. I have tried several examples from several places and have been unable to get them to work. I know I am missing something small I just can't figure out what. Any help would be greatly appreciated. This is the final piece to my puzzle.Sorry the sketch is a little messy, I just haven't cleaned it up yet. I am using a four wire fan blue wire is control, yellow wire is RPM.
#include <LiquidCrystal.h>
float tempC;
float tempF;
float voltage;
int reading;
int interval = 50;
int fanPulse = 2;
unsigned long pulseDuration;
//constants
const int tempPin = 0; //analog channel
const int fan1 = 3; //fan 1
const int fan2 = 6;//fan 2
const int setPointF = 68; //changing this set point will determine cabinet temperature and will be diplayed on the LCD
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
pinMode(fan1, OUTPUT); // Set pin for output to control fan 1
pinMode(fan2, OUTPUT); // Set pin for output to control fan 2
Serial.print(9600);
pinMode(fanPulse,INPUT);
digitalWrite(fanPulse,HIGH);
// establishes fan start up on lcd
lcd.begin(16, 2);
lcd.print(" Starting Up");
lcd.setCursor(0,1);
lcd.print(" Please Wait");
delay(10000);
lcd.clear();
}
void readPulse() {
pulseDuration = pulseIn(fanPulse,LOW);
double frequency = 1000000/pulseDuration;
Serial.print("pulse duration:");
Serial.println(pulseDuration);
Serial.print("time for full rev. (microsec.):");
Serial.println(pulseDuration*2);
Serial.print("freq. (Hz):");
Serial.println(frequency/2);
Serial.print("RPM:");
Serial.println(frequency/2*60);
}
void loop() {
//TMP36 voltage at the output varies linearly with ambient temperature.
//As a result,it can be used as a thermometer according to the equation:
// ?C = (Vout(V) - .5)*100
//To find Vout in V, we use the following equation for a 5V input
// Vout(V) = AnalogReading*(5/1024)
reading = analogRead(tempPin); //read the value from the sensor
voltage = reading*(5.0/1024); //convert reading to voltage (in V), for 5V input
tempC = (voltage-0.5)*100; //convert voltage to temperature
tempF = ((tempC*9/5)+32); //convert C temperature to F
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,0);// set the cursor to column 0, line 1
lcd.print(tempF); //Print Fahrenheit temperature to LCD
lcd.print((char)223); // degree symbol
lcd.print("F ");
lcd.setCursor(10,0);
lcd.print("RPM:");
lcd.setCursor(0,5); //print set temp to lcd
lcd.print("Set:");
lcd.setCursor(4,1);
lcd.print(setPointF);
lcd.print((char)223);
lcd.print("F");
lcd.setCursor(9,1);
// If the temperature is higher than the set point, run the fans.
// Fans reach full speed when temperature is more than 10°F above setpoint.
analogWrite(fan1,constrain( (tempF - setPointF) * 25, 0, 255));
analogWrite(fan2,constrain( (tempF - setPointF) * 25, 0, 255));
}