User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Stuck: Need switch controled by GPIO

Wed Dec 25, 2013 5:35 pm

I recently received a Pi Noir and setup a light source for it:
http://i1272.photobucket.com/albums/y39 ... f0e301.jpg

But I am having trouble figuring out how to automate the switch with a GPIO pin. From my searching, it appears I want to use a transistor. But from other searches, it looks like the examples I find are for using the transistor as a switch/amplifier. I just want the GPIO pin to turn the circuit on, not also power the LEDs.

Can someone please help or offer up a suggestion?
Last edited by DeanC on Thu Dec 26, 2013 1:48 am, edited 2 times in total.
We 'idiot proofed' the world, and now it's full of idiots!

User avatar
aTao
Posts: 1087
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Stuck: Need switch controled by GPIO

Wed Dec 25, 2013 5:46 pm

The link does not work, try again with that.
But, in the mean time....
To control a circuit/ light an LED you will either need to use a transistor to switch the power for the LED or use a relay to mimic the switch in the circuit. Since a relay would also need a transistor and a power supply (the RPi GPIO cannot drive one directly) you may as well just use a transistor.

You wouldnt really be powering the LED with your circuit, just switching the existing PSU with your transistor.
>)))'><'(((<

boyoh
Posts: 1344
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Stuck: Need switch controled by GPIO

Wed Dec 25, 2013 8:01 pm

The link is not working
It is a bit confusing at what you want to do
Be more explicit with the question
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Stuck: Need switch controled by GPIO

Thu Dec 26, 2013 1:50 am

Sorry about that, I didn't realize it was privately hosted.

http://i1272.photobucket.com/albums/y39 ... f0e301.jpg

Not at home right now, will check out relays later tonight.
We 'idiot proofed' the world, and now it's full of idiots!

User avatar
Burngate
Posts: 6067
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Stuck: Need switch controled by GPIO

Thu Dec 26, 2013 11:51 am

PNG,b60.PNG
PNG,b60.PNG (49.56 KiB) Viewed 2890 times

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Stuck: Need switch controled by GPIO

Thu Dec 26, 2013 2:49 pm

My advice is to reconfigure your array so that it can be powered from 5 volts from the RPI. You can use a power MOSFET to control the array. The following should help

http://learn.adafruit.com/rgb-led-strips/usage
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

LateDev
Posts: 44
Joined: Fri Dec 20, 2013 6:27 pm

Re: Stuck: Need switch controled by GPIO

Thu Dec 26, 2013 4:21 pm

Burngate is correct, as you can sink more current with the Pi, than source. PSU on board is limited to 50mA
To help where possible, and always to learn.

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Stuck: Need switch controled by GPIO

Thu Dec 26, 2013 5:08 pm

Burngate wrote:
PNG,b60.PNG
Ty for your help and the updated diagram. Now off to figure it out.

For others reading this post here is more information regarding what I'm doing.

The LEDs are Infrared (IR). A chain of 5 LEDs has a combined forward voltage of 6.9V.
http://canada.newark.com/webapp/wcs/sto ... reId=10196

I used 4 strings of LEDs to reduce the amount of shadowing that appears due to only a single light source. Each group of 5 LEDs are mounted near each corner of the Pi Noir.
We 'idiot proofed' the world, and now it's full of idiots!

boyoh
Posts: 1344
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Stuck: Need switch controled by GPIO

Fri Dec 27, 2013 2:50 pm

I'm at a lost on what the array of 20 led
Are playing in your project.
You are only letting 10 ma through
The led's ,Not much current for good
Brightness, You want at least 1.7v
And 20ma for each led.
Using a 9v battery is not giving you
Much tolerance
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

User avatar
Burngate
Posts: 6067
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Stuck: Need switch controled by GPIO

Fri Dec 27, 2013 6:45 pm

boyoh wrote:I'm at a lost on what the array of 20 led
Are playing in your project.
You are only letting 10 ma through
The led's ,Not much current for good
Brightness, You want at least 1.7v
And 20ma for each led.
Using a 9v battery is not giving you
Much tolerance
He's using it for IR illumination - so just on/off.
According to Farnell's data sheet, forward voltage is 1.2v typical, so each string will be 6v leaving 3v for the resistors - 64mA (which is actually above the 50mA absolute maximum, but hey, who's counting? And the 9v battery will soon lose some volts, anyway)

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Stuck: Need switch controled by GPIO

Fri Dec 27, 2013 7:31 pm

Burngate wrote:According to Farnell's data sheet, forward voltage is 1.2v typical, so each string will be 6v leaving 3v for the resistors - 64mA (which is actually above the 50mA absolute maximum, but hey, who's counting? And the 9v battery will soon lose some volts, anyway)
When I run the circuit above, a string of 5 LEDs in series measures a voltage drop of = 6v9. This leaves 2v1 used to set the current. 2v1 when using 47R should output 44.7mA.

I did the original calculations with the 9V battery when it was new. At that time, it measured 9v25. (9v25 - 6v9) / 50mA = 47R.

I'd like to add, I know shit about electronics, and have just started learning with google.

Also, I am finished with trying to figure out a transistor switch. I picked up some 2N3904 transistors, because they have a max VCEO = 40V (voltage running between collector and emitter, I only need 9V). And a max IC = 200mA (current on the collector pin?, I only need 151mA). Seemed to meet my needs. But from searching, it looks like I want to run the transistor in saturation. I read somewhere that saturation on these transistors occur when the emitter current is 1/10 the collector current. If the circuit is using 151mA, which will be on the collector, I want to apply ~15mA to the base to turn the transistor on and into saturation. With 3v3 (from GPIO), and 15mA needed, I came up with a resistor value of 220R. When I tried this, I observed 2 things... 1) the transistor didn't turn the circuit on, and 2) the transistor got real hot and started smelling funny. I'm obviously in over my head.

