Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Another LTspice question. Vswitch bug?

Status
Not open for further replies.

Blueteeth

Well-Known Member
Hi,

I have posted a few questions about LTspice, and have several issues with simuations - all of which were due to my lack of understanding of the software and silly mistakes, I'm guessing this is no different. So I'm not complaining about LTspice at all, I'm just stumped and need someone to point out an obvious error in my simulation :)

Its a basic high-side current sense circuit. As I wanted two ranges, one to measure 0-1A, and one for 0-100mA, instead of changing the gain of the sense amplifier by 10 (which would also amplify offsets) I plan on using a mechanical switch to change the sense resistor from 0.1ohm to 1ohm - simple enough eh?

I've used voltage controlled switches as a crude way of simulating mechnical switches, just changing the voltage across the switch to turn it on/off. Simple, and very handy for switching things in and out of circuits - just using very low on resistance, and very high off resistance. Seems that if I short the switch with a wire/node in this simulation, it works fine. Short the other switch, works fine. But if I turn either switch on using a voltage source, no dice. Heres the schematic, and I've included a small zip file of the *.asc file, and the LM358.sub model file.

I'm pretty sure I'm msising something obvious, someone put me out my misery... as things like this bug me, even though its really just for academic purposes rather than a vital project.

Cheers!
 

Attachments

  • Isense2A.zip
    1.5 KB · Views: 159
  • Isense2a.PNG
    Isense2a.PNG
    34.2 KB · Views: 618
hi BT,

This what I get with your asc file.???

Do you get a different plot.???
 

Attachments

  • AAesp03.gif
    AAesp03.gif
    35.2 KB · Views: 362
hi BT,

This what I get with your asc file.???

Do you get a different plot.???

Hi eric,

I do indeed get a different plot, but I see you jsut checked the current going though the load. That works. Apologies if I didn't explain properly, I should have added a net label for the output voltage. The switches carry the current when on, that works, the voltage across them when on is tiny (Ron=0.01, so V = 0.01 * I), that works. They switch the current just fine, the problem is it seems to interfere with the rest of the circuit! I doin't see *how* it can. I'm going to kick myself into next week when I realise what it is...

So, here's two images. Full screen size so you can see it (but just png's, so still small in size).

The first is what the 'Vout' should look like - this is with a shorted switch (Isense1). The second is without a short across the switch, but the switch turned on (Isense2). That is the only difference. The fact it works with thr short means the circuit works 'as intended', and indeed, without the switches, using just one current sense resistor, it works beautifully. The very presence of the switches makes LTspice unhappy.

Weird eh?
 

Attachments

  • Isense1.PNG
    Isense1.PNG
    47.1 KB · Views: 312
  • Isense2.PNG
    Isense2.PNG
    46 KB · Views: 345
Last edited:
hi BT,
Just to let you know I am looking it over...

EDIT:
Reduced the circuit to just the two switches, S1 is not being enabled by the applied voltages, so when you short out S1 it is operating incorrectly.?

If I swap over the 0 and 1 drive volts the S1 does come on.
 

Attachments

  • AAesp04.gif
    AAesp04.gif
    29.5 KB · Views: 258
Last edited:
Hi Eric,

thanks for checking! The two voltage sources I used, V4 and V3, is just me trying to be clever. I figured the if I connect one Vswitches '+' to a permenant voltage, then to enable it, one would have to pull the '-' input low (assuming a Vt of <1V). So, by changing the voltage of V4, to 1V, S1 is on, and S2 is off, and with V4 = 0V, S1 is off, and S2 is on. I have done this on many simulations (with more than 12 switches..) just saves me changing the value of two voltage sources to switch a group off, and another on.

Thanks for confirming things, I wasn't asking you to spend any time on it, as its not exactly a 'major problem', just one thats niggling at me (never could let things go...).

