I have a PWM signal that looks like the attached image. It's 12V with pulses of 0V at a 5% duty cycle. The frequency ranges from about 5Hz to about 50Hz. I'm reducing the signal voltage with a simple divider to keep it about 5V or so.

I want to drive an interrupt on each pulse using a PIC uC but I'm not sure how best to go about it. I don't mind using ECCP (the chip I'm using has it) but it's simpler for my code to just drive a simple interrupt. I can't use the signal's ground for my PIC as there is no usable corresponding live. My original plan was just to use an IOC on that pin...but I figure that won't work with a separate ground? Any ideas?
Second image is to check my understanding of first answer...

PWM trace

opto circuit


The best solution is probably a opto-coupler. I would arrange it so that the LED is lit when the signal goes low. Optos are generally faster to turn on than off, and this way will use lots less quiescient current.

The output of the opto will be the collector and emitter of a floating transistor, usually NPN. Connect the emitter to the PIC ground and the collector to a PIC INT pin or a interrupt on change pin. Either enable the internal pullup of the pin if it has one, or provide a external pullup to Vdd. The highest pullup would be about 100 kΩ if you have a fairly long time (10s of µs) between edges, don't care about the trailing edge of each pulse, and current drain is important. I'd probably use 10 kΩ unless speed under a few µs was needed.

Added about your proposed circuit

You proposed the circuit:

No, this is not what I meant. It doesn't seem you read much of what I wrote.

There is no need for Q2. As I said, connect the emitter of the opto's transistor to ground and the collector to the PIC input pin. Basically let the transistor in the opto be Q2 in your schematic.

Also, as I said, you have to make sure the PIC pin is pulled up somehow. This can be by enabling the internal pullup, but requires a external resistor if not.

Another point you seem to have missed is that it would be better to turn on the LED in the opto when the pulse goes low. It is not clear you have done that. In any case, you can't just connect a LED to a 12 V signal. Figure the LED will have about 1.8 V drop (the real value will be in the opto datasheet), and you shouldn't need more than a few mA thru the LED. Let's say that after taking the current transfer ratio, the response time, and the output load into account, you decide the LED should be driven with 2 mA minimum. A 4.7 kΩ resistor in series with the LED would guarantee that when 12 V is applied.

| improve this answer | |
  • \$\begingroup\$ Thank you. I knew I'd get round to using optos sooner or later! So, something along the lines of the (quick n dirty) circuit above? (couldn't work out how to add image here...) \$\endgroup\$ – Jon Jan 11 '13 at 16:12
  • \$\begingroup\$ Yes, sorry 'quick n dirty' meant that I'd not inserted and calculated any resistors, just tried to get connections correct. But I did misunderstand what you meant with the transistors. So I've chosen an opto that requires 10-60mA, Vf of 1.2V and calculated a resistor of about 1K ought to do it (or whatever's in the box about there). My opto's datasheet says the switch on and off times are the same (10us max), and I don't need to worry about a few mA as it'll be in a car so I'll avoid the LED pull-down for ease and use internal pullups on the PIC. Thanks for your patience! \$\endgroup\$ – Jon Jan 11 '13 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.