K4ktus
Posts: 8
Joined: Sun Jun 30, 2019 5:22 pm

Gpio-fan overlay not working

Sun Jun 30, 2019 9:26 pm

A while ago I discovered gpio-fan overlay and started using it with my cooling fan. I was really pleased with it until one day after an update it just stopped working. Now my fan is always on, no matter what temperature CPU has. I'm using a Pi 3 B+ with fresh Raspbian Buster install, fan controlled with 2n7000 mosfet on gpio 14.
I can control the fan by manually setting that pin high or low so it's not a wiring problem. Is there any way I could fix that?
Thanks in advance.
Using Pi as a full-time desktop PC since January 2019

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12010
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 4:34 pm

deleted duplicate post, and moved old post to device tree.
Next time you want something moved flag a moderator (flag button).
duplicate posts/treads will always be deleted without warning.

do a "divide and rule", divide problem into a hardware and a software part.
replace FET with a (red) LED, and 220E series resistor, (debug LED) to check if you are still controlling the FE when using the device tree.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 4:44 pm

Anything overlay-related is best reported here, where I'm guaranteed to see it.

It's not immediately obvious what would have changed to break your fan control. Could you try with a different GPIO - 17, for example - just in case there is an interaction with the UART (which can also live on 14&15)?

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 5:15 pm

Just tried in on pin 25, and noting happening.
Temp currently 58 and the led on pin 25 is still off...
HTH
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 5:35 pm

Code: Select all

grep fan kern.log
Jul  2 18:30:43 raspberrypi kernel: [   14.261922] gpio-fan [email protected]: GPIO fan initialized
Looks like it is being initalised...
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

K4ktus
Posts: 8
Joined: Sun Jun 30, 2019 5:22 pm

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 5:40 pm

I tried with LED on both GPIO14 and 17, LED is lit solid and doesn't care about the temperature. I have it set to 65 degrees in config.txt, currently vcgencmd measure_temp gives me 56 degrees and the LED is lit.
Using Pi as a full-time desktop PC since January 2019

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 9:03 pm

Testing it on a Pi 4 it appears to be working, provided an active-low output is what you want. The GPIO starts out on, then switches off when the temperature hits the limit, remaining on until the temperature drops below (limit - 10000) at which time it switches on again.

How did it used to behave?

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 9:09 pm

PhilE wrote:
Tue Jul 02, 2019 9:03 pm
Testing it on a Pi 4 it appears to be working, provided an active-low output is what you want. The GPIO starts out on, then switches off when the temperature hits the limit, remaining on until the temperature drops below (limit - 10000) at which time it switches on again.

How did it used to behave?
Oh, everything I've seen suggests it should be an active high output. And I've not actually seen it change state at all and I've seen temp rise and fall between 45°C and 65°C.
6by9 says ...
https://www.raspberrypi.org/forums/view ... 8#p1397885

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 9:15 pm

Thanks - that should be enough to work with.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 9:27 pm

Hmm - the gpio-fan overlay declares the gpio pin like this:

Code: Select all

...
				compatible = "gpio-fan";
				gpios = <&gpio 12 1>;
...
The pin number gets overwritten by the gpiopin parameter, but the second number contains various flags. According to the dt-bindings document for the GPIO controller:

Code: Select all

- #gpio-cells : Should be two. The first cell is the pin number and the
  second cell is used to specify optional parameters:
  - bit 0 specifies polarity (0 for normal, 1 for inverted)
i.e. the overlay has declared the pin to be active low, so by understanding it is doing what it also.

The overlay source code even includes some documentation:

Code: Select all

/*
 * Overlay for the Raspberry Pi GPIO Fan @ BCM GPIO12.
 * Optional parameter "gpiopin=12"
 * Requires: 
 *	- kernel configurations: CONFIG_SENSORS_GPIO_FAN=m and CONFIG_SENSORS_PWM_FAN=m; 
 *	- kernel rebuid;
 *	- DC Fan connected to GPIO via a N-MOSFET (2N7002)
 *
 *                   ┌─────────────────────┐
 *                   │Fan negative terminal│
 *                   └┬────────────────────┘
 *                    │
 *                 │──┘
 * [GPIO12]──────┤ │<─┐  2N7002
 *                 │──┤
 *                    │
 *                   ─┴─
 *                   GND
 *
 * sudo dtc -W no-unit_address_vs_reg [email protected] -I dts -O dtb -o /boot/overlays/gpio-fan.dtbo gpio-fan.dts
 * sudo nano /boot/config.txt add "dtoverlay=gpio-fan" or "dtoverlay=gpio-fan,gpiopin=12"
 * or 
 * sudo sh -c "echo '\n# Enable PI GPIO-Fan\ndtoverlay=gpio-fan\n' >> /boot/config.txt"
 * sudo sh -c "echo '\n# Enable PI GPIO-Fan\ndtoverlay=gpio-fan,gpiopin=12\n' >> /boot/config.txt"
 *
 */

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Tue Jul 02, 2019 9:36 pm

I'm not sure I understand all of that, so what should be in config.txt ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

