Go Down

Topic: Transistor, PWM and LEDs (Read 3294 times) previous topic - next topic

hfp777

Jun 03, 2012, 01:10 am Last Edit: Jun 03, 2012, 11:57 pm by hfp777 Reason: 1
--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:


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
I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

Runaway Pancake

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.)
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

hfp777

I'm using a PN2222 transistor
I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

pwillard

#3
Jun 03, 2012, 02:35 am Last Edit: Jun 03, 2012, 02:39 am by pwillard Reason: 1
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.

Runaway Pancake

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.)
"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

hfp777


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
I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

pwillard

Driving the base pin with over 20mA when 5mA or less is "enough"... is not considered to be "best practice".

Docedison

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
--> WA7EMS <--
"The solution of every problem is another problem." -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Claghorn

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).
http://home.comcast.net/~tomhorsley/hardware/arduino/arduino.html

hfp777

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.
I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

hfp777


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?
I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

pYro_65

#11
Jun 03, 2012, 01:46 pm Last Edit: Jun 03, 2012, 01:47 pm by pYro_65 Reason: 1
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

Grumpy_Mike

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.

majenko

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.

hfp777

I cannot fathom what is going on.

This is what I have now for the transistor


And for the photocell


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??
I am a total amateur at electronics and all things related. I have no education in the matter other than self taught.

Please factor this in

Go Up