Why did you choose RPI

Raspberry Pi fan control

You need the following parts for the instructions

Thank you for choosing a youth programming kit. Programs are everywhere, visible and invisible. As a programmer you understand the modern world and have the power to make it a little better. Have fun with this practice-oriented Python3 hardware programming tutorial, wishes the team of Jugend Programmiert.

In this guide we will show you how to connect and control one fan and three status LEDs to the Pi. With the LEDs we show how warm your Pi is. If it gets too hot, we turn on the fan. When the Pi is cold we turn the fan off again. We use Pyrthon3 as a programming language. You can get started with Python3 in our starter kit.

Before the start: You need a Raspberry Pi with Raspbian installed. You can get all information about installing, updating, upgrading and setting up your system in the Raspberry Pi 3 Starter Guide from the Pi3 Starter Set (link)

What we do: In this tutorial we show the basics of Python3 when controlling GPIO pins. In the Raspberry Pi temperature control kit, you will find the hardware you need. We explain step by step how you program and set up your control system. At the beginning we program the reading of the CPU temperature of your Raspberry Pi. Then we define three temperature ranges. We use LEDs to indicate in which temperature range your Pi is currently. If the Pi gets too warm, we turn on the fan.

1: Read out temperature

In this part we read the temperature of the Pi and compare it with three values ​​for normal, warm and hot. We output a message how warm the Pi is and what temperature range that is. You don't need hardware for this, we'll use it in the second part.

Open a terminal or log into your Pi's console via SSH.

First we want to read out the temperature. The temperature of your CPU is in the file. Reading out and outputting the temperature is possible e.g. with the command The result is without , shown but with 3 decimal places. For example; that's 45.622 degrees Celsius.

Now we know in which file the CPU temperature is saved. We want to read this file with Python.

Our program is called Temperatur.py and should be in the directory. Create or open a new file with the command If file already exists, it will be opened, if not, the Pi will create a new empty file. Save the file with. you close the file. You can also use another editor if you don't like nano.

We write a comment in the first line. Comments always begin with and are ignored when the program is executed. In the first line write:

It is a good idea to add comments to your program. Then you will know later what your program is doing at this point. We start with the first part, reading the temperature and the output in the console.

First we save the path of the file to be opened in a variable. Then we open the file with Python. This is done in line 2. That stands for the English word read. We just want to read the file. In the third line we read the first line of our file and save the value in a variable with the name. This is the number of characters we want to read. Without the 2, the whole line is read, including the invisible line break at the end. You can just try that out. With we close the file, we no longer need it. We have just written the value, the temperature, to the variable. The 5th line shows our temperature. So that it looks nicer, we have written some text in front of and behind the temperature.

So that this first part doesn't overwhelm you, we'll summarize the whole thing again.

We open a file. Read the first line from this and write the content of this line to a variable. We close the file. Then we output a text in the console, then the variable and some more text.

Now we're starting your program. You do that with the command It should give you this sentence:

Your CPU is 45 degrees

2. Set temperatures

Now we know how warm the Pi is. Depending on the temperature, we want to turn the fan on or off and light up one or more LEDs.

We have to tell our program what temperature is normal, warm or hot. To do this, open your program again with nano and write the following lines in.

You can freely adjust these values, make sure that normal is colder than warm and warm is colder than hot. In Python, values ​​are stored in variables. These differentiate between text (string) and numbers (integer). There are variables that contain things like "I am a text" and others that contain numbers, for example 5132. Our temperatures are numbers, i.e. integers. We get a text from the CPU temperature file. (String). Fortunately, the string is a number. We convert the string into an integer with the lines:

Now let's compare the temperature with our values:

An If query compares two values ​​and executes commands if they apply or not. In the first line we say if temperature <= normal output the following line. If the temperature is not lower, the command will not be executed. In the next line we ask whether the temperature is> (greater) than warm and if this is the case we execute the next command. The last query looks at whether the temperature is higher or the same and executes a command or not. Make sure that the code to be executed must be indented with SPACE (space bar) or tab. We'll list all the comparison operators again. Python can not only be smaller, larger, etc. but also (is the same) and is not the same.

Compare in Python
less than<
greater than>
smaller or same as<=
greater or less than>=
equal==
not equal!=

Now we already have a working temperature display. Carry out your program with you. In the next part we bring the GPIOs into play and let the LEDs light up.

3: LED structure

In this part we show you how to use the GPIO pins of the Raspberry Pi. With these we light up our LEDs and control the fan. On the picture you can see the structure. Turn off your Pi and use the supplied parts to build the structure. Instead of the fan, we first use an LED. You should always connect the LEDs via a resistor, otherwise they can break. The long leg of the LED is the anode (+) and the short leg is the cathode (-). The structure is always pin, resistor, LED, and then to GND (ground or minus)

Pin code
2Resistance 56 ohmsGreen LED long leg
3Resistance 56 ohmsYellow LED long leg
4Resistance 56 ohmsRed LED long leg
17Resistance 56 ohmsLED long leg
GNDYou connect with minus on the breadboard

You connect the short legs of the LED directly to ground (minus) on the breadboard. Great now we want to light up the LEDs. Start your Pi.

4: Fan and LEDs via controls

So that we can control the GPIOs with Python, we need the GPIO library. We include this at the beginning of our script. We also import the library. This enables us to access commands such as what makes our program sleep for 10 seconds.