If anybody has any suggestions, they would be very welcome.
We 'idiot proofed' the world, and now it's full of idiots!

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Stuck: Need switch controled by GPIO

Fri Dec 27, 2013 7:57 pm

Peak current is not the same as continuous current.

Try a transistor like this:

Image
http://www.adafruit.com/products/355
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Burngate
Posts: 6067
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Stuck: Need switch controled by GPIO

Sat Dec 28, 2013 11:26 am

DeanC wrote:... I'm obviously in over my head...
I don't think so - compared to some people here!

My calculation relied on the data sheet's Typ(ical) value of 1.2V giving 6.0V for a string of 5 LEDs. Your measurement of 6.9V for a string, giving 1.38V per LED, is obviously more correct, at least for the particular LEDs you've actually got.
Of course, someone else might have a different set of LEDs with a different average voltage, so would need to do the calculation based on those.

As for the LEDs not lighting, but with the transistor getting hot, 2 things... 1) are you sure they weren't lighting? I can't see IR. and 2) this sounds like a mis-wiring somewhere. If there's current going through the transistor, it must be going somewhere, and if it's not going through the LEDs, it's going somewhere it shouldn't.

Provided it's fully saturated, so not dissipating too much power, a 2N3904 transistor should handle 200mA quite happily.
It should handle 600mW - that's ~60mA at 10V. Getting hot and smelling funny means it's taking rather more than it likes.

A 220R base resistor should give 12mA base current (3.3V - 0.7V, don't forget the Vbe) which should be enough, but you are getting close to what the GPIO can provide.
I'd be thinking about using an FET instead - particularly as Richard-TX has just pointed one out.

LateDev
Posts: 44
Joined: Fri Dec 20, 2013 6:27 pm

Re: Stuck: Need switch controled by GPIO

Sat Dec 28, 2013 3:37 pm

DeanC wrote:
Burngate wrote:According to Farnell's data sheet, forward voltage is 1.2v typical, so each string will be 6v leaving 3v for the resistors - 64mA (which is actually above the 50mA absolute maximum, but hey, who's counting? And the 9v battery will soon lose some volts, anyway)
When I run the circuit above, a string of 5 LEDs in series measures a voltage drop of = 6v9. This leaves 2v1 used to set the current. 2v1 when using 47R should output 44.7mA.
Actually no, you have forgotten VCE the Voltage drop across the drive transistor. If you allow 1V it will be all right for that transistor.
I don't see why you are wanting to drive it into saturation, given that all you need is 100mA to turn the whole matrix on.

Always work from the spec sheets given, and not from so called measurements, which can vary.

Each of the photo transistors require 20mA for a relative radiant intensity of 1 @ ~1.2V
If we allow VCE of the drive transistor = 1V this gives us a Voltage value for the resistor @ 20mA of 9V - (5 x 1.2V) + 1V = 2V
Vr = 2V @ 20mA = 100R

http://www.fairchildsemi.com/ds/2N/2N3904.pdf

Calculate a resistor for the base of the transistor for 0.65/0.7Vbe @ 2mA ~1.2KR should do

