Arduino Forum

Using Arduino => General Electronics => Topic started by: hfp777 on Jun 03, 2012, 01:10 am

Title: Transistor, PWM and LEDs
Post by: hfp777 on Jun 03, 2012, 01:10 am
--edit: see http://arduino.cc/forum/index.php/topic,108403.msg814896.html#msg814896 for up to date info

Hi all,

I'm doing some experiments to better understand how to use transistors as amplifiers in future projects.

Right now, I have this:
(http://i.imgur.com/aSIqBl.png) (http://imgur.com/aSIqB)

But with this setup, PWM works in a very strange way.
Code: [Select]
analogWrite(ledPin, 0);
makes the LEDs super bright and
Code: [Select]
analogWrite(ledPin, 255);
turns it off.

On a single LED that is not on the resistor, 0 = off, 255 = full brightness.

Why is that? I guess the transistor is inversing the PWM's effects?
Should I carry on, using PWM with reverse values (ie 255 = off and 0 = full on) or does the transistor have other side effects on PWM?

Thanks!

--edit: see http://arduino.cc/forum/index.php/topic,108403.msg814896.html#msg814896 for up to date info
Title: Re: Transistor, PWM and LEDs
Post by: runaway_pancake on Jun 03, 2012, 01:31 am
What transistor are you using?
Perhaps there is a misconnection or two.
As shown, an NPN, it should be full on with 255 (not inverting.)
Title: Re: Transistor, PWM and LEDs
Post by: hfp777 on Jun 03, 2012, 02:27 am
I'm using a PN2222 transistor
Title: Re: Transistor, PWM and LEDs
Post by: pwillard on Jun 03, 2012, 02:35 am
Please tell me the you are using at least a 1k resistor on the base... arduino pin.  Secondly, yes, an npn will invert logic but you can work with that if wired right.   When the pin goes highfrom arduino it will turn on and supply gnd path to the leds.   Also, each led should have it's own current limit resistor.


To me, your results sound like a wiring issue.
Title: Re: Transistor, PWM and LEDs
Post by: runaway_pancake on Jun 03, 2012, 02:39 am
You didn't show a resistor between the base and the PWM pin, use one, 330? to 1K or so.
The leads of the PN2222 are E-B-C, looking at the flat/face and with the leads pointing down.
E goes to GND, B goes to PWM (via that resistor) and C goes the LEDs.

Make sure it's actually marked PN2222, I know of odd bits being stuffed into "multi-packs".

Another thing, each LED ought to have its own resistor (not doing so won't make it to work inverted.)
Title: Re: Transistor, PWM and LEDs
Post by: hfp777 on Jun 03, 2012, 03:03 am

Please tell me the you are using at least a 1k resistor on the base... arduino pin.  Secondly, yes, an npn will invert logic but you can work with that if wired right.   When the pin goes highfrom arduino it will turn on and supply gnd path to the leds.   Also, each led should have it's own current limit resistor.


To me, your results sound like a wiring issue.


I have a 220? resistor on the base. I'll add 2 other 220? for the two LEDs, thanks for the tip
Title: Re: Transistor, PWM and LEDs
Post by: pwillard on Jun 03, 2012, 03:53 am
Driving the base pin with over 20mA when 5mA or less is "enough"... is not considered to be "best practice".
Title: Re: Transistor, PWM and LEDs
Post by: Docedison on Jun 03, 2012, 04:44 am
One thought about 10 - 20 mA base current is that you can use a transistor with any Beta value whatever and Know it Will Work, Waste of power, Yes certainly but harmful no I have never found except in cases where the base current exceeded the device specifications that there was any great wrong done, obvious reasons excepted. Sometimes we forget that we were ALL new at something, sometime and advice of that nature about base current can be more defeating than a real lesson should be.
I Know How to do it right... Electronically about 98% of the time, after all occasionally, There is something New... I DO NOT always know how to tell someone else to do it right unless I am Very careful Because Every situation is Different...   IMO

Doc
Title: Re: Transistor, PWM and LEDs
Post by: Claghorn on Jun 03, 2012, 06:21 am
As far as I know, the brightness of the LEDs is entirely due to the current. I'm putting my pair of IR LEDs in series rather than parallel and adjusting the single resistor to a lower value to provide the 100mA of current the LEDs are rated for. I also used a darlington switching transistor which allowed me to use a 100K resistor from the output pin of the atmega
(actually a Teensy 2.0 board in my case).

Details of my circuit are here: http://home.comcast.net/~tomhorsley/hardware/arduino/teensy.html

(Of course the problem you run into putting LEDs in series is that with enough of them, the combined voltage drop is so big you can't get any current at all, but with just two I don't seem to have a problem).
Title: Re: Transistor, PWM and LEDs
Post by: hfp777 on Jun 03, 2012, 12:55 pm
Yes I put them in series first but I couldn't get any of them lit. I have 10 of them and the current drop was too much for any to light. My schematics has three because I wanted to simplify  the wiring while getting it to work but ultimately I will wire up all 10.
Title: Re: Transistor, PWM and LEDs
Post by: hfp777 on Jun 03, 2012, 12:57 pm

One thought about 10 - 20 mA base current is that you can use a transistor with any Beta value whatever and Know it Will Work, Waste of power, Yes certainly but harmful no I have never found except in cases where the base current exceeded the device specifications that there was any great wrong done, obvious reasons excepted. Sometimes we forget that we were ALL new at something, sometime and advice of that nature about base current can be more defeating than a real lesson should be.
I Know How to do it right... Electronically about 98% of the time, after all occasionally, There is something New... I DO NOT always know how to tell someone else to do it right unless I am Very careful Because Every situation is Different...   IMO

Doc


Thanks, it's much clearer.
However, I don't understand how to "send" only 5mA base current when I have no control over the intensity of the current sent out by the atmega chip. Should I use a resistor? But then it would be a waste of power too, right?
Title: Re: Transistor, PWM and LEDs
Post by: pYro_65 on Jun 03, 2012, 01:46 pm
Yes a resistor, this page helped me work things out: http://www.rason.org/Projects/transwit/transwit.htm
This one was good too: http://www.williamson-labs.com/480_xtor.htm
Title: Re: Transistor, PWM and LEDs
Post by: Grumpy_Mike on Jun 03, 2012, 03:14 pm
Quote
I don't understand how to "send" only 5mA base current when I have no control over the intensity of the current sent out by the atmega chip.

Yes you have it is the load you connect to it.

Quote
Should I use a resistor?

Yes

Quote
But then it would be a waste of power too, right?

It depends on what you mean by waste - in general no you are limiting the current. Thoughts about wasting power in this situation are meaningless.
Title: Re: Transistor, PWM and LEDs
Post by: majenko on Jun 03, 2012, 03:52 pm
That is not a good circuit.

Every LED (or serial chain of LEDs) should have its own current limiting resistor.  You should not share one resistor between multiple paralleled LEDs.

The way you have it now you will get uneven brightnesses, and some LEDs may not even light up.
Title: Re: Transistor, PWM and LEDs
Post by: hfp777 on Jun 03, 2012, 11:56 pm
I cannot fathom what is going on.

This is what I have now for the transistor
(http://i.imgur.com/6Gabv.png) (http://imgur.com/6Gabv)

And for the photocell
(http://i.imgur.com/6Gabv.png) (http://imgur.com/6Gabv)

And the code
Code: [Select]
const int ledPin = 6;
const int lightPin  = 0;
int   luminosite;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  luminosite = map(analogRead(lightPin), 0, 1023, 255, 0);
// this is inversed because I want the LEDs to become brighter as light increases
  analogWrite(ledPin, luminosite);
  Serial.println(analogRead(lightPin));
  Serial.println(luminosite);
  Serial.println();
  delay(200);
}


The lower the light, the lower the analog value on the photocell. And the higher the value mapped to luminosite.
So, when there is a lot of light, the luminosite should be closer to 255 and the LEDs be very bright.
When there is low light, the luminosite should be closer to 0 and the LEDs be less bright.
This is working as the values output on the serial monitor concur.

What blows my mind is that the LEDs work in reverse logic!!! If I hook one up directly to the Arduino pin, it is fine; 0 = off, 255 = full on.
But when I hook up the transistor, with the exact same code, the opposite happens!! The lower the 0-255 luminosite value, the brighter the LED!
Can anyone figure this out??
Title: Re: Transistor, PWM and LEDs
Post by: pwillard on Jun 04, 2012, 01:52 am
Quote
However, I don't understand how to "send" only 5mA base current when I have no control over the intensity of the current sent out by the atmega chip.


That *is* the primary job of a resistor in a circuit and what the recommended resistor will do in the circuit we discuss.

Quote
Sometimes we forget that we were ALL new at something, sometime and advice of that nature about base current can be more defeating than a real lesson should be.


To me, that statement is just noise that gives an excuse for doing shoddy work.  When working with transistors, there is no excuse for being loose and free with base current unless you have a supply of spare parts.   Any good advice  about base current can hardly be "more defeating than a real lesson should be".

You will note that I said "is not best practice".   I did not say the words "won't work".  However, I do intend to imply that ignoring advice (and ohms law) can mean that in some cases a device "lives fast and dies young".

Also, as grumpy mike has said in the past... just because something works... does not mean it's not doing damage.  Getting a concept wrong on a small scale can be really costly if in a final project you have replicated a misunderstanding many many times.

As for OP's most recent drawing and results.... if we are to trust your drawing... it won't work with your LED's in backwards.  As for your results... I still say... wiring error.

I used your code and the circuit you SHOULD be using... and I get the expected results...BUT only AFTER I make your MAP statement look like this:

[font=Verdana]luminosite = map(analogRead(lightPin), 0, 1023, 0, 255);[/font]

So, since you get a different result... your wiring must be incorrect.

IE; When Light level goes up, the LED gets brighter...
Title: Re: Transistor, PWM and LEDs
Post by: Grumpy_Mike on Jun 04, 2012, 10:35 am
Quote
And for the photocell

This is the same circuit as the transistor, is this correct?
As mentioned the LEDs are shown the wrong way round.
Using a transistor like this to drive an LED will work in the same manner as connecting the LED through a resistor from the arduino output to ground.
Title: Re: Transistor, PWM and LEDs
Post by: hfp777 on Jun 07, 2012, 07:36 pm
Latest developments: I replaced the CTPN2222 transistor with a pin for pin dropin BC547.
And to my surprise, the logic is no longer inversed.
I am glad but I still don't understand what is the difference between these two transistors that would make the logic inverted in the PN2222 and not in the BC547?
Title: Re: Transistor, PWM and LEDs
Post by: pwillard on Jun 08, 2012, 03:12 pm
Sadly, not really a 100% drop in replacement.

BC series have a different pinout than JEDEC based parts,   Jedec = EBC versus BC = CBE, which means you had the transistor in backwards.