Pages: [1] 2   Go Down
Author Topic: Transistor, PWM and LEDs  (Read 2332 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

--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:
analogWrite(ledPin, 0);
makes the LEDs super bright and
Code:
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
« Last Edit: June 03, 2012, 04:57:50 pm by hfp777 » Logged

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

Offline Offline
Edison Member
*
Karma: 56
Posts: 2181
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

"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?"

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using a PN2222 transistor
Logged

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

Cumming, GA
Offline Offline
Edison Member
*
Karma: 18
Posts: 1627
Ultimate DIY: Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 02, 2012, 07:39:00 pm by pwillard » Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2181
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

"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?"

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Cumming, GA
Offline Offline
Edison Member
*
Karma: 18
Posts: 1627
Ultimate DIY: Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 44
Posts: 2804
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Delray Beach, FL USA
Offline Offline
Full Member
***
Karma: 0
Posts: 113
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1770
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: June 03, 2012, 06:47:53 am by pYro_65 » Logged


Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 504
Posts: 31324
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Full Member
***
Karma: 0
Posts: 118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I cannot fathom what is going on.

This is what I have now for the transistor


And for the photocell


And the code
Code:
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??
Logged

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

Pages: [1] 2   Go Up
Jump to: