PWM controlled fan tacho wrong
PWM controlled fan tacho wrong
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)
-
- Posts: 9730
- Joined: Thu Nov 26, 2015 4:08 am
Re: PWM controlled fan tacho wrong
Before you go chasing a red herring, perhaps it's a good idea to check if the tacho output gives you back the correct rate to begin with? Can you hook up a LA or scope to that line to see if it makes sense?
Re: PWM controlled fan tacho wrong
Unfortunately I don't have that gear. All I have to measue is a digital multimeter.
But when used in my pc, both fans seems to work correctly.
Maybe ordering some cheap oscilloscope or asking to use one in the office could be possible though...
But when used in my pc, both fans seems to work correctly.
Maybe ordering some cheap oscilloscope or asking to use one in the office could be possible though...
Re: PWM controlled fan tacho wrong
Esp_sprite I too think that will reveal the cause which is that the signal while in essence is correct is noisy or has bounce.
Without a scope it could be tested by rotating very slowly to by hand an reporting the rpm.
Tom Meyers
Without a scope it could be tested by rotating very slowly to by hand an reporting the rpm.
Tom Meyers
IT Professional, Maker
Santiago, Dominican Republic
Santiago, Dominican Republic
Re: PWM controlled fan tacho wrong
As I wrote in my previous (not yet published by moderators) post, I don't own a scope unfortunately.
But when I turn the fan by hand while plugged to pwm, I still can only get 0 reading for speed.
If I unplug the pwm pin, let the fan spin up to its maximum the readings are correct, but as soon as I plug it back in (with still 0 PWM set in the ESP) the read speed will be zero in the next complete measurement period.
Re: PWM controlled fan tacho wrong
I tinkered around some more and found out I can in fact get correct speed readings for the fan if I use a seconds jumper cable on the tacho line and touch/thumbe around the two jumper cables. The effect gets lost when I crank up the fan to more than 150 (out of 255).
Is there a more practical solution than touching the cables / what is the cause of that?
Is there a more practical solution than touching the cables / what is the cause of that?
Re: PWM controlled fan tacho wrong
I now used a arduino nano as cheap oscilloscope to look at what I get
Here is the screenshot of the result. (Detected frequency is fluctuating between 24.4 and 25.6 Hz, forgot to turn it on in the screenshot. Fan should be around 800 RPM)
Does this show anything related to my problem?
Here is the screenshot of the result. (Detected frequency is fluctuating between 24.4 and 25.6 Hz, forgot to turn it on in the screenshot. Fan should be around 800 RPM)
Does this show anything related to my problem?
-
- Posts: 9730
- Joined: Thu Nov 26, 2015 4:08 am
Re: PWM controlled fan tacho wrong
Is this measured in the situation where your code returns faulty readings?
Re: PWM controlled fan tacho wrong
Yes it is
Re: PWM controlled fan tacho wrong
I got a proper oscilloscope now so here are some readings
100PWM 255PWM PWM Cable unplugged The measurings have been taken with channel 1 connected to the Pulled up tacho signal (where the cable would go to the ESP32)
100PWM 255PWM PWM Cable unplugged The measurings have been taken with channel 1 connected to the Pulled up tacho signal (where the cable would go to the ESP32)
Who is online
Users browsing this forum: No registered users and 92 guests