Turn on/off a 12v fan with RaspberryPi
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- dalexgray
- Posts: 16
- Joined: Sun Jan 15, 2012 12:08 am
Turn on/off a 12v fan with RaspberryPi
I'm working on a project to monitor temperature and humidity in a terrarium. When the RaspberryPi senses too high a temperature (via the Adafruit HTU21D-F) I would like the RaspberryPi to turn on a fan. I thought a 120mm pc case fan would work nicely, but it runs on 12vdc. Could I use an NPN transistor to have the Pi control the fan? I made a drawing.
I have the 10k resistor to prevent the Pi from accidentally triggering the fan during start up. I'm not sure of the second resistor's value, is 2.2k right? I really don't want to fry my Pi.- adafruit_support_bill
- Posts: 88153
- Joined: Sat Feb 07, 2009 10:11 am
Re: Turn on/off a 12v fan with RaspberryPi
Resistor choice will depend on the transistor. Using a MOSFET like this, the resistors are not required. https://www.adafruit.com/product/355
You will want a kickback protection diode in there as well:
You will want a kickback protection diode in there as well:
- dalexgray
- Posts: 16
- Joined: Sun Jan 15, 2012 12:08 am
Re: Turn on/off a 12v fan with RaspberryPi
Thank you!
- dalexgray
- Posts: 16
- Joined: Sun Jan 15, 2012 12:08 am
Re: Turn on/off a 12v fan with RaspberryPi
I got my box in the mail today (yay, Adafruit fast shipping!) and hurried to set up my project.
Unfortunately, I can't get the Pi to turn on the fan. I triple checked everything, but I'm new at this so I could be making a bonehead mistake.
Here is my circuit schematic And here is a photo of the wiring I know the fan works because if I just connect its positive and negative leads to the 12v power supply it runs.
And my python script
If I remove R1 the fan turns on when the 12v power supply is connected. Besides R1 my circuit seems to be like Bill's diagram with a MOSFET instead of a NPN transistor.
What am I doing wrong?
Unfortunately, I can't get the Pi to turn on the fan. I triple checked everything, but I'm new at this so I could be making a bonehead mistake.
Here is my circuit schematic And here is a photo of the wiring I know the fan works because if I just connect its positive and negative leads to the 12v power supply it runs.
And my python script
Code: Select all
#Timed on off switch for 12vdc fan
import time
import RPi.GPIO as io
io.setmode(io.BCM)
fan_ctrl = 25
io.setup(fan_ctrl, io.OUT)
io.output(fan_ctrl, False)
while True:
print("ON")
io.output(fan_ctrl, True)
time.sleep(10);
print("OFF")
io.output(fan_ctrl, False)
time.sleep(10)
What am I doing wrong?
- zener
- Posts: 4567
- Joined: Sat Feb 21, 2009 2:38 am
Re: Turn on/off a 12v fan with RaspberryPi
The good news is that, although your schematic does not show it, you do have your pi ground and power supply ground connected, which is correct. As far as I can tell from the photo everything is connected correctly. So the first question is: Have you verified with a multimeter that D25 is in fact going high? Can you measure 3V on the gate pin of the fet? If not then it is probably the code. If you have 3V there (or so) then the fet should be turning on.
- dalexgray
- Posts: 16
- Joined: Sun Jan 15, 2012 12:08 am
Re: Turn on/off a 12v fan with RaspberryPi
Thanks Zener! Wow, one little thing makes so much difference! I actually didn't have the Pi ground and the power supply ground connected on the breadboard. My breadboard's power rails are split in the middle. I powered up the Pi and tested the voltage-about 3v from gate to drain and gate to source when the Pi switched on the MOSFET. When I connected ground from the Pi to the power supply it worked like a charm.
I don't understand why the Pi should be connected to the power supply ground. Did the 3v the Pi was trying to supply to the MOSFET have no where to go without the ground?
Thanks again, Zener.
I don't understand why the Pi should be connected to the power supply ground. Did the 3v the Pi was trying to supply to the MOSFET have no where to go without the ground?
Thanks again, Zener.
- zener
- Posts: 4567
- Joined: Sat Feb 21, 2009 2:38 am
Re: Turn on/off a 12v fan with RaspberryPi
The pi makes the 3V relative to its own ground. The fet wants to see 3V relative to ITS ground. If the grounds are not connected together then nothing happens. Think of what would happen to the space station worker who tried to loosen a bolt without having their feet attached to something. They would just spin around. Bolt wouldn't move. OK that's a rough analogy...
- dalexgray
- Posts: 16
- Joined: Sun Jan 15, 2012 12:08 am
Re: Turn on/off a 12v fan with RaspberryPi
That helps me get it, thanks!
Please be positive and constructive with your questions and comments.