hFE of around 80 at Ic =100mA. Ib =1.2mA using 2mA will drive into saturation.
To help where possible, and always to learn.

User avatar
rpdom
Posts: 15450
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Stuck: Need switch controled by GPIO

Sat Dec 28, 2013 4:42 pm

Burngate wrote:are you sure they weren't lighting? I can't see IR
One way to check if IR LEDs are lighting up is to use a digital camera or mobile phone camera. All you need to do is turn the camera on and look at its screen. The LEDs should appear bright white if they are on. Test the camera with a TV remote first to check it doesn't fully block IR light.

User avatar
Burngate
Posts: 6067
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Stuck: Need switch controled by GPIO

Sat Dec 28, 2013 6:43 pm

LateDev wrote:Actually no, you have forgotten VCE the Voltage drop across the drive transistor. If you allow 1V it will be all right for that transistor.
...If we allow VCE of the drive transistor = 1V ...
Why?
If it's got 1V across it, and 200mA (its maximum) through it, that's 200mW wasted.
If it's fully saturated, Vce=0
hFE of around 80 at Ic =100mA. Ib =1.2mA using 2mA will drive into saturation.
HFE is down to 30 min for Ic=100mA & Vce=1V according to the spec I'm looking at. When fully saturated, hFE will be lower. 10 is a good aiming point

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Stuck: Need switch controled by GPIO

Mon Dec 30, 2013 6:59 am

Ok, I have a MOSFET on order that is better for a 3V3 input, but in the meantime I'm trying to use a MOSFET with a threshold voltage of 2 - 4V. Needing a GPIO pin to control the circuit, I am trying to use a transistor to turn on the 5V from the RPi to turn on the MOSFET.

I feel I'm way over my head, and it's just a matter of time before I lose one of my RPis. If someone could look over the schematics and advise whether this is a good idea, or if it might let the smoke out of the RPi. Is tying all the grounds together a wise thing?
Attachments
latest.png
latest.png (37.05 KiB) Viewed 2529 times
We 'idiot proofed' the world, and now it's full of idiots!

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Stuck: Need switch controled by GPIO

Mon Dec 30, 2013 3:46 pm

there is one error in your schematic. move R2 so that it connects to 5V and connect the collector directly to the gate.
when the npn is off, the gate is pulled to 5V by R2 and the LEDs are lit.
to turn off the LEDs, set the GPIO high.

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Stuck: Need switch controled by GPIO

Mon Dec 30, 2013 5:18 pm

rpdom wrote:
Burngate wrote:are you sure they weren't lighting? I can't see IR
One way to check if IR LEDs are lighting up is to use a digital camera or mobile phone camera. All you need to do is turn the camera on and look at its screen. The LEDs should appear bright white if they are on. Test the camera with a TV remote first to check it doesn't fully block IR light.
Actually, all I'm doing is swapping out one of the IR LEDs for a normal red one. If it comes on, the rest are on.

Tage wrote:there is one error in your schematic. move R2 so that it connects to 5V and connect the collector directly to the gate.
when the npn is off, the gate is pulled to 5V by R2 and the LEDs are lit.
to turn off the LEDs, set the GPIO high.
But won't there be a voltage drop across R2, which will reduce the 5V?

I think I'll go and get a logic gate. They take a range of inputs and output 5V, which should safely turn the MOSFET on, as well as protect my RPi.
We 'idiot proofed' the world, and now it's full of idiots!

User avatar
rpdom
Posts: 15450
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Stuck: Need switch controled by GPIO

Mon Dec 30, 2013 7:30 pm

DeanC wrote:
Tage wrote:there is one error in your schematic. move R2 so that it connects to 5V and connect the collector directly to the gate.
when the npn is off, the gate is pulled to 5V by R2 and the LEDs are lit.
to turn off the LEDs, set the GPIO high.
But won't there be a voltage drop across R2, which will reduce the 5V?
But the gate of Q2 is connected directly to 5V so it doesn't matter what Q1 does.

If the resistor is between 5V and the gate, the gate will be pulled towards 5V when Q1 is off, and pulled to 0V when Q1 is on. The gate should be directly connected to the collector of Q1 (the other end of R2).

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Stuck: Need switch controled by GPIO

Mon Dec 30, 2013 11:38 pm

Thanks for everyone's help. I finally figured out what was wrong with the last diagram (beside R2 being on the wrong side of the Gate).

