# Transistor switching 3.3v with 12v logic

Discussion in 'The Projects Forum' started by ethanwhite, Jun 13, 2013.

1. ### ethanwhite Thread Starter New Member

Jun 13, 2013
6
0
Hi, I like electronics but I'm no guru

I've been trying to find a way to switch the 3.3vdc GPIO logic of my Raspberry Pi (which I got from school) when I have 12vdc present.

My project is to record the state of the 12v logic from an alarm system (see image below)

I originally thought a relay would be a good idea however I've since been reading on transistors and the transistor way seems better. I designed a circuit first with the idea that the base of the transistor would allow me to switch C and E but I'm getting more than 3.3v (which would blow up my raspberry pi) and I really don't want to do that...

Where did I go wrong the LED is just acting as a diode, I know it's the wrong way round. 12v is acting as one of the trigger outputs.

Ideally I'd like it so when I get a +12v the raspberry pi sees a +3.3v.

2. ### wayneh Expert

Sep 9, 2010
15,723
5,852
Why not just use a pair of resistors to divide down the 12V input?

Where in your diagram are you tapping the signal for your pi?

ethanwhite likes this.
3. ### ethanwhite Thread Starter New Member

Jun 13, 2013
6
0
The voltage isn't that stable, it's more like 13.52v than 12v.

+3.3v being one of the GPIO headers and ground is on the pi too.

4. ### wayneh Expert

Sep 9, 2010
15,723
5,852
You could use a zener diode to cap the output voltage, for example at 10V, and then divide that down with 2 resistors. (You'll need a 3rd resistor to limit current through the zener.)

I still don't understand where your voltage of 8.55V was measured. Does the Pi share common ground with the other two voltage sources?

One problem with your diagram is an LED on a 3.3V circuit. Some LEDs will not light with that low voltage, so the voltage at the collector of the transistor is sort of unpredictable.

5. ### Papabravo Expert

Feb 24, 2006
11,929
2,557
Use a comparator powered from 12V. It has an open collector output that can be pulled up to 3.3V. A quad part is the LM339 and the dual is LM393. Set the reference input to about 6V (for example) and you input goes to the other input. The output can be inverted by swapping the + and - inputs to the comparator. A series resistor on the input will protect against excessive currents.

ethanwhite likes this.
6. ### ethanwhite Thread Starter New Member

Jun 13, 2013
6
0
it was measured on the wire below where it says 3.3v. The software sucks so I've drawn a diagram below

http://i.imgur.com/Jb00lEd.jpg

It shouldn't say 8.55v though, it should say 3.3v and I don't know why unless it's because the ground is shared?

On the new diagram I'm unsure if I should be sharing the ground on the E side of the transistor (dotted line).

7. ### ethanwhite Thread Starter New Member

Jun 13, 2013
6
0
Thanks I've never seen a comparator before, have I understood correctly? I have a new diagram:

http://i.imgur.com/PF5MxAk.jpg

So I have a reference voltage for my raspberry pi on gpio 12 to solve the problem of a floating pin when it comes to programming, and a 3.3v into the comparator. When +12v is < 3.3v I should see a 3.3v from the output into gpio 6.

Last edited: Jun 13, 2013
8. ### absf AAC Fanatic!

Dec 29, 2010
1,905
535
The circuit should look something like the attached.....

Allen

File size:
25.2 KB
Views:
147
• ###### LM393 input low.PNG
File size:
25.2 KB
Views:
99
ethanwhite likes this.
9. ### panic mode Senior Member

Oct 10, 2011
1,651
462
if i was doing it, i'd isolate both circuits:

• ###### rPI_and_Alarm.png
File size:
6.2 KB
Views:
149
ethanwhite likes this.
10. ### wayneh Expert

Sep 9, 2010
15,723
5,852
Now that you've seen the elegant solution, here's the ugly solution I was throwing out. No isolation, but it does convert a messy 13V into a steady 3.3V.

Sorry about the drawing. My old drawing app is too old for my new laptop and I'm having to switch to a new one.

• ###### Screen Shot 2013-06-14 at 11.18.18 AM.png
File size:
18.2 KB
Views:
441
ethanwhite likes this.
11. ### panic mode Senior Member

Oct 10, 2011
1,651
462
any simulation or pcb design package will have schematic capture.
popular simulator (and free) is LTSpice.
I just used KiCad

12. ### MrChips Moderator

Oct 2, 2009
17,974
5,620
You really don't need the zener.
You can allow your 12v to fluctuate for 8v to 14v and you would still be ok.

13. ### wayneh Expert

Sep 9, 2010
15,723
5,852
Yeah, my problem is I had years of experience with drawing programs. Freehand was my favorite by far but Adobe bought it to kill it years ago. I have zero experience with any circuit software. So it's like Eric Clapton laying down his guitar to learn the tuba. Kinda tough.

14. ### panic mode Senior Member

Oct 10, 2011
1,651
462
it is not hard, just give it a try. you just drop parts and connect them, how hard is that?

15. ### ethanwhite Thread Starter New Member

Jun 13, 2013
6
0
Thanks for the info, if the optoisolator is an npn transistor controlled by an LED how do I know if I'm controlling the LED correctly? is it just:

Resistance = (12v - LED req voltage) / Forward continuous current

Maplins has a YY63T but I don't know what the LED's required voltage is. Peak reverse voltage? http://www.ebay.co.uk/itm/320894141740

16. ### wayneh Expert

Sep 9, 2010
15,723
5,852
Yes, that's pretty much it. You generally want to stay well below the rated current of any LED though, to extend its life. You just need enough light to make the coupling work reliably, and that may be only 20mA if it's rated for 30. The manufacturer should be able to supply more detailed specifications to help you make such a judgment.

17. ### wayneh Expert

Sep 9, 2010
15,723
5,852
Haha, well it's not so easy for an old dog. It'd be easier if I hadn't already learned other tricks.

18. ### ethanwhite Thread Starter New Member

Jun 13, 2013
6
0
looks like the YY63T is a ILD74 (which is why I couldn't find any info). Is the Forward voltage part what I should be looking at in the data sheet? So using a 12v supply, and a 1.3v @ 20ma is 560 ohms resistance.

Am I on the right lines?

19. ### Papabravo Expert

Feb 24, 2006
11,929
2,557
Why would that be necessary? You don't have long distances or excessive common mode voltages between the input circuit and the output circuit.

20. ### Papabravo Expert

Feb 24, 2006
11,929
2,557
Reread the secion of the b-book that talks about open-collector and open-drain outputs. It is a very useful piece of knowledge. Remember that such outputs can be wire-ore'd together.