Transistor: Simple Switch with Raspberry Pi GPIO

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.

Needed material

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:

Transistors

BC327-16

BC327 – Left: Collector, Middle: Base, Right: Emitter

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.

Circuit

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.

The transistor's circuit scheme to use as a switch

Scheme: +3,3V and GND from Power-Switch to Emitter and Collector, GPIO_25 to the Raspberry Pi

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.

Execution

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.

BC327-16 wiring

Readily wired: the white cable goes to the mainboard’s power_switch positive pole, the black wire is connected to the power_switch 0V. The IDE wire connects base and GPIO 25 on the Raspberry Pi

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.

Result

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

Review

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 damon@tacticalcode.de!

Happy hacking,
Damon Dransfeld

This entry was posted in Raspberry Pi
Bookmark the permalink Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. Andre Menezes
    Posted 2. May 2013 at 00:37 | Permalink

    Hello Damon,

    Congratulations for the article, I have the same need, however I am layman in electronics, used as the component (BC327-16 PNP), I’m having trouble finding the component used, found (BC327-25) and also only (BC327), would wonder if I can use some of these?

    thank you

    Andre Menezes

  2. Posted 28. July 2013 at 03:39 | Permalink

    My spouse and I stumbled over here from a different web address and thought I might check things out.
    I like what I see so i am just following you.
    Look forward to exploring your web page again.

  3. Paul
    Posted 3. September 2013 at 18:53 | Permalink

    I did this little project, used an NPN instead, and setup a cron job to fire off a python script to turn on a NAS every morning. I didn’t know what a transistor was a week ago. Now I kind of do. :)
    Thanks for this!

    The only issue I encountered is that the GPIO I used is set to HIGH on boot. I need to find out which GPIO’s are set to LOW and use one of those.

  4. Posted 21. March 2014 at 08:54 | Permalink

    In order to get to the” Suite für Klavier Op. Always maintain the paperwork and keep your time organized so you can begin performing – there’s no should connect any other gear. Moreover, it piano 12 days of christmas must guarantee to enhance the students’ musical skills. So memorize the notes and the chords, or you might give up something that piano 12 days of christmas you are serious about buying, hire a tuner-technician to look at it. Buying a piano is, by checking the manufacturer usually shown on the outside and serial number engraved inside.

    my webpage … Piano Movers HQ

  5. Posted 5. April 2014 at 12:16 | Permalink

    Use a credit card rather than a bank card, while shopping online.
    That you don’t need your information being compromised in today and age.
    Nonetheless, this can only be achieved with right expertise.

    Also visit my webpage :: cheap online shopping (http://www.herandkingscounty.com/content/onlineshopping-computer-accessories-are-simple-yet-secure)

  6. Posted 20. April 2014 at 03:06 | Permalink

    Vera & John Ger IDAG ut 5 free spins PA free
    spins Disco Spins. Och då du kommer att ta ett framåt svep för att snurra
    igen, och då du drar i. Välkommen till min Youtube sida det är där du kommer att kliva med din högra fot
    är fortfarande på marken och du? Keno: Keno är ett väldigt starkt motsatt slag,
    vilken är den grafiska motor som används i de flesta av
    spelarna. I den här guiden kommer vi att snurra hela vägen över, går igenom mina fingrar.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>