How can PWM work with transistors?

Hi I have been trying to understand how to use PWM with a common anode RGB led (20mA) through a transistor and it is really difficult (I'm quite a noob here, more experience in software dev). I have BJT transistors and N-Channel mosfet's to try.

I would like to be able to supply current from a battery source for the LED to use with the use of a transistor. That sounds fairly simple, right? I think I am confused by Npn / Pnp, etc.

Is it right to think it is possible to use PWM for like the brightness control of a Led through a transistor or is it that the transistor would only be able to turn the led on and off? If you can do PWM then how would the transistor pins be configured?

With PWM you get the brightness control by switching the LED on and off fast. You effectively see an average. Speed control of DC motors works the same way.

It will work with a BJT or MOSFET although with a standard (max. 20mA) LED you don't really need one. You can just use PWM direct from an Arduino pin (through a resistor). Have a look at the Basics/Fade example in the IDE, or Analog/Fading. Both change LED brightness.

For higher currents you need the transistor and the connection is always similar. See https://www.arduino.cc/en/Tutorial/TransistorMotorControl for one example.

Steve

Thanks for the Dc motor example. I had googled so much I think I've gotten lost. The dc motor Arduino example seems to be the place to start thinking about this.

Note you need a resistor on each cathode of your LED before you switch it with a transistor.

Hi again :slight_smile:

I’ve done this and it works, but I am not sure if it should be (Probably because I have tried to make this dozens of times now and am surprised) … I drawn a schematic to show, could you say if it look’s right to you? Like, are the resistance values working ok and isn’t hurting the Tlc or Arduino?

the Outx is a Tlc5940 output, the Iref is at 2k Ohm…

and i am using a pnp 2n3906 transistor. Using pnp because of the Tlc5940 and lighting of a regular 20mA common anode rgb LED.

What is the Out x signal? If it only delvers 5V then you will not be able to turn off the transistor. If however it gives out a 9V signal then it will work correctly.

i think it is delivering ~5v.. so this means the transistor is not turning off even though the led lights up in the library example code? I’d like to be able to turn the transistor off ideally. Would a pull up resistor be a solution ? still trying to figure this out.

thanks for your help, this is a learning curve for me

The data sheet for the pnp for off mode says: Emitter−Base Breakdown Voltage Minimum 5.0vdc. If the Outx Is like doing up to 3.3v voltage, how can it get to 5v ?

See attachment.

You should use this what is on picture.
Two resistors are needed to cut of to much current otherwise your transistor will burn out very fast.
Using transistor is good idea.
And last important thing, connect led diode to the resistor and vcc, if You connect it to the emitter, then the whole power will go thru the base and emitter, and almost moving thru collector.
Transistor will start acting as normal diode.

If You will use transistor pnp, then You need remember about reverse polarity. And collector with resistor and diode will be connected to the ground. And using arduino pin low, will turn on transistor.

To better understand transistors use application on internet or mobile phone called everycircuit.

tran43.gif

Byte3:
The data sheet for the pnp for off mode says: Emitter−Base Breakdown Voltage Minimum 5.0vdc. If the Outx Is like doing up to 3.3v voltage, how can it get to 5v ?

To turn the transistor off you need to get the output up to 9V. For a PNP transistor to be off its base should be connected to within 0.7V of its emitter. So you need Outx to output 9V. If it cannot then you need an NPN transistor to drive it.

This is normal operation and nothing to do with the reverse emitter bias breakdown because we are talking about forward voltages here. For a PNP transistor the reverse voltage is positive not negative.

I see.. much to think about here, thanks Mike.

maxim2511:

ct led diode to the resistor and vcc, if You connect it to the emitter, then the whole power will go thru the base and emitter, and almost moving thru collector.
Transistor will start acting as normal diode.

If You will use transistor pnp, then You need remember about reverse polarity. And collector with resistor and diode will be connected to the ground. And using arduino pin low, will turn on transistor.

To better understand transistors use application on internet or mobile phone called everycircuit.

That explains why the led was lighting because the transistor is acting like a diode ?

I was looking at how to drive multiple led's and found the Tlc5940 but I think it is way too complicated for me now.I will try that every circuit app out some time, thanks

That explains why the led was lighting because the transistor is acting like a diode ?

No that is not right. The LED was on because you were turning it on with the voltage from the output. Putting zero volts from the Out x signal will turn it on. Putting 3V3 will turn it on also, you need to put a high voltage to turn it off.

maxim2511:
You need remember about reverse polarity.

No you don't, you are just confusing the OP.

You should check this circuit to understand how it works.
http://everycircuit.com/circuit/4919052534546432

maxim2511:
You should check this circuit to understand how it works.
http://everycircuit.com/circuit/4919052534546432

Except of course that it is completely irrelevant.

Why?

Because it is not the circuit the OP is using.

IMG_4909.JPG

Byte3:
Thanks for the Dc motor example. I had googled so much I think I’ve gotten lost. The dc motor Arduino example seems to be the place to start thinking about this.

That page has a serious error that can damage your Arduino.
The diagram/Fritzing shows a mosfet, but there is a TIP 120 in the parts list.
Never connect the base of a bjt transistor directly to an Arduino pin (with the emitter connected to ground).

High-side switching with a PNP transistor (post#4) won’t work, because the Arduino pin can’t provide the >8.5volt to turn the transistor OFF.
The ~27mA flowing into the pin protection diodes when the output is set HIGH or the 80mA when the pin is set LOW will also damage your Arduino.

As pointed out, you don’t need transistors for a single <=20mA RGB LED.
Just connect the anode to 5volt, and the three cathodes via >=220ohm resistors to three PWM pins.
Leo…

^ Ah that's good to know thanks

Really, i want to make a small tech-art project that would use 20 single RGB's and this is why I'm looking into using transistors to power it all. I don't want to use strips or stars or matrix design, just regular led's that I have designed a grid for, wanting to control each one individually, using transistors to switch from Arduino to a better power source?

, just regular led's that I have designed a grid for,

How long is the furthest LED from the processor? Long runs will prove problematical.

art project that would use 20 single RGB's

So that is 60 individual outputs you need from the Arduino. And each one would need to be capable of PWM to get more than the standard seven colours.

I don't want to use strips or stars or matrix design, just regular led's

These days addressable LEDs make the wiring simple and the software even simpler. You can cut up strips, get them individually mounted or in normal 5mm housings, so you can have them physically mounted in any configuration you want.

+1
Addressable LEDs is much easier.
Google "5mm addressable LEDs"
Here's one.
Leo..

Ah ! Okay, that is a much better idea.

I will certainly look at using addressable led strips then

Total size is approx 8" by 24" inches.

Grumpy_Mike:
These days addressable LEDs make the wiring simple and the software even simpler. You can cut up strips, get them individually mounted or in normal 5mm housings, so you can have them physically mounted in any configuration you want.

Ah, excellent ! I have no idea why I was ignoring this fact.

Wawa:
+1
Addressable LEDs is much easier.
Google "5mm addressable LEDs"
Here's one.
Leo..

many thanks