# How to remove spikes from switching transistor?

I am building a steady current source which delivers steady current (0-20mA) to an LED. The brightness of the LED (current) is controlled through a potentiometer's voltage and a voltage follower as shown in the picture below. Further, I want to blink (modulate) this LED with a square pulse frequency of 2kHz using a switch between the transistor gate and the voltage follower output.

The problem is that every time the switch closes, spikes are produced at the base and as a result at the source. Even when I reduce the current of the LED to 1mA (example picture 2), the spikes don't go away. Only when I set the current of LED to 0 do the spikes dissapear.

So the question is how can I remove/reduce these spikes? Is there some way?

[

• Multiple problems here, the largest probably being the fact that opening and closing the switch continuously breaks and closes the op amps feedback loop. – Matt Young Feb 18 '16 at 18:01
• Pretty sure I've seen this discussion before on EE:SE, but I am not sure where to look for it... (having to do with saturating op-amps when switching modes like this) – Daniel Feb 18 '16 at 18:04
• In paragraph 2; what base & source are you referring to? Also, is there any reason why you specifically need your pot & buffer to be located on the gate lead of the FET? Generally speaking, those would be more usefully placed on the +Vpos side of the LED, with the switch controlling the FET gate to connect/disconnect the LED from GND (I.E. where the FET already is). – Robherc KV5ROB Feb 18 '16 at 18:14
• If you don't understand the mechanism that is causing this, pay attention to what happens to the op amp when you switch the output to the gate off and the MOSFET turns off. I don't have any easy suggestions to fix it though. – Daniel Feb 18 '16 at 18:24
• In paragraph 2 i mean the gate and source of the fet. – Sparky Feb 18 '16 at 18:27

One way would be to use a SPDT switch, add a series resistor to the inverting input:

simulate this circuit – Schematic created using CircuitLab

Real switches have a certain amount of charge injection so a little more finesse may be required in a real implementation. A series resistor to the gate would slow the switching edges a bit, but in that case a capacitor from the output to the inverting input of the op-amp may be required for stability.

Some op-amps take longer than others to recover from saturation, so you could also just shop around for a different op-amp. Offhand, however, I don't have any useful suggestions- usually I try to avoid this situation.

Edit: Here is a really simple way to do this:

simulate this circuit

• What i want to do is to control the LED current and keep it steady at the mA i want while i switch it. I just didn't think of any other circuit to do this. I will try this thank you – Sparky Feb 18 '16 at 18:42
• @Lefteris See edit for a simpler way without the switch. It does waste the current for the LED by shunting it. – Spehro Pefhany Feb 18 '16 at 18:47
• i've been trying to find a solution for several hours now. I will check it again tomorrow first thing and i will tell you how it went thank you for your interest – Sparky Feb 18 '16 at 18:53
• I tried your second solution with a switch and an 80 ohms Resistor in series with it in the simulator and it works Great! – Sparky Feb 19 '16 at 10:33
• 5V -> LED is 'on' (transistor is 'off') – Spehro Pefhany Feb 19 '16 at 19:05

Because you're frustrated, here is the solution first thing! :)

Switch the 1V going into the top of the potentiometer, instead of the output connection.

In other words, use the potentiometer to modulate the amplitude of a 2KHz 1V square wave going into the op amp's noninverting terminal. Do not open and close this terminal, 0V means at ground potential. Open circuit is not 0V. But, as long as there is a potentiometer with one leg connected to ground, it will not be open circuit and work fine.

Note: the 33nF capacitor is to critically damp the LM324, but the compensation strategy will differ for each op amp, or often not even be needed if the op amp is internally compensated for unity gain.

Now for those interested, here is a somewhat lengthy explanation of what is going on, why there are current spikes, and why this change eliminates them.

Oh, actually, a quick note: you shouldn't refer to a gate as a base. One is a current actuated diode, the other acts like a charge actuated voltage-dependent capacitance. BJTs have bases, MOSFETs and IGBTs have gates. You called it by both terms in the question, just something to watch out for.

Anyway...

There will always be a spike, because electromagnetic waves take time to propagate at the very least (assuming you had some magical op amp of the infinite that uses unicorn farts instead of the standard blue smoke they normally have inside). But the far bigger problem with non-magic op amps like the one in your circuit is that they have a reaction time. Every control loop has reaction time. For there not to be a spike, your op amp would actually need to predict the future and begin slewing downward before the switch is closed to account for this reaction time (which is really just what we call 'phase lag' in the context of AC amplification. For a DC control loop, I prefer to think of it as reaction time but it's really just metaphorical sugar).

What you're doing is essentially causing a transient. This is no different from if you were physically connecting an LED to a current limited power supply, and taping the lead against one of the output terminals, making the LED flash on and off. It's like you're disconnecting and connecting the load physically. Indeed, that's exactly what you're doing, only its the output that you are disconnecting, but the point is that you're creating a discontinuity by adding and removing a load. The rise time is nanoseconds if not less.

Take any bench power supply with current regulation as well as voltage, set it to 2mA, set the voltage higher than the LED's drop, put a current sense resistor in series and view it on an oscilloscope, then hook up an LED. I promise you that any power supply, from a $50 sketch box to a$10,000 Agilent/Keyshite power supply will have a current spike just like you're seeing in your circuit. It will be lower or higher in amplitude and wider or narrower in time depending on the quality of the power supply, but it will always be present. Always. It's unavoidable. Your options are limited to acceptance or denial. Either way, the current spike will happen.

This is actually one of the main factors that separates a good power supply from a bad one, transient response. This is how fast it can respond to a sudden change like the one you're causing to happen at 2kHz. Nothing responds instantly, unless the datasheet has ∞V/µs as the slew rate for the op amp (it doesn't). A control loop for current is always fully on if there is no current flowing, because it wants 2mA not 0mA to be flowing. It doesn't matter if it is because there is no load for current to flow through, or no connection from the output to the pass transistor, it's ultimately the same thing - way less current than 2mA. A nice \$1000 power supply will have very good transient response, on the order of 15µs for a 50% load step. That means if it is a 1A power supply, and you have it set to 500mA, and it is open circuit, then you connect a dead short, more than your 500mA current limit will flow into the load for only 15µs before going into regulation. Likewise, if the load suddenly doubles in resistance, the current going through it will fall beneath 500mA for 15µs before catching up. This current spike isn't a spike, its just the rise time and fall time of your control loop. And all control loops have them. You can lower the amplitude by using a resistor to limit the maximum possible current, and you can shorten it by making your control loop faster (which is also a trade off with stability), but you can't make it go away.

Plenty of power supplies will actually blow an LED if you turn the voltage all the way up but set the current to 2 ma. Try it. They'll dump enough current through the LED long enough to kill it before the control loop can get the current into regulation. Any time you have a load transient, there is going to be to much, or too little current going through it. Normally its brief enough that the device doesn't care. But it is there, even if you don't realize it.

Hopefully you understand why the spikes are occurring now. Since a load transient causes spikes that cannot be avoided, the solution is obvious - remove the load transient.

If the LED and control loop is always connected, and it is the noninverting input that is slewing from 0 to , then the current regulation point will follow that. There will be lag still, but the lag will be a slight delay in the wave form (it will be slightly out of phase) but it will still match. To return to the power supply metaphor, this is like you're leaving an LED connected to a power supply, but wiggling the current knob up and down really fast, in contrast to physically connecting and disconnecting the LED.

• Thank you for the explanation sir. I tried your method with the simulator and it worked even without the 33nf capacitor. The problem now is that i have three correct answers and i don't know where to put my check mark. :-) – Sparky Feb 19 '16 at 11:06
• Feel free to delete this comment, but what is that wonderful simulator in this answer? The color scheme is so much easier on the eyes than std ltspice! – Luke Gary Nov 6 '16 at 21:54

I think the issue occurs when the switch opens and breaks the feedback loop of the op-amp. But, because the op-amp is non-ideal in this simulation, it has a real response time. When the switch is closed, the op-amp has to drive back down from the positive rail (the decreasing slope you see) to the quiescent level necessary to keep the LED current regulated where you want (the flat part). Perhaps a better implementation would be to move the switching logic to the input side of the op-amp, and switch the reference on and off. It's generally inadvisable to break the feedback loop like you're doing.

Is there a reason why you couldn't use something like this?

Here:

1. The OpAmp scales your 0-1V pot input up to 0-5V.
2. This Vout level, across the voltage drop of the LED and the resistance of the FET & the 50ohm LED series resistor controls your current.
3. The switch controls the FET (or could be placed in the FET's place directly) to cause the LED to switch on/off at your desired frequency.
4. The 50uH inductor poses a high impedance to transient spikes (7.9Kohm @ 25MHz for ~20ns spikes), but only a very low impedance (0.628ohm) at your 2KHz switching speed.
5. The diode allows the inductor to discharge without causing harmful voltage spikes when the switch/FET 'opens.'
• I tried this but it produces a great deal of spikes both at the op amp output and the source of the FET during the the rising edge of the pulse mainly (Closed switch). Of course this circuit is far better than my... approach since these spikes last for almost 20nsec when i tested it in multisim. – Sparky Feb 19 '16 at 10:30
• 20ns pulse-width = 25MHz frequency. As long as your switching frequency is at least 3 octaves below that, you could add a series inductor with high reactance to the 25MHz spike that would have a minimal effect at your switching frequency (just be sure to add a free-wheeling diode to dissipate the stored energy when the switch is opened). – Robherc KV5ROB Feb 19 '16 at 10:50
• @Lefteris answer & diagram updated. – Robherc KV5ROB Feb 19 '16 at 11:03

The answers of Metacollin, Spehro Pefhany and KV5ROB are all correct and solve the problem of spikes pretty much as it was mentioned in my question at the top of the post. I tested all three of them in the simulator and here are the results.