Page 1 of 3

PWM controlled fan tacho wrong

Posted: Tue Dec 31, 2019 4:23 pm
by maxus1998
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)

Re: PWM controlled fan tacho wrong

Posted: Wed Jan 01, 2020 11:29 am
by ESP_Sprite
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

Posted: Wed Jan 01, 2020 12:21 pm
by maxus1998
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...

Re: PWM controlled fan tacho wrong

Posted: Wed Jan 01, 2020 5:57 pm
by tommeyers
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

Re: PWM controlled fan tacho wrong

Posted: Wed Jan 01, 2020 6:30 pm
by maxus1998
tommeyers wrote:
Wed Jan 01, 2020 5:57 pm
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
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

Posted: Fri Jan 03, 2020 9:47 am
by maxus1998
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?

Re: PWM controlled fan tacho wrong

Posted: Sun Jan 05, 2020 12:10 am
by maxus1998
I now used a arduino nano as cheap oscilloscope to look at what I get

Here is the screenshot of the result.
rpm.PNG
rpm.PNG (52.38 KiB) Viewed 3101 times
(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?

Re: PWM controlled fan tacho wrong

Posted: Mon Jan 06, 2020 1:30 am
by ESP_Sprite
Is this measured in the situation where your code returns faulty readings?

Re: PWM controlled fan tacho wrong

Posted: Mon Jan 06, 2020 8:24 am
by maxus1998
Yes it is

Re: PWM controlled fan tacho wrong

Posted: Mon Jan 06, 2020 2:16 pm
by maxus1998
I got a proper oscilloscope now so here are some readings

100PWM
100PWM 50mV.jpg
100PWM 50mV.jpg (13.41 MiB) Viewed 2997 times
255PWM
255PWM 50mV.jpg
255PWM 50mV.jpg (14.79 MiB) Viewed 2997 times
PWM Cable unplugged
FULL 50mV.jpg
FULL 50mV.jpg (11.07 MiB) Viewed 2997 times
The measurings have been taken with channel 1 connected to the Pulled up tacho signal (where the cable would go to the ESP32)