LED flickering when off

Folks, I’m not an EE… but try to play one!

Here’s the circuit I cobbled together:

The Led is a few amps, so I understand that I can’t drive it directly!
I had a TIP42 PNP laying around.

Here’s the code I wrote:

#include "math.h"

int dim = 0;
int toggle;

void setup() {
    toggle = digitalRead(D6);
    Particle.variable("DimValue", &dim, INT);


void loop() {

    if (digitalRead(D6)!=toggle)
        if (dim>0)
            dim =0;
            dim = 254;
        toggle = digitalRead(D6);
    for (int i=0;i<100;i++)
        if (dim<254)
            //off duty cycle
            if (getPinMode(D1)!=INPUT_PULLDOWN)
        if (dim>0)
            //on duty cycle
            if (getPinMode(D1)!=OUTPUT)
                digitalWrite(D1, 0);

int SetDim(String command) {
    /* Particle.functions always take a string as an argument and return an integer.
    Since we can pass a string, it means that we can give the program commands on how the function should be used.
    In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
    Then, the function returns a value to us to let us know what happened.
    In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
    and -1 if we received a totally bogus command that didn't do anything to the LEDs.

    dim = (254*command.toInt()/100);
    return dim;

I’m essentially PWMing between input and output mode on D1.
With SetDim(1-100), everything appears to work fine.
SetDim(0) does turn the Led off, but randomly, the Led blips…

What am I doing wrong?



Why? Why not just use PWM (via analogWrite), which is available on D1?

Also, Vin is capable of supplying 1 amp at 4.8 volts, so if your LED is really “a few amps”, you’re trying to draw too much power from Vin.

You are probably having problems with switch “bounce” also, since you aren’t making any provision for that.

Your led is placed in the reverse direction and you probably want to use a NPN transistor or mosfet instead.

I tried analogWrite & writing 0 or 255 never shuts down the led… it dims it a little but that’s it.
Having the base of the transistor open cuts off the led. Base = Vin turns on the led. That’s what I am trying to replicate through software pwm.
(btw, I’m not drawing the Led power from Vin. Supply to vin comes from transformer.)

Yes, I know about the bounce issue, but I’m not there yet!

Kennethlimcp, yes, the led on the schematics is turned the wrong way - noob drawing error!

An analogWrite of 0 should turn the LED off, and it does when I test with a MOSFET rather than a bipolar transistor. It could be that your transistor is not turning off completely (I don’t have much experience with BJT’s). I’m using a FQP30N06L from SparkFun. This is really overkill since it’s good for 60 volts, 30 amps, but they only cost $0.76 so they’re good for all sorts of uses. The threshold voltage to turn it on has a max of 2.5 volts, making it good for 3.3 or 5 volt systems. You should also probably have a resistor in series with your LED. I’m going from 5 volts through a 150 ohm resistor to the LED + lead, then LED - lead to the drain of the MOSFET. The MOSFET’s source is connected to ground and the gate to A4.

Why do you switch the pinMode(D1)?
Why not just stay in OUTPUT?

Try that and see.

The internal pull-down (~40k) might be too weak. You could try an ext 4k7 with INPUT.

1 Like