Simulating a photo interrupter in Ltspice - Page 1
Forum section: electronics / beginners

author: mercurial
author_rank: Regular Contributor
timestamp: August 27, 2025, 01:48:02 pm
content: Hi,I wanted to simulate a photo interrupter in Ltspice (slotted optocoupler). I did some searches online and found this could be done with the F component. The gain will simulate the light falling from the led on the transistor.I used the "F" component (CCCS Current controlled current source) in Ltspice.I mapped it to a voltage source that is in series with my led component and the current source is controlling a NPN transistor between B and E.This part is fine so it mirrors the current passing thru the led.Now I wanted to control the gain of the "F" with another voltage source. So I set up a voltage source to output a triangle wave and labeled it Vtri.Now in the "F" component I tried to set the value field toF=V(Vtri) but this fails with errorsalso tried this {V(Vtri)} but this didn't work too.I' looking at varying the Gain with the triangle wave so I see the behavior of the photo-interrupter. I don't want to do .step/.param since that creates a new simulation per step.

author: Ian.M
author_rank: Super Contributor
timestamp: August 27, 2025, 03:11:34 pm
content: See "B. Arbitrary Behavioral Voltage or Current Sources" in the help.A BI source can have an arbitrary expression like you are trying e.g. I=I(Viled)*V(Vtri) which is equivalent to the unimplemented 'F' CCCS with variable gain V(Vtri).N.B. components controlled by complex arbitrary expressions can be computationally very expensive, resulting in slow simulation, especially if the expression has a discontinuous first derivative.

author: Zero999
author_rank: Super Contributor
timestamp: August 27, 2025, 07:53:39 pm
content: Here's an example of a photovoltaic opto-coupler model. It's for using an opto-coupler to control the brightness of an LED PSU model from 5V PWM. It's isolated and the J-FET means the output is normally closed, so the LED turns off, when the 5V signal is removed.

author: mercurial
author_rank: Regular Contributor
timestamp: August 28, 2025, 03:41:41 am
content: Quote from: Ian.M on August 27, 2025, 03:11:34 pmWhen I check the docs for F sourcehttps://ltwiki.org/files/LTspiceHelp.chm/html/F-device.htmI see 3 alternative syntaxQuoteThe second one does say value = {<expression>}

author: mercurial
author_rank: Regular Contributor
timestamp: August 28, 2025, 03:53:47 am
content: Quote from: Zero999 on August 27, 2025, 07:53:39 pmDoes this model the light falling... on the phototransistorDoes it have the ability to control the light... sorry, I cannot view the file currently.. I will play around with it and see how it works.. thanks!

author: Ian.M
author_rank: Super Contributor
timestamp: August 28, 2025, 06:37:10 am
content: Quote from: mercurial on August 28, 2025, 03:41:41 amYes, but its simply an alias of a B (arbitrary behavioral) source. Its no longer a CCCS with a controlling current probe voltage source, and the expression is not simply the gain. Of course you can *make* a CCCS using a BI source, if the expression is probe current times gain, but why would you obfuscate that by using the second F source syntax rather than explicitly using a BI source?I don't know where Zero999 is going with his photovoltaic optocoupler model - it seems to be only vaguely connected to your problem.It may be instructive to consider LTspice's 4N25 optocoupler model and see where one would add variable light transmission between the LED and phototransistor to convert it into a slotted optosensor.Code:* Copyright © Linear Technology Corp. 1998, 1999, 2000. All rights reserved.*.subckt 4N25 1 2 3 4 5R1 N003 2 2D1 1 N003 LDG1 3 5 N003 2 .876mC1 1 2 18pQ1 3 5 4  NP.model LD D(Is=1e-20 Cjo=18p).model NP NPN(Bf=610 Vaf=140 Ikf=15m Rc=1 Cjc=19p Cje=7p Cjs=7p C2=1e-15).ends 4N25That's modelling the LED on the input side as a 2 ohm resistor and a custom diode LD in series, with the resistor used for current sensing, and also 18pF of capacitance across both.On the output side it has a custom NPN transistor model NP, with all pins externally accessible (and an explicit substrate connection to the emitter), with the photo current in the base collector junction modelled by a voltage controlled current source across it, using the control voltage developed across the 2 ohm resistor on the input side.Replace G1 with a BI behavioural current source, with an expression like I=V(N003,2)*.876m*slot where slot is whatever you want to use as the transmission in the slot, (0 to 1) and you've made a slotted optosensor that is fairly realistic.It (and the original 4N25 model) makes the assumption that the photocurrent is linearly proportional to the LED current. If you have a better model for the LED's optical output and the phototransistor's sensitivity vs illumination you could incorporate their combined effect in the BI expression, possibly using a table() function for a piecewise linear approximation.N.B. LTspice optimises away subcircuit terminals with no internal connection, so if you want to express the slot transmission as a control voltage used in the BI expression, provide a high value resistor in the model to load that pin! Seehttps://www.eevblog.com/forum/beginners/simulating-potentiometers-using-ltspice/msg2373096/#msg2373096

author: Zero999
author_rank: Super Contributor
timestamp: August 28, 2025, 07:10:22 am
content: Quote from: mercurial on August 28, 2025, 03:53:47 amIt models a photocoupler. The photovoltaic cell is a stack of diodes to limit the voltage in parallel with a behavioral current source. The emitter is a stack of silicon diodes to give the same forward voltage drop as an LED.If you want to model a plain phototransistor, then you just need a behavioral current source connected between the base and emitter of a transistor. The current source can be controlled by a voltage or current which is directly proportional to the light.

author: mercurial
author_rank: Regular Contributor
timestamp: August 31, 2025, 04:28:26 am
content: Hi,Is there any other way to get this done, behavioral sources take additional time to compute and that somehow limits fast iteration.

author: Ian.M
author_rank: Super Contributor
timestamp: August 31, 2025, 07:26:39 am
content: Quote from: mercurial on August 31, 2025, 04:28:26 amWhy do you need to model a slotted photosensor at this level of silicon detail? Unless you are doing something like trying to design an ultra--low power pulsed sensor, all you usually need to do is model the output signal.Anyway, the difference in time to simulate using LTspice's default solver and SPICE options is negligible unless you are doing something pathological with the optical path parameter, or have *far* too many slotted optocouplers in your circuit!