Automating Your Raspberry Pi Fan

Veröffentlicht von

If you're looking for a way to cool down your Raspberry Pi to prevent overheating and damage, you have a wide variety of options. Some methods can keep the board's components at a reasonable temperature without active cooling, while others require a fan to dissipate heat properly. If you choose a fan method, you may need to draw power from the Pi itself, either through the 5V or even 3.3V rail.

While effective, a constantly running fan—even when the Pi is happily idle—isn't very energy efficient. Additionally, you're putting unnecessary wear on the fan. With its built-in GPIO pins and processor temperature sensing, adding an "on-when-hot feature" to your Pi fan setup is a reasonable next step. 

Let's explore the most efficient solution, which includes adding a transistor to power the fan, instead of the Pi's output. Consider adding a diode in parallel with the motor to prevent excess current to the transistor after you shut it off as well.

What You'll Need

Here are the components I chose, though you can substitute similar devices that you have on hand in your own build. When implemented in an actual cooling system, you'll likely forgo the breadboard.

- Fan: 5V
- Transistor: 2N2222A
- Diode: FR207
- Resistor: 1000 ohm
- Breadboard, breadboard adapter, and hookup wire
- Raspberry Pi

Follow these steps to assemble your build:

1. Hook the components up as shown in the first image on this page, with GPIO pin 19 connected to the transistor base via the resistor. 
2. The positive 5V bus attaches to one fan connection, while the other fan lead goes to the transistor's collector pin. 
3. Connect the transistor's emitter to ground and run the diode from the positive bus to the negative leg of the fan. 
4. This diode blocks the direction of normal current flow, but allows the motor to ramp down slowly to prevent undue stress on the transistor.

I also added an LED light with a resistor to verify that the output was operating correctly, which is useful for troubleshooting, but otherwise unnecessary.

Basic Fan Control with Python

Once you complete the connections, you'll need to program the Pi to operate the fan. 

1. From the Pi terminal, open the Pi text editor by entering nano fan.py. 
2. Input the code in the image below, then press ctrl x, y, and enter to exit the program and save. 
3. From the command prompt, enter python fan.py. This step will turn your fan on for two seconds, then turn it off.


Image: Jeremy S. Cook

Automating Raspberry Pi Fan Control 

Now you're ready to write the automatic fan control routine that suits your application best. One popular option is to have it react to the Pi's processor temperature, which you can access via the terminal command: vcgencmd measure_temp. 

Add this command to a Python script set to run at startup, and you'll have a fan that doesn't just spin mindlessly, but is instead controlled by the Pi that it's cooling. Review the procedure for setting up a temperature-reactive system (written for the Pi 4), which lets you create your own script to turn the fan on or control its speed via PWM.

Does Direct Control with GPIO Work?

I went ahead and tested powering the fan with a GPIO pin. I expected it to work without issue, even if it strained the system. Instead, it didn't work at all. You can see my setup in the image below.


Image: Jeremy S. Cook
Caption: Don't try this at home!

Neue Beiträge

Leider ergab Ihre Suche kein Ergebnis

Aktuelles über Elektronikkomponenten­

Wir haben unsere Datenschutzbestimmungen aktualisiert. Bitte nehmen Sie sich einen Moment Zeit, diese Änderungen zu überprüfen. Mit einem Klick auf "Ich stimme zu", stimmen Sie den Datenschutz- und Nutzungsbedingungen von Arrow Electronics zu.

Wir verwenden Cookies, um den Anwendernutzen zu vergrößern und unsere Webseite zu optimieren. Mehr über Cookies und wie man sie abschaltet finden Sie hier. Cookies und tracking Technologien können für Marketingzwecke verwendet werden.
Durch Klicken von „RICHTLINIEN AKZEPTIEREN“ stimmen Sie der Verwendung von Cookies auf Ihrem Endgerät und der Verwendung von tracking Technologien zu. Klicken Sie auf „MEHR INFORMATIONEN“ unten für mehr Informationen und Anleitungen wie man Cookies und tracking Technologien abschaltet. Das Akzeptieren von Cookies und tracking Technologien ist zwar freiwillig, das Blockieren kann aber eine korrekte Ausführung unserer Website verhindern, und bestimmte Werbung könnte für Sie weniger relevant sein.
Ihr Datenschutz ist uns wichtig. Lesen Sie mehr über unsere Datenschutzrichtlinien hier.