I was also recently stumped by a simulation of a low current CC/CV supply I paln on building. It used the LM317 model found on this very forum. After 2 hours (I know, you can tell I'm not married eh..) I eventually realised the model had an internal current limit of 845mA - which is the Ilimit for the SOT223 version, not the TO-220... and its little things like that which lead me to believe I'm at fault here.

Well, half way through writing this reply, I got it to work. The PWL settings for the load, start at 10mA. The original: PWL(10m 10m 20m 300m 30m 900m).
I changed the load to '0' at 10ms, so it starts with no load. This works, wit the switch enabled, it behaves as if its shorted, and when disabled, like open (well, a 1Meg resistance, which I can change). Same effect can be achieved by checking 'start external voltages at 0', although I don't always use this as it can take ages for the transient analysis to do its thang (when I'm just checking DC things, and transient response isn't needed).

So after all that, its not a bug, not the Vswitch, and, not *quite* my stupidity. But rather an oddity of the opamps spice model when the simulation starts at full voltage. Sorry if I've wasted your time, just hope it perhaps *maybe* might save some headscratching for you, or others, in future projects.
 
Last edited:
hi BT,
Thanks for the update, if you ever want a sanity check on a LTS simulation I would be pleased to give your circuits a run, it makes me aware of any 'holes' that maybe waiting for me.!;)

BTW: you could use the .IC LTS Directive to set the Initial conditions in parts of your circuits,
 
Hi Eric,

thanks for checking! The two voltage sources I used, V4 and V3, is just me trying to be clever. I figured the if I connect one Vswitches '+' to a permenant voltage, then to enable it, one would have to pull the '-' input low (assuming a Vt of <1V). So, by changing the voltage of V4, to 1V, S1 is on, and S2 is off, and with V4 = 0V, S1 is off, and S2 is on. I have done this on many simulations (with more than 12 switches..) just saves me changing the value of two voltage sources to switch a group off, and another on.

Thanks for confirming things, I wasn't asking you to spend any time on it, as its not exactly a 'major problem', just one thats niggling at me (never could let things go...).

