Today, I’ll show you a small tutorial on how you can use a Raspberry Pi, a transistor and some wire to replace a PC’s on/off button. This way, it could be possible to start devices over a network, although they aren’t wake-on-lan capable.
In general, this method can be used everywhere a circuit is to be closed. For voltages above 5V however, You should use a relais to do the job.
Also, my Twitter-Followers can calm down: No, this wasn’t a doomsday device ;)
For the time being, I don’t have much component parts to play with, so I can only do articles on rather simple circuits. Nevertheless, I find it quite astonishing what You can do with a RasPi, some wire, a transistor and some spare time.
In my experimental circuit, I used the following components:
Mainboard, PSU, Fan, On/Off button from a PC-Case, IDE-Cable, BC327-16 bipolar transistor, Soldering Iron, Solder, insulating tape and – of course – the Raspberry Pi.
Certainly, You can use any device and take it apart to use a transistor and the RasPi as a simple on/off switch. As I have plenty of old PC hardware lying around here, I decided to use the Transistor as an on/off switch for an old mainboard.
But before we start to solder, we should have a look at the principles of a transistor:
You can imagine a bipolar transistor as a little switch. There are two poles, called emitter and collector, as well as a “signal” pin, the base. Based on what kind of resistor you have, the polarity of emitter and collector can be different. I use a PNP-Transistor (BC327-16).
As for any PNP transistor applies:
If the base voltage is more negative than the emitter’s voltage, current flows from emitter to collector.
It’s the other way around for NPN transistors:
If the base voltage is more negative than the collector’s voltage, current flows from collector to emitter.
Here, the technical current direction applies, so current flows from (+) to (-). The physical current direction would be from (-) to (+), but we don’t have to worry about that now. In general, you should remind the following for bipolar transistors:
NPN = Current flows from Collector (+) to Emitter (-), if the base is positive (relative to the emitter)
PNP = Current flows from Emitter (+) to Collector (-), if the base is negative (relative to the emitter)
The important characteristics of a transistor can be found in its’ datasheet, which can easily be found online. Just google for the model number. For example, here you find the BC327-16′s datasheet.
Our goal is to close the circuit between the mainboard’s positive and negative power-switch pins. On the positive pin, there is a voltage of +3.3V, the negative one is at 0V (GND). For PNP Transistors, the emitter is the positive pole, so +3.3V are applied there. The collector has a voltage of 0V.
If the base now becomes more negative that the emitter having 3.3V applied to it, the current flows from emitter to collector. As we control the base’s voltage with our Raspberry Pi, we have to switch the GPIO Pin on (+3.3V) to prevent current from flowing through the transistor. Then, the potential difference between base and emitter is 0V, no current can flow from emitter to collector, therefore the potential difference between those two is 3.3V.
Is the base’s GPIO pin is off (0V), there’s a potential difference of -3.3V between base and emitter. Therefore, current can flow from emitter to collector, whose potential difference is now (almost) 0V.
The composition is relatively simple. But as always when handling hardware:
Wrong threatment can cause irreparable damage to the hardware or you.
With some caution and insulating tape, we shouldn’t run into problems.
First, we have to solder the power-switch’s plus to the transistor’s emitter. The minus (on the mainboard, of course) goes to the collector. And the base will be connected to one of the Raspberry Pi’s GPIO pins. For this, I used a case’s power-switch cable to connect the mainboard to the transistor, and an IDE cable to connect the transistor’s base to the GPIO. This way, I didn’t have to solder something directly to the Mainboard or the RasPi. The only soldering action takes place at the transistor.
Important: Use the right polarity! Before You solder something, always use a multimeter to determine voltage and polarity.
After the soldering, our work is complete! In the next step, we hook everything up and test it.
Before you plug any of the cables into mainboard and/or Raspberry Pi, you should boot the Pi. In my case, I use a PNP transistor, so when the base is more negative than the emitter, the transistor acts as a closed switch. We don’t want this, as the Mainboard/PC would instantly be turned on when connecting the transistor to it, so first we switch the GPIO pin connected to the base to on (+3.3V). See the code fragment below to do so, if necessary.
I’ve connected the base to GPIO 25, set this to high (on), connected the transistor’s emitter and collector the the mainboard’s power switch pins, and also hooked up a fan to the mainboard’s CPU_FAN as a visual indicator to see if the PC turns on. Then, I supplied power to the mainboard’s PSU.
If everything is wired correctly, nothing should happen now. If the PC switches repeatedly on and off, this meant the transistor (we imagine as a switch) would be permanently closed. The same happens, when you permanently push your PC’s on/off button. So if this happens, make sure your GPIO pin is correctly wired up to the base and has the right voltage.
Now, the following code can be run in a terminal on the Raspberry Pi. This code is meant to work with PNP transistors, so if you have a NPN transistor, simply swap the values 0 and 1.
#become root, change directory pi@raspberrypi ~ $ sudo su root@raspberrypi:/home/pi: cd /sys/class/gpio #activate the GPIO pin 25 root@raspberrypi:/sys/class/gpio: echo "25" export root@raspberrypi:/sys/class/gpio: echo "out" > gpio25/direction #For PNP transistors: switch gpio25 on (switch open) root@raspberrypi:/sys/class/gpio: echo "1" > gpio25/value ##This would emulate a press on the case's power button #PNP: close the switch root@raspberrypi:/sys/class/gpio: echo "0" > gpio25/value # wait for 0,5 - 1sec, then open the switch again root@raspberrypi:/sys/class/gpio: echo "1" > gpio25/value #To turn the PC off, close the switch: root@raspberrypi:/sys/class/gpio: echo "0" > gpio25/value #wait, until the PC turns off. Then open the switch root@raspberrypi:/sys/class/gpio: echo "1" > gpio25/value
We emulated a press on the power button and turned on a PC. Also, we learned how to use transistors. Now we can use this example to turn on a PC, LEDs, and many other devices. You should just check your transistor’s polarity. For an NPN transistor, just switch the wires on emitter and collector, so that the NPN’s emitter is the negative pole.
Wake on Lan
Our handy solution to turn things on could be used as a wake on LAN replacement. You “just” need to control the GPIO pin over LAN (there already are solutions for this). You should however mind the security: If your Web Interface isn’t secured, everybody on your network can switch your device on and of, perhaps damaging your hardware. So be careful.
If you like this post, please leave me some feedback. Share it, leave a comment, write an email to firstname.lastname@example.org!