Be cool part 2 - Writing the Python Fan control program

Be cool part 2 - Writing the Python Fan control program

So initially we will be using Raspbian and the Python IDLE programming environment. More experienced users will say there are other ways to do this, but if you are learning from the beginning you will understand and develop your knowledge rather than blindly copying and pasting. I have been programming for 40 years in different languages and doing things the right way helps a lot.

I will assume that by now you know how to enter the terminal and find your way around the Raspbian desktop.

Our finished fan controller prototype


Here is our to do list-

We will start on the PI using Raspbian, then we will setup the Python program.
Install Python 2 and 3 (because some projects are written for Python 2),
Install GPIO (general purpose input - output)

sudo apt-get update
sudo apt-get install python
sudo apt-get install python3
sudo apt-get install python-dev python-rpi.gpio

I will show you how to make two versions -

1- Raspian to prove concept and show Python programming.
2 - OSMC/OpenElec to use in the background when playing media.

of course you may not need both versions, that is your choice.

Once we have tested the program and fan controller hardware, then we will set the script to run automatically on startup.


Here is my parts list for the first build

This is the prototype and proof of concept as rarely do things ever work first time. This will work for Raspian or OSMC versions with no hardware changes at all.

Parts needed -
1x 5v Fan (not a PC fan) with mounting screws to suit your Raspberry PI and its case

All the other components are taken from an Electronics Starter kit -
http://www.mallinson-electrical.com/electronic-kits/electronic-project-starter-kit.htm of course you may have other components that you can use, but this is aimed at novices.

1x Prototyping breadboard - this is so you can push wires in rather than solder.
1x NPN transistor ideally a BC337 or BC338, (I used a BC547 from the kit as substitute initially but it is underrated @100mA, it should be at least 200mA).
1x 1K ohm resistor [brown black red]
3x wires with male and female pins (from the rainbow wires above)
2x wires to extend the fan connector (from the packet below the rainbow connector)

If you want an LED you will also need these. 
I advise beginners to do this and leave the fan disconnected until you know it all works. There is less chance of damage driving an LED through a resistor.

1x 100 ohm resistor [brown black brown]
1x Green LED, I chose green but its colour is not that important.
Electronics Starter Kit
What we need to do is use the PI GPIO pin to supply a 3.3v Logic 1, a small current to switch on a transistor which will amplify the tiny current (2.7mA here) and use that to turn on the transistor which can supply around 200mA to the fan at 5v.

Circuit Diagram - remember this shows the electrical connections, not the physical layout though they can be similar for clarity.




(yes LED1 does connect to R2 if you want it to work, if you do not want an LED leave out LED1 and R2 or remove them after testing)

You can put resistors in either way round, all other components must be oriented as shown, especially the flat on transistor Q1.
The suggested breadboard layout

First identify which way the connections go on your breadboard, most have lines one way around the outside, and the opposite way in the central area. We just need an area where they go one way.

Note orientation of strips
Take your time and check, check and check before applying power. It is a good idea to leave the fan disconnected and check with just the LED as much less power is involved. Note R1 has the brown/black/red bands, R2 has brown/black/brown bands. I pride myself on learning the resistor colour code before I left school!

Assembled breadboard
Testing

Even before we write the program a quick test will happen when you power up the PI, the LED will light and the fan should spin (if connected) for about a second as the rainbow screen is shown. You may get a second burst when Raspbian starts. This is the PI resetting it's GPIO ports (in hardware, then software) and it tells us that the circuit is likely working. It should then shut off. If the fan does not shut off or LED go out after a second or two, there is a problem. Switch off and check everything.

Transistor Q1 may get warm/hot when the fan is on, this is normal but if it smells of frying silicon there is a problem! (It is a very distinctive smell.) Don't forget, the Power Supply can provide 2.5 Amps to the path of least resistance, if there is a short circuit, that is where it'll go. Even 5v @2.5amps can do real damage!

We have lift off!
The second build - (preview) this will be a more permanent one using a stripboard PCB and soldering the components in to make it fixed and prevent shorts occurring.

Stripboard from Maplin £7
All you need extra is a soldering iron, stand and solder, and a stripboard or VeroBoard as it was in my day. Bigger boards work out cheaper over a number of projects and are more versatile for ideal layouts.

Sneak preview of finished product!
How neat is that?