Help with the GPIO pins.


8 posts
by hop » Thu Dec 27, 2012 8:38 pm
Image

I have an external fan that I want to connect to the GPIO pins and have a go controlling the fan speed using Python. How would I go about connecting and running the fan safely? This is the only thing I have that I think I can connect to the GPIO pins, I'm not trying to fan cool my RPi. :lol:
User avatar
Posts: 87
Joined: Thu Nov 22, 2012 9:40 pm
by joan » Thu Dec 27, 2012 8:55 pm
You will need additional components. The gpios can only switch a few milliamps at 3.3V which will have no effect on your fan.

Do you have a description of the pins on your fan and what they do?
User avatar
Posts: 4211
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by hop » Thu Dec 27, 2012 9:02 pm
I assume that red = +ve and black = -ve so they must just be power cables. I thought that the GPIO could provide 5v.
User avatar
Posts: 87
Joined: Thu Nov 22, 2012 9:40 pm
by joan » Thu Dec 27, 2012 9:08 pm
The terminology used is fairly lax. The gpios only supply a few milliamps at 3.3V. There are other pins on the various headers. The main header called P1 also has pins connected to the ground, 3V3, and 5V rails. They are not gpios.
User avatar
Posts: 4211
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK
by toxibunny » Thu Dec 27, 2012 10:26 pm
that looks like a very small fan. you might be able to get away with just needing one transistor. there'd be 2 wires going into the transistor from the raspi - 1 gpio and the 5v, then one wire going from the transistor into the fan, and then one from the fan going into the ground gpio pin. I'm probably the last person you should listen to about this though...
note: I may or may not know what I'm talking about...
Posts: 999
Joined: Thu Aug 18, 2011 9:21 pm
by toxibunny » Thu Dec 27, 2012 10:29 pm
- but if you just want to experiment with the GPIO, use an LED...
note: I may or may not know what I'm talking about...
Posts: 999
Joined: Thu Aug 18, 2011 9:21 pm
by johnbeetem » Thu Dec 27, 2012 11:58 pm
There's a long thread on controlling a motor using GPIOs here: viewtopic.php?f=66&t=15688

You do have to be careful, because 5V can damage the RasPi BCM2835 SOC.

The simplest way to drive a 5V (or higher) DC motor is through an NPN transistor. Connect the GPIO through a resistor (start with 1K Ohm) to the base of the transistor and connect the emitter to ground. Connect the collector of the transistor to 5V (or whatever) through the motor. When you turn the GPIO on, it will run a small current through the transistor base-emitter junction, which will then run a 10-20X larger current through the collector, powering the motor.

When the motor turns off, a very bad thing will happen. Electrically, the motor is basically an inductor and you cannot stop the current through an inductor immediately. So when you try, the collapsing magnetic field causes a large voltage to accumulate at the transistor collector and quickly burn it out. If you're unlucky, the large voltage will also burn out your RasPi.

To prevent this from happening, you'll need a flyback diode (http://en.wikipedia.org/wiki/Flyback_diode) across the motor.

Here's another discussion about driving a relay, which has a circuit diagram with transistor, resistor, and flyback diode: viewtopic.php?f=37&t=22052 The discussion also talks about a Darlington pair, which you many need in place of a simple transistor.

The BCM2835 has a Pulse Width Modulation peripheral. You might be able to use that for variable speed.

One more caveat about GPIOs: the easiest way to destroy RasPi is to accidentally short one of the GPIO 5V pins to 3.3V. Be very careful not to do that -- if you're not using them, strip a short piece of insulation off a wire and insert it over the 5V pins.

I think this covers the most likely things that can go wrong. So enjoy!
User avatar
Posts: 938
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Coast
by Pi eater » Fri Dec 28, 2012 2:00 pm
If you're just starting to experiment with the pins I would read about it some more in the Raspberry Pi user guide.
Eating pi is sure good!
Posts: 19
Joined: Fri Dec 28, 2012 1:21 pm