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.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.