I'm new to the world of Raspberry Pi, I'm setting one up as a little PC for a kid. I have a RPi 4 running Raspberry Pi OS, and I've build a case around this heatsink. I have created a simple circuit to control the fans from a GPIO using a MOSFET.

Schematic for fan control, motor represents fans

I am using a python script as a service to control the fans when booted. This seems to be working fine, I'll include it for reference in case it's important:

import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO
import logging

gpioPin = 2 # The GPIO connected to fan circuit
onTemp = 40 # The temperature in Celsius after which the fan is turned on
offTemp = 35 # The temperature in Celsius after which the fan is turned off
status = False # Cooling status, true when the fan is in a cooling cycle, false when it's not

logging.basicConfig(filename='heatsink-fan.log', encoding='utf-8', format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)

def setup():
    GPIO.setup(gpioPin, GPIO.OUT, initial=0)

def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    temp =(res.replace("temp=","").replace("'C\n",""))
    return temp

def fanON():
    logging.debug('Fan turned on')

def fanOFF():
    logging.debug('Fan turned off')

def getTEMP():
    CPU_temp = float(getCPUtemperature())
    if status is False:
        if CPU_temp > onTemp: fanON()
        if CPU_temp <= offTemp: fanOFF()

def setPin(mode):
    global status
    GPIO.output(gpioPin, mode)
    status = mode

    logging.info('Fan script started.')
    while True:
        sleep(10) # Read the temperature every 10 seconds
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
    GPIO.cleanup() # resets all GPIO ports

The real purpose of the circuit was to turn the fans off when the Pi is shutdown/halted so they aren't running 24/7. The power supply will always be connected. However, for some reason the GPIOs are being driven high even when shutdown.

I have tried using dtoverlay=gpio-poweroff in config.txt which I now understand is not the correct thing to do.

I have tried creating a systemd service (following this) to run at shutdown, which briefly set the GPIOs low but then when it fully shut down they went high again.

Why does the Pi drive pins high when halted? Is there a way to overcome this or do I have to make a new circuit to be driven by active low?


1 Answer 1


GPIO 2 and GPIO 3 have hard-wired external pulls of 1k8 to 3V3.

Those pins will read high while the Pi is powered (unless they are driven low by software or an external circuit).

  • I know about GPIO 3 (which I'm using with a power button), I did not realize GPIO 2 had that too! So if I swap to another GPIO it should work? I'll give that a shot thanks!
    – shane-pi
    Oct 11, 2020 at 19:18
  • 1
    Thank you! This solved my problem, I simply swapped it to GPIO 17 and updated my script and now it's working. That 1K8 resistor perfectly explains why I saw a 2.7V signal into my MOSFET.
    – shane-pi
    Oct 11, 2020 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.