I was also recently stumped by a simulation of a low current CC/CV supply I paln on building. It used the LM317 model found on this very forum. After 2 hours (I know, you can tell I'm not married eh..) I eventually realised the model had an internal current limit of 845mA - which is the Ilimit for the SOT223 version, not the TO-220... and its little things like that which lead me to believe I'm at fault here.

Well, half way through writing this reply, I got it to work. The PWL settings for the load, start at 10mA. The original: PWL(10m 10m 20m 300m 30m 900m).
I changed the load to '0' at 10ms, so it starts with no load. This works, wit the switch enabled, it behaves as if its shorted, and when disabled, like open (well, a 1Meg resistance, which I can change). Same effect can be achieved by checking 'start external voltages at 0', although I don't always use this as it can take ages for the transient analysis to do its thang (when I'm just checking DC things, and transient response isn't needed).

So after all that, its not a bug, not the Vswitch, and, not *quite* my stupidity. But rather an oddity of the opamps spice model when the simulation starts at full voltage. Sorry if I've wasted your time, just hope it perhaps *maybe* might save some headscratching for you, or others, in future projects.
It's not a quirk of the op amp's spice model. The circuit can actually lock up with the op amp output at the positive rail. In your schematic, the transistor saturates, and your feedback becomes positive instead of negative. You can put a diode in series with the collector to prevent the positive feedback, but if the op amp output ever goes high enough to saturate the transistor, i.e., forward bias the c-b junction, the +input pin will be higher than the -input pin, so the "fault" will be stable.
A fix that seems to work is to add a small signal diode such as a 1N4148 from the op amp output to the -input, anode to the op amp output. This diode will be reverse biased in normal operation, but it seems to insure that the circuit always comes up in the linear mode.

In tha attached sim, I added .ic v(a)=15 to try to force the circuit to latch up. If you add UIC (use initial conditions) at the end of the .TRAN directive, you get a different start-up transient, but it still comes up in the linear mode.
 

Attachments

  • Isense2a fix.asc
    2.8 KB · Views: 167
Ahh Roff, thanks for the heads up! I have to admit, I had to re-read your post at least 4 times to understand it... (my knowlenge of raw analogue still has major gaps).

I downloaded the file, but couldn't get it to run. I believe it was because you have your own symbol defined for the LM358 (sometihng I've done on my main PC, but I have 2 PC's, and two laptops...) then I had to remove the net label 'a', and redraw the node, labelling it 'a' (I have no idea, and this probably changed what you did). Now I'm getting 'timestep too small'. I removed the .ic v(a)=15, just to see how it ran, and the voltage output has an error or roughly +9%. But! when the diode is removed, as per my original simulation - yep, latches up and output stays high, so even with the error of 9% is better than latchup! I'm guessing the error is something I have done wrong by messing with your file.

Could also be that I have a different LM358 model - actally I have two, one from NS (LM358/NS) and one from TI. The TI's model appears to have a very low offest voltage (basically, ideal).

I've been palying with spice for a year now, but I must confess I am still pretty ignorant as to using some of the parameters, or understanding them. Not asking for a tutorial here, as I'm sure the helpfile knows all, just maybe giving an excuse as to why I can't get that *.asc file simulating. But in general its such a handy tool to have! Even just using basic circuits, the ability to analyse voltage and current anywhere in the circuit saves lots of time breadboarding/headscratching. I'm still impressed by the accuracy of it too, simulations will never be as good as real world circuits, but good lord this gets close... even to simulate latch up (something I have heard about, but knew hardly anything about).

Thank you so much for taking the time to look at it man! I know its just a simple high-side current sense, with perhaps a less than ideal opamp used, but I was intrigued to see how accurate it could be when compared with other methods - where low side sensing is inappropriate that is. As for a solution, I'll breadboard it with and without the diode. If needs be, when put 'in circuit' I was planning on using P-channel MOSFET's for the switches (and some clever switching of the opamps inputs), which wil be controlled by a microcontroller, so I could always just delay those switching on, until the opamps power is on. Once again, what started as a need for a 0-200mA constant current source, has ballooned into a 0-20V, 0-4A precision CC/CV PSU - and this was a part of it :/
 
Your biggest error is in the switch ON resistance. It adds 10% to your current sense resistor in the high range. Change Ron to 1µΩ and Roff to 1e12. It still runs on my sim.
Another error is op amp input offset voltage. My model has about 2.3mV Vos at the operating point. This contributed about 2.5% error on the high range (determined by simulation - I didn't calculate it).
Another source of error, maybe the last one, is transistor base current. It contributes about .33% in the sim (at 900mA). You can use a MOSFET like BSS123 instead of an NPN, but you might have to add frequency compensation to the feedback loop. It seems to eliminate the diode that I had to add to prevent latchup.
After correcting for all three sources of error, the remaining error was essentially zero.
 
Oh wow, thanks again :)

I believe I have some MAX4172's (https://www.electro-tech-online.com/custompdfs/2011/10/MAX4172.pdf) which appear to have much lower error - at least for the opamp/transistor. But...I'll use those for my main bench PSU design. A relatively simple circuit such as this one would be great for a smaller power supply (ie: cheaper) with a character LCD - saves one having to set up a multimeter in series.

The switches, were originally meant to be mechanical, alas I didn't change the Ron/Roff in the sim. In the smaller PSU, a mechanical switch should be ok, providing its mounting right next to the sense resistors (even for a 0-500mA PSU, might as well try to remove errors as much as I can). Bigger PSU will be MOSFET's - which will add their own error off-course but only if placed between the opamps inputs..(directly in series with the sense resistor)

Thanks for the offset voltage error, I knew this would skew results, but I wasn't aware (well, I didn't calculate..) how much. 2.3mV seems to be the upper end of the LM358 which Im' using purely out of convenience here. Reckon this can be cancelled out with calibration of the real circuit?

