PWM controlled fan tacho wrong
Posted: Tue Dec 31, 2019 4:23 pm
Hello there,
I've encountered a problem with my esp32 but could not find any solution online yet, just a few posts facing similar problems but with no solution.
I wrote some code to control and read the fan speed of a 4pin PWM fan. Controlling the fan works fine, just one of my test fans is not turning off completly but that does not matter for now.
When I try to read the tacho signal of the fan, I get awkward readings. When the fan is turning I get values between 30k and 40k, When it stands still its 0.
When I unplug the PWM pin, I get the correct RPM values. The fan spins up automatically and I get readings fof 1620 which go down if I stop the fan with my hand.
I also noticed, setting values above 511 the fan speed is read correctly.
I have connected the ground pin of my fan to a ground pin of my esp32. I also use a pull up resistor for reading the tacho signal - both of which have been proposed in other threads I found.
Has anyone an idea?
How I set the speed:
[Codebox]
int pinFan1Rpm = 13;
int freq = 25000;
int fanChannel = 0;
int resolution = 8;
ledcSetup(fanChannel, freq, resolution);
ledcAttachPin(pinFan1Rpm, fanChannel);
ledcWrite(fanChannel, 255);
[/Codebox]
How I read the speed:
[Codebox]
int pinFan1Tacho = 34;
int fan1RPM = 0;
volatile int fan1InterruptCounter;
unsigned long previousmills;
#define calculationPeriod 1000
void ICACHE_RAM_ATTR handleInterruptFan1Tacho()
{
fan1InterruptCounter++;
}
attachInterrupt(digitalPinToInterrupt(pinFan1Tacho), handleInterruptFan1Tacho, FALLING);
previousmills = millis();
int count = fan1InterruptCounter;
fan1InterruptCounter = 0;
fan1RPM = count / 2 * 60 ;
Serial.println(fan1RPM);
[/Codebox]
If requested I could also write a working minimal example but I hope that the provided segments are enough to probably find the fault
Pinout:
Fan Vin => 12V
Fan GND => GND => ESP32 GND
Fan PWM => ESP32 Pin13
Fan Tacho => ESP32 Pin 34 (with pull up resistor to ESP32 3.3V)
I've encountered a problem with my esp32 but could not find any solution online yet, just a few posts facing similar problems but with no solution.
I wrote some code to control and read the fan speed of a 4pin PWM fan. Controlling the fan works fine, just one of my test fans is not turning off completly but that does not matter for now.
When I try to read the tacho signal of the fan, I get awkward readings. When the fan is turning I get values between 30k and 40k, When it stands still its 0.
When I unplug the PWM pin, I get the correct RPM values. The fan spins up automatically and I get readings fof 1620 which go down if I stop the fan with my hand.
I also noticed, setting values above 511 the fan speed is read correctly.
I have connected the ground pin of my fan to a ground pin of my esp32. I also use a pull up resistor for reading the tacho signal - both of which have been proposed in other threads I found.
Has anyone an idea?
How I set the speed:
[Codebox]
int pinFan1Rpm = 13;
int freq = 25000;
int fanChannel = 0;
int resolution = 8;
ledcSetup(fanChannel, freq, resolution);
ledcAttachPin(pinFan1Rpm, fanChannel);
ledcWrite(fanChannel, 255);
[/Codebox]
How I read the speed:
[Codebox]
int pinFan1Tacho = 34;
int fan1RPM = 0;
volatile int fan1InterruptCounter;
unsigned long previousmills;
#define calculationPeriod 1000
void ICACHE_RAM_ATTR handleInterruptFan1Tacho()
{
fan1InterruptCounter++;
}
attachInterrupt(digitalPinToInterrupt(pinFan1Tacho), handleInterruptFan1Tacho, FALLING);
previousmills = millis();
int count = fan1InterruptCounter;
fan1InterruptCounter = 0;
fan1RPM = count / 2 * 60 ;
Serial.println(fan1RPM);
[/Codebox]
If requested I could also write a working minimal example but I hope that the provided segments are enough to probably find the fault
Pinout:
Fan Vin => 12V
Fan GND => GND => ESP32 GND
Fan PWM => ESP32 Pin13
Fan Tacho => ESP32 Pin 34 (with pull up resistor to ESP32 3.3V)