Transistor/PWM problem

My porblem is that when the PWM = 0 ie at the start of the for loop the LEDS are still on. As the PWM goes up they become brighter. SO how can i make them turn off correctly? I need to USE 12V for the LEDs.

I think this has something to do with the transistor and using only 5V instead of the 12V im trying to drive? Any advice would be great.

This is my curcuit basically:

And my code just a really simple test piece.

const int driverPin = 9;
int i=0;

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

void loop()
 {
    for(i=0;i<256;i++){
      analogWrite(driverPin,i);
      Serial.println(i);
    }
    delay(200);
 }

Is the ground from the transistor connected to the Arduino ground?

You diagram has no current-limiting resistor for the LED - is this intentional?

You should set the Pin Low initially.

But that wouldn’t explain why it gets brighter round the loop! I was thinking a floating-ground issue.

Also this is not the right forum for this kind of question, please note.

sorry the resistor is there just not in the PCB. If i connect the ground pin (3of them) to the GND of the pwoer supply it makes no difference. How can i tell the pin to be LOW? I though that anaologWrite(pin,0) would be low? Thanks Rob

Simplify things. Connect 5V to LED Anode, cathode to a resistor, other side of resistor to pin 9.
You don’t need the transistor.
Look at the examples here.

hookup_examples.jpg

Have you tried the resistor approach?, You can set it low initially by using the digitalWrite() function and then it will surely get low and then when your loop start's it will make use of the PWM function as you are using the Analog Write() function and as such the program will deal or mainly switch on the supply only when it reaches the loop and not before as you are encountering.

If you do want to use the transistor, make sure to wire it like the examples here.

LED_connections.jpg

Assuming you have the transistor wired correctly, there is also likely some leakage current from collector to emitter, and with a 12v source that could be enough current flow to make the transistor appear to be on.

I see a couple problems. 1) You need to make sure there is a ground connection from the breadboard to the Arduino board or there is no common referece for the circuit. (as MarkT mentioned) 2) For a proper transistor circuit, there should be a leakage drain resistor from the base to ground. Try a 470 ohm (1/4 watt). Usually transistors are good enough these days to do without it, but good circuits are more reliable.

To test whether the transistor is shorted, take the line going to the Arduino and and uplug it. If the LED is still on, then the transistor may be bad or very leaky. Also ground that same line. If the LED is still on, then the transistor is definitely bad.

Or you could just measure the voltage on the line to the arduino. That would tell you if it's an Arduino issue or transistor issue. But definitely connect a ground line, and make sure you have a 1/2 watt 470 ohm resistor for that LED.

One last thing. It looks like you're using a 470 ohm transistor on the base circuit. That means the Arduino will be driving about 10ma at the max. If that's the case, then why not just drive the LED directly with the Arduino (as Crossroads suggested)? You probabbly want more like a 4.7K resistor there (not that this would cause your symptom).

I did say I need to use the 12V supply as Im going ot have alot of LED's being pwoered.

If i discounnect the arduino the LEDs are still on about 50%. and goes from 50%-100% and also if a tottally disconnect the GND :-s

So it's not an Arduino issue then.

If you ground the middle pin of the transistor and the LED stays on, (and assuming you have the rest of unseen circuit correct) then the transistor is bad. (Although it should not change 50 to 100%). Are you sure it's an NPN transistor?

A 470 base to ground resistor is overkill. Lets see that the circuit is wired correctly prior to making drastic additions like that. Say there was an output pin to base resistor already of 470 ohm to limit current into the base to 10mA ( (5-0.7Vbe)/470 = 9mA ) Now the 470 base to ground just cut that in half and likely keeps the transistor from turning on hard enough to fully drive the LED.

How many LEDs are actually in series?

And what is the transistor you are using?

I’m suggesting the 470 because it may be a very leaky transistor. That will show if it is, but only up to a 1.4ma leak.

Also here is a way to drive an led using one resistor instead of two, but only if the supply for the led is the same voltage as the supply for the Arduino.

SingleResistorTransistorCirc.GIF

CrossRoads: A 470 base to ground resistor is overkill. Lets see that the circuit is wired correctly prior to making drastic additions like that. Say there was an output pin to base resistor already of 470 ohm to limit current into the base to 10mA ( (5-0.7Vbe)/470 = 9mA ) Now the 470 base to ground just cut that in half and likely keeps the transistor from turning on hard enough to fully drive the LED.

How many LEDs are actually in series?

Not true. 470 base to ground only takes 1.4ma max. .7/470 =1.4ma. You still have 7.6ma to drive the transistor. That is overkill. The Led is only going handle about 30ma steadily. The transistor is likely to have a gain around 100 or more. You only need .3ma to drive that. Standard transistor circuit design in the old days.