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.

23 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.

  7. Posted 26. April 2014 at 14:47 | Permalink

    My brother suggested I would possibly like this web site.

    He was once totally right. This put up truly made my day.
    You cann’t imagine simply how a lot time I had spent for this info!
    Thanks!

  8. Posted 1. May 2014 at 18:02 | Permalink

    Thank you for some other informative web site. Where else may just I am getting that type of information written
    in such an ideal means? I’ve a project that I’m just now
    running on, and I have been at the glance out for such info.

    Feel free to surf to my webpage – real estate contracts

  9. Posted 17. June 2014 at 07:34 | Permalink

    Delta Dunarii Cazare

    My spouse and I absolutely love your blog and find
    a lot of your post’s to be what precisely I’m looking for.
    Would you offer guest writers to write content in your case?

    I wouldn’t mind writing a post or elaborating on most of
    the subjects you write concerning here. Again, awesome weblog!

  10. Posted 27. June 2014 at 15:18 | Permalink

    My spouse and I absolutely love your blog and find almost all of your post’s to be just what I’m
    looking for. Does one offer guest writers to write content for you?
    I wouldn’t mind creating a post or elaborating on a lot of the subjects you write
    regarding here. Again, awesome site!

    my blog … car accident need lawyer

  11. Posted 5. July 2014 at 15:35 | Permalink

    It’s hard to find your page in google. I found
    it on 14 spot, you should build quality backlinks ,
    it will help you to increase traffic. I know how to help you, just search in google –
    k2 seo tips

  12. Posted 26. July 2014 at 10:40 | Permalink

    I do not even know how I ended up here, but I thought this post was good.
    I do not know who you are but definitely you are going to a famous blogger if you aren’t
    already ;) Cheers!

    My web blog … Click Here

  13. Posted 31. July 2014 at 11:59 | Permalink

    Spot on with this write-up, I seriously think this web site needs far more attention. I’ll
    probably be back again to see more, thanks for the information!

  14. Posted 20. August 2014 at 01:00 | Permalink

    This is the right web site for everyone who would like to understand this topic.
    You understand so much its almost hard to argue
    with you (not that I actually will need to…HaHa).

    You definitely put a new spin on a topic which has been written about for decades.
    Wonderful stuff, just wonderful!

    My web-site – best 24 hour emergency ac service miami gardens fl

  15. Posted 24. August 2014 at 03:13 | Permalink

    I read a lot of interesting posts here. Probably you spend a lot of time writing, i know how to save you
    a lot of work, there is an online tool that creates
    readable, google friendly posts in seconds, just search in google – laranitas free content source

  16. Posted 24. August 2014 at 09:43 | Permalink

    If you would like to obtain a great deal from this piece of writing
    then you have to apply such strategies to your won webpage.

    Feel free to visit my webpage … short Haircuts for round faces women

  17. Posted 3. September 2014 at 03:08 | Permalink

    I read a lot of interesting content here.
    Probably you spend a lot of time writing, i know how to save you
    a lot of time, there is an online tool that creates high
    quality, SEO friendly articles in seconds, just type in google –
    laranitas free content

  18. Posted 6. September 2014 at 09:26 | Permalink

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You definitely know what youre talking about,
    why throw away your intelligence on just posting videos to your site
    when you could be giving us something enlightening to read?

    My blog post … emergency heating and ac repair lauderhill fl

  19. Posted 7. September 2014 at 14:05 | Permalink

    Howdy! This is kind of off topic but I need some help from an established blog.

    Is it very difficult to set up your own blog?
    I’m not very techincal but I can figure things out
    pretty quick. I’m thinking about setting up my own but I’m not
    sure where to start. Do you have any ideas or suggestions?

    Many thanks

    my blog post – 30th birthday ideas (Aisha)

  20. Posted 8. September 2014 at 03:37 | Permalink

    What a data of un-ambiguity and preserveness of precious experience on the topic
    of unpredicted feelings.

    Here is my homepage; http://Blondehairstyles.Info

  21. Johng584
    Posted 12. September 2014 at 18:38 | Permalink

    Several of these games are worth some time and are actually ebgaegefdedb

  22. Posted 18. September 2014 at 07:08 | Permalink

    Finally i quit my regular job, now i earn decent money on-line you should try too, just type
    in google – blackhand roulette system

Achtung: Wordpress interpretiert bestimmte Zeichenfolgen als Markup und verändert diese. Nutzt für Programmcode lieber Gist oder PasteBin-Services und verlinkt die Code-Schnipsel.

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>