PWM controlled fan tacho wrong

maxus1998
Posts: 11
Joined: Tue Dec 31, 2019 4:08 pm

PWM controlled fan tacho wrong

Postby maxus1998 » 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)

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: PWM controlled fan tacho wrong

Postby ESP_Sprite » Wed Jan 01, 2020 11:29 am

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?

maxus1998
Posts: 11
Joined: Tue Dec 31, 2019 4:08 pm

Re: PWM controlled fan tacho wrong

Postby maxus1998 » Wed Jan 01, 2020 12:21 pm

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...

tommeyers
Posts: 184
Joined: Tue Apr 17, 2018 1:51 pm
Location: Santiago, Dominican Republic

Re: PWM controlled fan tacho wrong

Postby tommeyers » 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
IT Professional, Maker
Santiago, Dominican Republic

maxus1998
Posts: 11
Joined: Tue Dec 31, 2019 4:08 pm

Re: PWM controlled fan tacho wrong

Postby maxus1998 » Wed Jan 01, 2020 6:30 pm

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.

maxus1998
Posts: 11
Joined: Tue Dec 31, 2019 4:08 pm

Re: PWM controlled fan tacho wrong

Postby maxus1998 » Fri Jan 03, 2020 9:47 am

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?

maxus1998
Posts: 11
Joined: Tue Dec 31, 2019 4:08 pm

Re: PWM controlled fan tacho wrong

Postby maxus1998 » Sun Jan 05, 2020 12:10 am

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 16731 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?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: PWM controlled fan tacho wrong

Postby ESP_Sprite » Mon Jan 06, 2020 1:30 am

Is this measured in the situation where your code returns faulty readings?

maxus1998
Posts: 11
Joined: Tue Dec 31, 2019 4:08 pm

Re: PWM controlled fan tacho wrong

Postby maxus1998 » Mon Jan 06, 2020 8:24 am

Yes it is

maxus1998
Posts: 11
Joined: Tue Dec 31, 2019 4:08 pm

Re: PWM controlled fan tacho wrong

Postby maxus1998 » Mon Jan 06, 2020 2:16 pm

I got a proper oscilloscope now so here are some readings

100PWM
100PWM 50mV.jpg
100PWM 50mV.jpg (13.41 MiB) Viewed 16627 times
255PWM
255PWM 50mV.jpg
255PWM 50mV.jpg (14.79 MiB) Viewed 16627 times
PWM Cable unplugged
FULL 50mV.jpg
FULL 50mV.jpg (11.07 MiB) Viewed 16627 times
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: Glen104TMR and 88 guests