Now we define the pins that we want to use. We need a pin for each signal. We use 3 LEDs. Green, yellow, red and the fan so we need 4 pins. As retaining + and - . The GPIO pins can be used as input or output. Inputs are for signals e.g. from temperature sensors. With exits you can supply things with energy. We need outputs for our LEDs and the fan. There are several ways to count the 40th pins on the Pi. We use BCM. This is the assignment on the red reference board from the starter kit and on the small card that is in the set. We define how we address the pins with the line. Now we have to determine which pins we want to use. We have to activate each pin individually. We use 2, 3, 4 and 17 as outputs.

The program now knows that we are using these pins as an output. An output can have two states. On (high) or off (low). We'll first turn off all the pins.

With these lines we switch the GPIO.output pins with the numbers 2,3,4,17 to LOW, i.e. to off. You can also set the pins to HIGH, the code then looks like this:

Make sure your LEDs are connected properly and turn them on and off. Between the on and off you can insert breaks, without a break the LED switches so quickly that you cannot see it.

Breaks are made with the command for a 2 second break. At the end of your program you should insert the line to avoid mistakes. The GPIO pin assignment is canceled again. You can test this in an extra script, it looks like this:

All LEDs should light up for 10 seconds and then go out again. Can you make the LEDs light up one after the other? Try it.

5. Connect the fan.

If your LEDs are working, it's time to turn your fan on.

Your LEDs get 3.3V via the GPIO pins. Unfortunately, that's not enough to operate the fan directly. That's why you will also find a transistor (BC337) in the kit. A transistor has three legs and works like a switch. The leg in the middle is the switch (base). With this you switch the connection between the other two legs (collector & emitter). If you connect the base to a GPIO pin and set it to HIGH (on), the current flows between the other two legs. If the pin is LOW (off), the connection is interrupted. The order is important! The current must flow from the collector to the emitter. You connect the plus line from the fan to the Pi at 3.3V. You connect the minus line to the collector. The emitter is connected to the Gnd (Ground) of the Pi. You connect the base pin to your GPIO pin. In our example pin 17.


The fan from your kit is a 5V fan. It works with 5V and 3.3V. You can also connect it to 5V, but then it is a little louder. We therefore recommend connecting it to 3.3V. If everything works, we'll move on to the next step.

6: GPIO and temperature

We connect the query of the temperature and your IF loops with the GPIO control. To do this, open the temperature.py file again. At the beginning we import our GPIO and time library again and define our 4 pins. First we switch the 4 pins to low (off).

Now let's go to the part with the IF loop. We expand this with the code to switch on the LED.

Below the issue with print let's add the line so that the green LED lights up when the temperature is less than or equal to the value for normal.

We repeat that with the value for warm. If the temperature is higher than warm, we turn on the green and yellow LEDs.

Finally, we compare the temperature with the value hot. If the temperature is higher than hot, we activate the red LED and our fan.

So that the LEDs and the fan do not go out again immediately, we include a break. makes our program wait a minute. In this minute the fan turns if the Pi was too hot and the LEDs light up.

In the end we clean up with:

Your program can now call up the current CPU temperature of your Pi, compare it with your limit values ​​for temperature, turn LEDs and the fan on and off. In the end the program runs out.

You can restart the program to query the temperature and activate the fan. It would be much cooler if the program runs automatically in the background.

For that we need a loop. Loops run until something arrives. A while loop can be translated as “while loop”. As long as the sun is shining, keep the windows open. But when the sun is no longer shining, then close the window.

Or, as long as the time is not 9:00 a.m., don't activate the music. A similar loop runs in your alarm clock.

But there are also loops that simply run to infinity. We need just such a loop. It looks like this:

At the beginning of your program we integrate the libraries and assign the GPIO pins. We don't need to repeat that every time. We want to repeat the reading in of the temperature file in each run. Otherwise we have no current value. That's why we add the loop in front of it.

Everything after While 1: will be executed again and again until you stop your program. That goes with. Just like with IF queries, we have to indent the code in the while loop with TAB or Space. So everything that should be in the loop has to be indented. Also the content of the IF loops! This must now be indented twice.

Start your program with It now gives you the current temperature every minute. But it would be cooler if the program started automatically in the background, we'll do that in the next step.

7: Start automatically

You can also start your program automatically when your Pi starts. This is what the so-called cron job is for. A cronjob starts a program e.g. every 5 minutes or once a week, every year at Christmas, etc. You can set that.

You can create cron jobs using the cron manager. What you do with At the first start you will be asked for the editor. The best thing to do is to use nano, No. 2. In the crontab, you can store commands that your computer will execute at a specified time. Our program should be executed when it starts, so we specify the point in time:

followed by the command. Note that you have to work with whole paths for cronjobs. The cron job has to look like this

Save your file and exit the editor. You can now restart your Pi. Your program will now start in the background. You can see from the LEDs that something is happening. The outputs in the terminal no longer appeared. That's why we can make our script a little leaner. All issues with print you can remove it, nobody can see it anymore. We commented you out here with #.

Your finished program should look like this.

Congratulations.

You have built your own Raspberry Pi fan controller. You can use your structure of the breadboard to integrate the control in a housing or in the supplied frame,

If you want, you can also insert the temperature data directly into a Google Spreadcheet. Or write you an email if your Pi is too hot. You can find this and many other cool projects on our homepage. We'll be happy to help you with problems and give you tips if something doesn't work. On our discussion platform you can exchange ideas with other users and present your program and project. We are looking forward to your visit.

Goodbye

We hope you enjoyed this project! If you want to experience even more, you will find numerous other kits and handicraft projects for your Raspberry Pi on CodingWorld.io

Any questions or feedback?