Automating Your Raspberry Pi Fan

发布人是

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!

相关新闻文章

最新消息

Sorry, your filter selection returned no results.

请仔细阅读我们近期更改的隐私政策。当按下确认键时,您已了解并同意艾睿电子的隐私政策和用户协议。

本网站需使用cookies以改善用户您的体验并进一步改进我们的网站。此处阅读了解关于网站cookies的使用以及如何禁用cookies。网页cookies和追踪功能或許用于市场分析。当您按下同意按钮,您已经了解并同意在您的设备上接受cookies,并给予网站追踪权限。更多关于如何取消网站cookies及追踪的信息,请点击下方“阅读更多”。尽管同意启用cookies追踪与否取决用户意愿,取消网页cookies及追踪可能导致网站运作或显示异常,亦或导致相关推荐广告减少。

我们尊重您的隐私。请在此阅读我们的隐私政策。