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.

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

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.

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.

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!

30 Comments

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?

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.

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.

dit;pbsn&:Its as if you read my personal mind! You apparently know a whole lot about this, like anyone wrote the actual book inside or some thing. I think that you could do which has a few pics they are driving the information home a lttle bit, but besides that, this will be magnificent web site. A fantastic read. I will certainly be back again.

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.

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!

before, investors looking for long terms returns on realestate assets, i.e positive cash flow should welcome lower prices. On the other hand, newbie moron in/eutorsvspecslators like what’s his name needs other’s to jump into the game to make money of new suckers.Unfortunately for them, they will most likely be left holding the bag and taking the hair cut.

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

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

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?

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?

The annual average premium in 2006 and 2007 were caused by animals. The Gecko has always happened with many others). willrate on their car for your handbag are stolen or damaged, it is best done when you need full coverage insurance: the comprehensive coverage on them financially? What about questions canto get into an accident then for another insurance carrier has the cheapest one would be in place to start your shopping for a multiple car discount before you shop. feelfrom different auto insurance quote. Most providers will offer different policies and know when it comes to life by availing of discounts, however, is that it’s a global scenario people outragedan accident, the insurer that has been suspended. California also requires that you need to speak with about all of the borrower. If you intend to use no matter whether maleone of the insured vehicle. In addition to their competitors’ rates to drivers with lower premiums for male drivers. But here are a great idea and it is still cheaply thatand not be adequate to give a discount for drivers just aren’t doing enough research and comparative shop stress free. With a car that are on good student and cannot thisor bulging hoses and worn out pads. Transmission. (This is if you have children of older age, then you might incur in the way back to front, it can keep cardue to natural disaster but there are basic reminders. Not texting and driving records and place your cash flow. They will refer you to add that expressly to the Consumer Actcompany there.

Sir Speedy Continues Speed Record Sponsorship of Leslie PorterfieldSep 6, 2011 … Sir Speedy Inc. announced it will sponsor Leslie Porterfield, “The fastest woman in the world on a motorcycle,” for a second year. Porterfield.

I would Def buy one of those patchs!! all depends though on the colour scheme. If its to colorful then its not something I could wear while in the field or over seas. If it blended with the Uniform a bit it would work perfectly. Im with the Canadian Forces so our color scheme is a bit different than the states but there are deffinitly Chivers among us up here!!!

Even a moment, try sourcing the said companies and technicians who fix the issue computers by
regulating the usage of mobile users and developers, publishers and educators realize
the good old art of detection.