As I have a bunch of 2N7002's I may try those for the transistor, both in simulation and real world. Higher gate threshold, but the minimum voltage I'll run the 358 at is 15v (for a max of 12V out), and to make matter more complicated, max Vout should be 4.096V for easy ADC interfacing (giving ranges 0-409.6mA, or perhaps I'll double that) I'll try to work this out on my own as I don't want to waste your time... even though I'm starting to get more questions lol

Perhaps as a thankyou, I should post a schem of a relatively simple, but 'as accurate as I can get' 0-1A PSU, with 0-12V. It all started out as a small power supply for copper plating/anodising small items (need constant current, but a voltage limit, and visa versa), but might be handy for those who need a portable digital PSU. As always, I try to go for the easiest/cheapest parts (thus the LM358) so if others wish to build they won't have issue. I'm sure you (Roff) and Eric already own good ones, but you never know :) Internet has many PSU projects, but not *that* many CC CV, which can display both voltage and current output, so I tought I'd give it a whirl.

Btw Roff, and Eric, how long have you been using LTspice/Spice sims?
 
I had tried a 2N7002 in the sim. For some reason, the model showed about 900nA gate current. I don't believe it, but I took it out because I was trying to eliminate sources of errors.
I started using spice when you had to hand draw a schematic, name or number all the nodes, and then manually generate a net list for spice to chew on. If you wanted a graphical output, it was done by printing an X, or whatever, for each data point, on tractor feed printer paper. Not sure when that was. I'm also not sure when I discovered LTspice, but it was a relatively long time ago.
 
hi BT,
I started using LTS about 2 years ago, after getting some support/encouragement from 'Roff' and also looking at his Sims.

I try just for the heck of it, to Sim posted circuits in order to practice technique and methods, I wish I had been able to have a modern version of LTS 25yrs ago it would have saved a lot of time and effort in designing.
 
hi BT,
I started using LTS about 2 years ago, after getting some support/encouragement from 'Roff' and also looking at his Sims.

I try just for the heck of it, to Sim posted circuits in order to practice technique and methods, I wish I had been able to have a modern version of LTS 25yrs ago it would have saved a lot of time and effort in designing.
I hear you, Eric. Thinking back, I built an astonishing number of breadboards before spice became available.
I used several commercial versions of spice during my career (before I retired), both before and after the introduction of LTspice. I never actually used LTspice at work.
 
I hear you, Eric. Thinking back, I built an astonishing number of breadboards before spice became available.
I used several commercial versions of spice during my career (before I retired), both before and after the introduction of LTspice. I never actually used LTspice at work.

Yikes, even though all the modern simulation software must speed up design/time considerably, I bet you enjoyed breadboarding to a certain extent :) I'm kind of young, but still not up on the latest and greatest software tools - partly because I simply cannot afford expensive software (when the free versions are more than adequate for my needs) and also because I have a real attachment to hardware prototyping. It takes much longer, which means I end up getting paids less for how long it takes me ... but I dunno, spice/software is great, I stil feel more at home with a soldering iron or poking around with a multimeter.

Might sound like brown-nosing, but it really is good to know there are very experienced and knowledgable spice users in the forum (Roff and Eric, and I'm sure there's others).
 
Yikes, even though all the modern simulation software must speed up design/time considerably, I bet you enjoyed breadboarding to a certain extent :) I'm kind of young, but still not up on the latest and greatest software tools - partly because I simply cannot afford expensive software (when the free versions are more than adequate for my needs) and also because I have a real attachment to hardware prototyping. It takes much longer, which means I end up getting paids less for how long it takes me ... but I dunno, spice/software is great, I stil feel more at home with a soldering iron or poking around with a multimeter.

Might sound like brown-nosing, but it really is good to know there are very experienced and knowledgable spice users in the forum (Roff and Eric, and I'm sure there's others).
My all-time great breadboard was a 9 bit video A/D converter with a conversion rate of 14.3 megasamples/sec. This was in the mid-70's, before integrated A/D's or spice were available. The PC board version went into a very profitable digital video timebase corrector. A picture of the PC board is attached. Its dimensions are 13" x 8.25".
The breadboard was a made up of a series of smaller ones. Each one was a major block of the full A/D. Each was built on single-sided copperclad board, with holes and pads cut for the components. As each block was built, it was tacked on to the other ones with a solder bead to join the ground planes, and wires as needed. Some of the blocks were designed, tested, redesigned, and retested through several cycles until they were finalized.
As you might guess, it was bigger than the final PC board.
 

