Electronic – How to read a PWM with a separate ground using PIC

microcontrollerpwm

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

Best Answer

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.