Can't get transistor to work correctly as a switch

What I'm trying to achieve:

I have a 5V PWM signal that either needs to be left as-is, or converted into an analog 0-5V signal.

How am I planning to achieve this?

The PWM to analog conversion is done through an RC circuit using a 4.7K resistor and 0.1uF ceramic capacitor.

To switch between the signal being digital or analog, another input alternates the voltage at the base of a PNP transistor, which short-circuits the RC circuit, allowing the original PWM signal through. If the transistor is off, the output signal takes an analog form filtered by the RC circuit.

It kind of works. A PWM duty of 33% should produce an analog signal of 1.65V. With the transistor off, this works as expected with an analog output signal of 1.65V. With the transistor ON, a PWM signal is passed through but at a voltage of 2.5V instead of the expected 5V.

I'm expecting a very small voltage drop through the transistor which should be negligible. However I'm seeing this is not the case.

I have a basic understanding of transistors but I think I'm probably doing something obviously wrong here.

Perhaps there's a better way of approaching the problem. I thought about using relays to short-circuit the RC circuit, but there must be a "simpler" way of using semiconductors to do the job.

Any help appreciated! • Where did you find this circuit? – Ignacio Vazquez-Abrams May 5 '14 at 13:02
• I came up with it lol - why? – Ibraheem May 5 '14 at 13:05
• It's not surprising that it doesn't work. You could replace the transistor with a diode and it would do the same thing. – Ignacio Vazquez-Abrams May 5 '14 at 13:13

If I assume that that the X junction is connected, then when the PWM signal is "high", the output is indeed high, however when it is low, the transistor is 'off' and the capacitor discharges through the resistor.

You really have to disconnect the capacitor to get this to work. A simple 4000-series analog switch would work beautifully (eg. 74HC4053)- you could simply multiplex between the PWM signal and the analog signal (the 4.7K resistor won't affect the digital signal unduly, but the capacitor will).  simulate this circuit – Schematic created using CircuitLab

I guess you could simulate an analog switch by adding another transistor (NPN) driven with the opposite logic, but you will still have the capacitor connected to the logic signal, which will affect the waveform greatly.

• Yes the X junction is connected. So just to understand the problem properly, when the PWM is low and control signal is HIGH (i.e transistor is OFF), C1 is discharging through R1? – Ibraheem May 5 '14 at 13:15
• Thank you for the suggestion, I'll take a look at analog switches! – Ibraheem May 5 '14 at 13:15
• Yes to your first question. If you were able to disconnect the capacitor then the output would follow the input with alternately a transistor or 4.7K in series, not great, but it would work. It's bad to connect a large grounded capacitor to a logic output directly, it will draw large pulses of current every time the output switches. – Spehro Pefhany May 5 '14 at 13:45
• Thank you I've been investigating the 4053 and it looks perfect! – Ibraheem May 5 '14 at 14:01