When I was reading about MOSFETs, it only speaks of needing voltage at the Gate, and never mentions about current. I figured instead of wasting unneeded current, I could set the Collector current at only 1mA because it only needs the 5V. This was wrong, and so it wasn't working.

After raising the Collector current to 5mA, all is well. I also added a 10K resistor from the Gate to ground to stop it from floating. The finalized diagram is included below.
Attachments
Drawing1.png
Drawing1.png (37.75 KiB) Viewed 2410 times
We 'idiot proofed' the world, and now it's full of idiots!

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Stuck: Need switch controled by GPIO

Tue Dec 31, 2013 2:11 am

there is no need to connect a resistor across the npn if you have the pullup resistor to 5V. the gate will not be floating.it is connected to the 5V supply through the pullup. when the 5V supply goes down it brings the gate voltage with it.

you could have any resistor value for the pullup resistor (from 5V to gate). 1 milliohm to 1megaohm. as long as you make sure that the npn can drive enough current to pull the gate below the threshold voltage.

have fun!

you could of course get a logic gate that can function as a level translator form 3.3V input signal to 5V gate drive signal, or a MOSFET gate drive that can function with the 3.3V levels from the GPIO, and stop worrying about npn transistors and resistors.

LateDev
Posts: 44
Joined: Fri Dec 20, 2013 6:27 pm

Re: Stuck: Need switch controled by GPIO

Tue Dec 31, 2013 3:01 am

Burngate wrote:
LateDev wrote:Actually no, you have forgotten VCE the Voltage drop across the drive transistor. If you allow 1V it will be all right for that transistor.
...If we allow VCE of the drive transistor = 1V ...
Why?
If it's got 1V across it, and 200mA (its maximum) through it, that's 200mW wasted.
If it's fully saturated, Vce=0
Sadly Vce will never = 0v so there is always a power drop. As current increases Vce (sat) increases.
This is an amplifier transistor, not a switching transistor, so I would expect it to go into thermal breakdown at some point, particularly when you try to use it in a DC on and off situation.


.
Last edited by LateDev on Tue Dec 31, 2013 3:45 am, edited 2 times in total.
To help where possible, and always to learn.

LateDev
Posts: 44
Joined: Fri Dec 20, 2013 6:27 pm

Re: Stuck: Need switch controled by GPIO

Tue Dec 31, 2013 3:24 am

DeanC wrote:Thanks for everyone's help. I finally figured out what was wrong with the last diagram (beside R2 being on the wrong side of the Gate).

When I was reading about MOSFETs, it only speaks of needing voltage at the Gate, and never mentions about current. I figured instead of wasting unneeded current, I could set the Collector current at only 1mA because it only needs the 5V. This was wrong, and so it wasn't working.

After raising the Collector current to 5mA, all is well. I also added a 10K resistor from the Gate to ground to stop it from floating. The finalized diagram is included below.
1/ Why are you actually using a transistor ?
2/ I tend to think the FET being used is overkill, but it will certainly handle the current.
http://www.irf.com/product-info/datashe ... rfz44n.pdf

Just 2 points to remember.
A transistor is a Current Device.
A FET is a Voltage device.
To help where possible, and always to learn.

User avatar
DeanC
Posts: 136
Joined: Thu Sep 26, 2013 4:07 pm
Location: Vancouver, Canada

Re: Stuck: Need switch controled by GPIO

Tue Dec 31, 2013 3:50 am

Tage wrote:you could of course get a logic gate that can function as a level translator form 3.3V input signal to 5V gate drive signal, or a MOSFET gate drive that can function with the 3.3V levels from the GPIO, and stop worrying about npn transistors and resistors.
The logic gate was my next go to. But this worked out. I have a MOSFET on order that will have a VGS(th) that will be below 2V. This was just so that I could play around with it. But found out that I needed 5V to turn this one on, hence the transistor.

LateDev wrote: 1/ Why are you actually using a transistor ?
2/ I tend to think the FET being used is overkill, but it will certainly handle the current.
http://www.irf.com/product-info/datashe ... rfz44n.pdf
Yes, it is sort of overkill. Until you realize that this circuit can have the IR LEDs circuit removed. Then you can take an adapter from an AC outlet (assuming it is after the transformer) and hook it up to that MOSFET. Because of the MOSFETs crazy voltage and amp ratings, pretty much anything in my house can be control by a GPIO pin.

There is a lot of cool shit you can do once you start understanding some basic electronic principles.
We 'idiot proofed' the world, and now it's full of idiots!

Return to “Beginners”