K4ktus
Posts: 8
Joined: Sun Jun 30, 2019 5:22 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 9:43 am

This indeed used to be active high. But if You say it's now active low, then no wonder I thought it was broken, since my fan is good enough to keep the temperature below 50 degrees no matter what, so it never hit 65 degrees mark where it would turn off. That would be enough to confirm it was active low.
I put

Code: Select all

dtoverlay=gpio-fan,gpiopin=14,temp=65000
In my config.txt
Using Pi as a full-time desktop PC since January 2019

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 2:43 pm

I can confirm that the output was active high on 4.14, but I can't yet explain why. Watch this space.

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 3:24 pm

PhilE wrote:
Wed Jul 03, 2019 2:43 pm
I can confirm that the output was active high on 4.14, but I can't yet explain why. Watch this space.
I would just like mine to be active ! SO far I've not seen any transitions either way on the selected pin.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

K4ktus
Posts: 8
Joined: Sun Jun 30, 2019 5:22 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 3:44 pm

PeterO wrote:
Wed Jul 03, 2019 3:24 pm
I would just like mine to be active ! SO far I've not seen any transitions either way on the selected pin.
Maybe try reinstalling Raspbian?
If your install was updated from a previous version (stretch updated to buster) there is a chance that something broke in the process. Some time ago when I did an update it broke vcgencmd and I had to reinstall.
Using Pi as a full-time desktop PC since January 2019

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 4:11 pm

Apologies if you have essentially done all this, but to convince me we're on the same page:

1. Edit your dtoverlay line to set the temperature limit high enough to never be normally reached and at least 10 degrees above the idle temp but within reach on an a busy system, and reboot.
2. Use "raspi-gpio get <gpiopin>", where "<gpiopin>" is your chosen pin; I see "level=1".
3. Run up a few background threads:

Code: Select all

$ while true; do true; done &
$ while true; do true; done &
4. Wait until "vcgencmd measure_temp" exceeds the limit, then repeat "raspi-gpio get <gpiopin>"; I see "level=0".
5. Stop the background threads - "kill %1 %2" (substituting the relevant job numbers).
6. Wait until "vcgencmd measure_temp" drops below (limit - 10), then repeat "raspi-gpio get <gpiopin>"; I see "level=1".

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 4:28 pm

The 4.14 driver was using the bare gpio number from Device Tree rather than requesting a descriptor, and this was losing the ACTIVE_LOW request. Since the originator was happy with this overlay on 4.14 I can only assume the "1" was an error. 4.19 interprets the flags correctly, leading to the polarity inversion.

Can you download a replacement overlay to test from here?: https://drive.google.com/file/d/1mk6rUk ... sp=sharing

Copy it into /boot/overlays and try again.

K4ktus
Posts: 8
Joined: Sun Jun 30, 2019 5:22 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 4:37 pm

Ok. I have

Code: Select all

dtoverlay=gpio-fan,gpiopin=17,temp=65000
in config.txt.
Just after it boots I type

Code: Select all

raspi-gpio get 17
into terminal and get

Code: Select all

GPIO 17: level=1 fsel=1 func=OUTPUT
now I load the Pi.
after it reached 65 degrees i run

Code: Select all

raspi-gpio get 17
and get

Code: Select all

GPIO 17: level=0 fsel=1 func=OUTPUT
after it dropped below 55 degrees I run

Code: Select all

raspi-gpio get 17
once agin and get

Code: Select all

GPIO 17: level=1 fsel=1 func=OUTPUT
Using Pi as a full-time desktop PC since January 2019

K4ktus
Posts: 8
Joined: Sun Jun 30, 2019 5:22 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 5:09 pm

I tried the overlay You posted earlier and it totally works!
Thank You very much for Your assistance!
Are You going to make it public so it's included in original Raspbian image?
Using Pi as a full-time desktop PC since January 2019

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 7:14 pm

I'm back at home now (after a day at TNMOC) so I'll try this after I've cooked and eaten.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 7:48 pm

I've pushed the updated overlay to our Linux repo, so it will be in future kernel releases.

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 8:20 pm

So can the new overlay be made active high ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 9:00 pm

The new overlay is active high - that's the only change.

User avatar
PeterO
Posts: 4869
Joined: Sun Jul 22, 2012 4:14 pm

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 9:06 pm

PhilE wrote:
Wed Jul 03, 2019 9:00 pm
The new overlay is active high - that's the only change.
This suggests its active low
4. Wait until "vcgencmd measure_temp" exceeds the limit, then repeat "raspi-gpio get <gpiopin>"; I see "level=0".
5. Stop the background threads - "kill %1 %2" (substituting the relevant job numbers).
6. Wait until "vcgencmd measure_temp" drops below (limit - 10), then repeat "raspi-gpio get <gpiopin>"; I see "level=1".
so you can see why I asked !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2279
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: Gpio-fan overlay not working

Wed Jul 03, 2019 9:09 pm

I posted those instructions before I understood the problem, then posted a link to the fixed overlay. Most stories don't make sense in reverse.

Return to “Device Tree”