I have two completely seperate python scripts involving the GPIO that runs on startup. The issue i'm having is that when I call the turn fan on or off it sometimes triggers the halt script I have and turns off the pi. I've tried disabling my halt script and that eliminates the issue. I'm completely lost here as to why it happens, i'd love to hear some input! :D

The function I use in my django website to trigger the relay

import RPi.GPIO as GPIO
channel = 21
GPIO.setup(channel, GPIO.OUT)

def TurnFanOff():
    GPIO.output(channel, GPIO.LOW)

def TurnFanOn():
    GPIO.output(channel, GPIO.HIGH)

My halt script triggered by a physical button

import RPi.GPIO as GPIO  
import time  
import os

GPIO.setup(3, GPIO.IN, pull_up_down = GPIO.PUD_UP)

def Action(channel):
        os.system("shutdown now -h")

GPIO.add_event_detect(3, GPIO.FALLING, callback = Action, bouncetime = 500)  
while 1:  
  • Why would anyone bother to use a script to halt when there is already a kernel function to do this?
    – Milliways
    Commented Aug 19, 2020 at 11:48
  • The first script will define 2 functions then exit i.e. do NOTHING
    – Milliways
    Commented Aug 19, 2020 at 12:07
  • @Milliways as mentioned in the question I have a button which calls the functions when pressed.
    – Baxorr
    Commented Aug 20, 2020 at 10:45
  • Also there is a kernel function to do this? How so?
    – Baxorr
    Commented Aug 20, 2020 at 10:46
  • raspberrypi.stackexchange.com/a/77918/8697
    – Milliways
    Commented Aug 20, 2020 at 12:12

1 Answer 1


All I can think of is the fan causes a power spike which temporarily drains power causing a voltage drop.

I suppose you could sort of test that by using the script without the fan connected.

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.