Convert varying PWM into solid digital ON/OFF output

Hey Guys, I've run into a challenge I was not expecting. I expected to collect a +12V signal from a 12V LED when it was turned on. Instead, it is always connected to positive and PWM to ground to turn it on. I need to be able to know when the LED is on, and detect it on a digital PIN of the Arduino. I've already got a NPN Transistor waiting for the signal to convert it to a +5V input when triggered (I know I should make this a PNP because it's a negative signal, but again, I wasn't expecting this).

Code is as below:

if(digitalRead 5, HIGH) doStuff(); //It's really just a trigger for a function

Currently, my Arduino is getting mixed signals as it reads the pin. Depends where in the PWM cycle it does the read. But as it does this very frequently, it's picking up a digital HIGH signal and runs doStuff() even when the LED is off and is on a very low duty cycle.

As far as I can tell, to do this we need to do 3 things: (unless someone has a better idea) -Convert the negative supplied PWM to a positive signal. This can be done with an PNP transistor, I got this covered. -Convert the PWM signal to an analog voltage. This can be done with a resistor and capacitor like here. http://www.ontrak.net/Pwm1.gif -Convert the varying analog signal into a digital +12V ON or OFF so the digital pin can read it. ...my question is, how do we do this ^ ?

Schematic is as seen below: What can I put between the 2 points highlighted in red to do the digitalRead() and pick up when the LED is turned on?

Is there some code involved? Must be, assuming this is some kind of Arduino issue, although you didn't say.

You might be able to sort this out in software....

alxndr: -PWM into analog voltage. Using a ceramic cap and a resistor. (Shouldn't be hard).

Those RC values won't work well for 100Hz, time constant = RC = 4.7ms, you want the time constant to be >> 0.01s (try R = 100k)

Analog voltage into digital I/O.

That's what a comparator is for.

Sorry guys, I was writing the first post as I was rushing out the door and I didn't have time to compose myself. I've edited the first post for the people just entering the thread. Hope the schematic and code help.

MarkT: That's what a comparator is for.

YES! A comparitor! So is the easiest way to get one of those with an OPAMP? Or how else can I do it? I need to have a threshold voltage.

Either this with the combination of my other 2 steps, or something more elegant that does all 3 steps.

Can you try again with that circuit. You have drawn a PNP transistor and put c next to the emitter and e next to the collector. What is correct the symbol or the labelling?

Grumpy_Mike: Can you try again with that circuit. You have drawn a PNP transistor and put c next to the emitter and e next to the collector. What is correct the symbol or the labelling?

Derp, the labelling is correct. One sec, I'll adjust the image.

EDIT: ...and done.

You will only be able to detect the fault if the LED fails open circuit not short circuit.

You need a resistor and capacitor at that point, the cap will smooth the PWM signal.

If the labelling is correct and you have an NPN then the base will be pulled higher than the collector by 7V and that could blow your transistor, chech the transistor's data sheet.

Better if you have a common emitter circuit with that transistor not a common collector.

Hey all, just in case someone needed an answer or would like to know what I did. I used a cap to smooth the incoming and mainly put a delay on the pulse, so it's not fluctuating wildly. Resistor was not needed because VMAX could be at 12V no problems. The PWM fades from 5% to 100% closed circuit. So the resistor just delayed the feedback and charge of the capacitor. Just used a 20K pot, 1uF cap and LM341N. I could have used a comparitor, but this works and it's the hardware I had on hand. Schematic and my PCB layout are below.

Glad it works.
Two points

  1. you don’t draw a pot like that.

  2. the output of they circuit is 12V that could damage your arduino if you feed it directly to a pin. You need a potential divider to ensure the maximum voltage the arduino sees is 5V.

Grumpy_Mike: 1) you don't draw a pot like that.

What's wrong with it? There are several perfectly good ways and that's one of them. You can have zig-zag track or rectangle, you can have arrow at side or diagonally across.

You only use the diagonal line across the body when you connect the pot as a variable resistor, not when you use the wiper as a potential divider.

Indeed that's the usual convention, but so long as its clear what is meant there's no need to jump on people (compared to some of the attempts of circuit notation on here!).

The diagonal line is a dashed line when it represents mechanical linkage of multi-gang pots (or variable caps), so using a solid diagonal line is different and I don't see it being ambiguous in anyway.

After all people draw transistor symbols without an enclosing circle all the time, which isn't standard but is perfectly clear.

I wasn't jumping on anyone just pointing it out, no value judgment involved, I though the OP would like to know how to do it conventionally. The more important part was point 2.

Grumpy_Mike: Glad it works. Two points 1) you don't draw a pot like that.

2) the output of they circuit is 12V that could damage your arduino if you feed it directly to a pin. You need a potential divider to ensure the maximum voltage the arduino sees is 5V.

1) Without appearing to be completely sarcastic, or a jerk. Sorry it doesn't meet your standards, it was just a 45 second drawing to convey the solution I settled with. It's not an official drawing to be published in a engineer's drawing. But for the most part, the internet appears to agree with me and my illustration of a potentiometer - http://goo.gl/QIkgLo. I figured it was nice to post on the forum and share my decision even though I had already had my solution.

2)If you check earlier in the thread, on the first post. The schematic I have listed there already includes a transistor to drop the 12V down to 5V. Which you've already seen and disputed against saying common emitter is better.

Grumpy_Mike: Better if you have a common emitter circuit with that transistor not a common collector.

Regardless of that, thanks to both MarkT, and forever and always GrumpyMike for your contributions.