Attachments

  • IMG_0022.JPG
    IMG_0022.JPG
    1.1 MB · Views: 252
Nice! looks neater/more modern than many of my boards.... As for stitching together multiple breaboards, now THAT is prototyping at its finest. A hell of a lot of effort, but well worth it :)
 
Nice! looks neater/more modern than many of my boards.... As for stitching together multiple breaboards, now THAT is prototyping at its finest. A hell of a lot of effort, but well worth it :)

hi,
I also use 3 regular bb's fixed to a copper clad pcb, the copper backing pcb is connected as an overall ground plane.

Along one of the edges of the back plane are terminal strips [ solder type] which a used to distribute the power rails to each bb and also has the power rail decoupling.
By ensuring good grounding and decoupling of the bb's even the most complex, fast circuits will operate reliably.

E
 
Well gentleman, I'm still working on this, and thought instead of starting a new thread which may clog up the board, I'd post something here.


Firstly, thank you both Eric and Roff! After being stumped by that opamp lock up I've removed the voltage switches to something more practical, MOSFETS. I believe the 'lock up' won't be much of a problem if I use power sequencing - may sound complicated, but this is all controlled, very basically I might add, by a micro. And given I'm using MOSFET's on both the current paths for the sense resistors, I can rpetty much turn then on or off as I please.

SO here it is, a slightly more practical version. I do have some MAX4172's, which are designed for the job. But I cannot find the spice model for these (only the MAX4173T.. similar, but not ideal). And as I wanted a fairly simple 'modular' circuit to be used in anything from bench PSU's, to small handy metering gizmos, best to stick with an opamp, albeit a rather common one with its faults.

I didn't just use a single FET per sense resistor, as this would put the measurement across both the FET and resistor. Simple enough but considering I may want to use resistances of <0.1 Ohm, the 20-30mOhm on resistance of the FET's becomes significant - as does any resistance of a relay contact, or mechanical switch (but can be calibrated out later.). Relays use power, and mechanical switches aren't as cool as solid state control :) So I used two per current path, one main one, and one to switch the 'input' to the sense resistor to the opamp. Dual P-channel MOSFET's are pretty common, and I have many many different SOIC devices liberated from a good 20 (dead) laptop motherboards I was given from a computer guy.

Here it is! Looks like a nice start to an autoranging ammeter. Given the flexibility in current sense values, it *may* be the framework for 0-10mA, 0-1A, and 0-10A, all with a resolution determined by the ADC used. That is, without using a single resistor value and using gain to achieve the range (gain amplifies opamps input offset...). Of course, the ranges can be changed for 0-400mA, 0-4A, or anything really. Probably over kill when one just has to plug a probe into a different socket on ones multimeter for the high range, but humour me :)

The opamp isn't ideal. LM358 is cheap as chips, but one with an input range up to VCC, and output down to 0.6V isn't easy to find/obtain for all. Your thoughts? too complicated?
 

Attachments

  • Isense2C4.PNG
    Isense2C4.PNG
    44.5 KB · Views: 322
  • Isense2b4.asc
    3.1 KB · Views: 149
hi BT,
Runs OK for me.

For the OPA look at the LT1006 prec OPA. [its in the LTS library]

Hey man,

Thanks again. What can I say, now THAT is an opamp! I've used precision ones occassionally (I'm mainly embedded micro's, FPGA's, and digital comms) but it seems linear tech never fail to impress. With the ultra low offset voltage/bias current, that opamp would probably do just fine for sub miliamp resolution measurements even when using a 0.1ohm resistor (one current sense element, but changing gain).

I'll pick a couple up next order, guess thats the candidate for a precision 0-20V CC/CV bench supply :) I was starting to find it tough to find an opamp like that, as some Nationsemi (now taken over by TI) ones I looked at were mainly low voltage, seemed to focus on rail-to-rail rather than low-offset.

Hopefully, if I don't lose time with other projects/work I'll post a few schems in the 'projects/ideas' forum just so folks can see if a circuit would be useful in a project, putting the 'high side current measurement?' problem to bed.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top