Automating Your Raspberry Pi Fan

Publicado por

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!

Últimas noticias

Lo sentimos, pero su selección de filtros no devolvió resultados.

Hemos actualizado nuestra política de privacidad. Por favor tome un momento para revisar estos cambios. Al hacer clic en Acepto, usted está de acuerdo con la Politica de Privacidad de Arrow Electronics y sus condiciones de uso.

Nuestro sitio Web coloca cookies en su dispositivo para mejorar su experiencia y nuestro sitio. Lea más sobre las cookies que utilizamos y cómo desactivarlas aquió. Es posible que se utilicen las cookies y tecnologías de seguimiento con fines de marketing.
Al hacer clic en "Aceptar", usted está consintiendo la colocación de cookies en su dispositivo y el uso de tecnologías de seguimiento. Haga clic en "Leer más" a continuación para obtener más información e instrucciones sobre cómo desactivar las cookies y tecnologías de seguimiento. Si bien la aceptación de cookies y tecnologías de seguimiento es voluntaria, la desactivación de estos puede resultar en que el sitio web no funcione correctamente, y es posible que ciertos anuncios sean menos relevantes para usted.
Respetamos su privacidad. Lea nuestra política de privacidad aquió