I have connected a fan to my raspberry pi with a transistor that sits on pin 5 ( GPIO 5 ).' I followed an online "guide" to make it.

I would like to make a script that makes it run during the day, say from 9 in the morning to 9 in the evening.

I want to use python and the GPIO library, but how would I set it up?

1 Answer 1


I'd suggest using cron to schedule the fan's on/off tasks.

Scheduling tasks can be managed by the crontab command.

shell ~> crontab -e

Add each on/off task. They would look something like:

0  9 * * * /path/to/your/python/script.py on
0 21 * * * /path/to/your/python/script.py off

The actual python script would be straight forward. Import sys to evaluate the on/off argument, and the GPIO library to work with pin 5 (refer to GPIO reference to map correct pin number).

#!/usr/bin/env python
import sys
import RPi.GPIO as GPIO

# Identify which pin controls transistor

# Set pin 4 as output

# Get what action to take
action = sys.argv.pop()

if action == "on" :
   print "Turning fan on"
elif action == "off" :
   print "Turning fan off"
else :
   print "Don't know what to do"

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.