High side transistor calculation - Page 1
Forum section: electronics / beginners

author: crgarcia
author_rank: Regular Contributor
timestamp: August 20, 2024, 10:46:27 am
content: Hi all,I'm a total beginner in electronics (self-taught), so any advice is really appreciated!I’ve put together this circuit to sense mains voltage using an IO Expander, which I’ll connect via I2C to an ESP32.Based on the MCP23018 datasheet:Here’s what I understand:A low signal should be less than 0.2 * Vdd = 0.2 * 3.3V =0.66VA high signal should be more than 0.8 * Vdd = 0.8 * 3.3V =2.64VFrom the mains side, I'm expecting:Current: If = V/R ≈ 300V/300kΩ ≈ 1mAAccording to the LDA210 datasheet:I expect the voltage drop across the optocoupler to be less than 0.6V, meaning Vce should be around 2.7V.I’ve built the circuit, and it’s working! The MCP detects a '1' when mains are present and '0' otherwise.These are the readings from my oscilloscopeAvg: 2.72vBase: 2.68vMin: 2.41vIt takes about 200ms to switch from high to low, which is fine for my use case.Now the questions:1) Even though it seems to works on the bench, I feel I am too close to the lower limits of the MCP. Is there a better way of doing this?I considered high-side switching, but I think I’d run into the same issues.2) My test setup is a small workbench, sensing a 20cm wire. I am wondering what would happen if I use much longer floating wires (like in a normal house installation).Will the optocoupler “pull down” to earth? Should I lower the resistance to increase the 1mA current reading? (My guess is that this wouldn't really help)3) Is the ripple I’m getting normal for a household circuits? Or I should increase the capacitance? (Not a problem if discharging the capacitor takes too long)Extra links:Full Schematics:https://github.com/crgarcia12/electronics-homeassistant-lightscontroll/blob/main/PCB/schematics-v23.8/readme-media/board-schematics.svgLDA210 datasheet:https://www.mouser.ch/datasheet/2/240/media-3322391.pdfMCP23018 datasheet:https://ww1.microchip.com/downloads/en/DeviceDoc/22103a.pdfThanks a lot for taking the time to read this long post

author: PGPG
author_rank: Frequent Contributor
timestamp: August 20, 2024, 01:03:24 pm
content: Quote from: crgarcia on August 20, 2024, 10:46:27 amHere is your main mistake.If you have voltage drop across the optocoupler output 0.6 then for this chart you have Vce=0.6.When optocoupler is switched off its Vce=3V3.When it is switched on it is trying to make its Vce being 0. But at chart you see that around 1V problems begin and it is hard to make Vce much smaller than 1V.Look at internal optocoupler schematic.I don't know what is the minimum voltage drop when optotransistor is saturated. It may be about 0.2V.After optotransistor you have second, this time standard, transistor. For this second transistor to start conducting small current you should expect Vbe=0.6V and for higher current about 0.7V.So the max (meaning min voltage) what optocoupler can reach at its output is about 0.2V + 0.6V so around 0.8V. What you see at this chart.So when optocoupler is on you will get at its output max about 3.3-0.8=2.5V while you counted that minimum you need is 2.64V.2.64V is the guaranteed level. Practically for your lower (2.5V) input IC also sees it as being 1 and it works but it is wrong design.1. You can use different optocoupler.There are such that don't have two transistors, but they have lower CTR (Current Transfer Ratio) so you need higher current in diode.There are such with digital circuits inside so you get regular digital output from them (close to 0V and close to 3.3V).2. You can add 1 transistor amplifier after your optocoupler. So your optocoupler will drive current to its base and its emitter will be connected to GND and collector by resistor to 3.3V. So at its collector you will be able to have both 0V and 3.3V.

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2024, 01:30:54 pm
content: Yes, in order for this kind of opto-coupler to work, a small transistor amplifier is required.The resistors bias the transistor on, when the opto-coupler is off, which pulls the MCU input down.When the opto-coupler is on, thetransitor turns off, and the MCU input is pullled up to +V.The two 10k resistors between the opto-coupler and 0V form a potential divider to ensure the voltage on the base of the transistor is below 0.6V, when the opto-coupler is on.

author: PGPG
author_rank: Frequent Contributor
timestamp: August 20, 2024, 04:17:19 pm
content: I'm not sure if this circuit will work.Depends on how long will be breaks in optocoupler being on when AC is crossing 0.The problem is that transistor can discharge C45 very fast while 10k loads it with 220 ms time constant.If the task is to detect AC than if there is no AC circuit can have long time constant because nothing interrupts this stage.When AC is present because of AC being not DC we will have breaks in detection so detecting voltage should fast load the capacitor, and breaks should have slow effect.It simply have to be designed that way that when optocoupler is on then transistor is on and here it is opposite.

author: Xena E
author_rank: Frequent Contributor
timestamp: August 20, 2024, 05:01:50 pm
content: One way to implement this is to allow the optocoupler to transition on zero crossings but use it to reset a monostable, that can have a timed period of whatever multiple of the mains half cycles you want and still give clean switching.

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2024, 05:39:49 pm
content: Quote from: PGPG on August 20, 2024, 04:17:19 pmYes, I overlooked the fact it changes the behaviour, with the transistor pulling down, rather than up. Connecting it to +V should change that, but I would get rid of the capacitor altogether and change the code if necessary.

author: PGPG
author_rank: Frequent Contributor
timestamp: August 20, 2024, 11:17:59 pm
content: Quote from: Xena E on August 20, 2024, 05:01:50 pmIt is exactly such done in original project.On zero crossing optocoupler switches off and then on.Each time being on it resets monostable (fast charges C45).Monostable is build with C45 and 10k and U12 digital input.In lack of reset signal after timed period (depending on RC time constant and digital input changing state level) the output stage is switched.Only one thing really need correction. Fast charging should be with key (transitor) having drop voltage of 0.1V and not 1V.This can be done with npn or pnp.In original project when optocoupler is on capacitor is charged from 3V3.Using npn when optocoupler is on you will charge capacitor from GND (discharge it) - so logic output will be inverted.Using pnp when optocoupler is on you will charge capacitor from 3V3 so the logic output state will be the same as in original design.I intentionally left the task to draw new schematic version to OP.

author: Xena E
author_rank: Frequent Contributor
timestamp: August 21, 2024, 07:58:13 am
content: Quote from: PGPG on August 20, 2024, 11:17:59 pmYes, i understand thank you.However the basic system the op uses, timing is poorly defined as well.If the system is adequate for the op, I would also agree that it needs to discharge the resistor/capacitor network on removal of the input.I suggested a dedicated mono as the cost would would possibly not be much greater than a discreet solution.Regards,X

author: PGPG
author_rank: Frequent Contributor
timestamp: August 21, 2024, 10:16:12 am
content: Quote from: Xena E on August 21, 2024, 07:58:13 amAgree - time is poorly defined, but in this application it clearly need not be perfectly defined.Association with AC and monostables and poorly/perfectly setting time period.Back in 70s together with my brother we made a darkroom clock (if someone doesn't know - a device for measuring time when exposing photographic prints).We used AC 50Hz as the time standard. Steps:- full-wave rectification - we have 100Hz,- monostable flip-flop (2 transistors) with properly set time - divide it by 5 - we have 20Hz,- monostable flip-flop (2 transistors) with properly set time - divide it by 5 - we have 4Hz.We didn't had oscilloscope those time and had to set relatively perfect time periods to skip exactly 5 pulses and then have enough time to reset internals to be ready for next 5 pulses time. We divided output frequency into human observable and then find capacitor values when counting changes from 5 to 4 and from 5 to 6 to then use medium value.Based on 4Hz we then binary (dividing by 2 flip-flops (2 transistor) need not regulation) set the time with a resolution of up to 1/4 second.Of course monostables were different that this what you suggest to use here - pulses coming at input during time period were ignored (time count was not reset).

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 21, 2024, 11:44:32 am
content: Quote from: crgarcia on August 20, 2024, 10:46:27 amThat is always a concerning combination, but nannying is also not good advise.I see you put all the mains voltage on a sturdy plank. That looks reasonable, but I would prefer some extra distance or barrier between the mains voltage and your arduino circuit.In some countries there is a clear difference between "neutral" and "live". Here in Europe we don't make that distinction and treat both wires as if they are live. This avoids a lot of silly mistakes. I don't know how they do this in Argentina.Normal small resistors are not rated for mains voltage. You can easily fix this by putting multiple resistors in series. Best is to use 4 resistors. Two in the "live" wire, and two in the "Earth" wire. This also makes it safe if you mistake the conductors. put the power plug in reverse etc. With 4 resistors, (each 82k or 100k) the circuit is safe enough to touch. You will feel it, it may prickle or hurt a bit, but it won't kill you.It's already mentioned, but C45 is much too big. It has a time constant of: 1e4*22e-6 = 0.22s Omitting it is better. If you want to use it for filtering, then the time constant should be less then the mains frequency. Something in the order of 100nF is better suited.

author: PGPG
author_rank: Frequent Contributor
timestamp: August 21, 2024, 05:23:52 pm
content: Quote from: Doctorandus_P on August 21, 2024, 11:44:32 amWith 100n*10k=1ms time constant with pulse from optocupler every 10ms (100Hz) he will get at output 100Hz pulses why he wants to have constant state.

author: crgarcia
author_rank: Regular Contributor
timestamp: August 21, 2024, 09:35:52 pm
content: Thank you very much everyone for the help!Quote from: PGPG on August 21, 2024, 05:23:52 pmExactly, My goal is to detect whether the lights at home are on or off using Home Assistant. A delay of 200ms or even 500ms isn't an issue for me. I added that capacitor to get an output as constant as possible. I will try with a smaller one as well.I have 8 identical channels. To reduce PCB space, I’m now considering using a voltage level shifter instead of the original setup. Here’s the plan:I’ve been looking at theTI TXB0108 voltage level shifter (datasheet link).Here’s what I’ve gathered from the datasheet:-Input (Port A)I’m planning to supply Port A with 3V (instead of the usual 3.3V) by using a voltage divider.-High level input for Port A≥ VCCA* 0.65 ≥ 3V * 0.65 ≥1.95V-Low level input for Port A≤ VCCA* 0.35 ≤ 3V * 0.35 ≤1.05VRegarding the output (PortB)-High level output≥ VCCB- 0.4 ≥ 3.3V - 0.4 ≥2.9V-Low level output≤0.4VThe questions- What do you think on using a voltage level converter? Would that be a good design?- Any other IC I should consider instead?The full schematic of the solution now:https://github.com/crgarcia12/electronics-homeassistant-lightscontroll?tab=readme-ov-file#version-23-eight-channelsThank you very much everyone for your help!---PS----I tried to simulate your circuit, @Zero999 and apply your proposed changes @PGPG. Unfortunately I didn't succeed- I either never reached 3.3V, or never went to 0VSimulation:linkReading a bit more on the tips you gave me, I also saw that it might be a good idea to try out with a MOSFET.And, as a curiosity (at least to me) this is how the LDA210 (optocoupler) behaves:

author: PGPG
author_rank: Frequent Contributor
timestamp: August 21, 2024, 11:57:50 pm
content: Quote from: crgarcia on August 21, 2024, 09:35:52 pmYou understand electronic circuits less than I thought. You made from pnp voltage follower and not amplifier.In the circuit on the right do the following:1. Reconnect pnp base to other side of optocoupler output (where it is connected with 1k).2. Change 1k into 10k.3. Break connection between emiter and capacitor.4. Disconnect collector from GND.5. Connect emitter to 3V3 (replace 100 with wire).6. Connect collector to capacitor.7. Change 1M to 100k.