How do I Solve Race Conditions? - Page 1
Forum section: electronics / beginners

author: bobleny
author_rank: Newbie
timestamp: October 23, 2019, 03:39:05 am
content: Hi,I have the following circuit, and I am concerned about a race condition between Q8 and Q9.The purpose of this circuit is to only energize L1 only if VCC < ~14v DC.So, from a de-energized circuit, if VCC is connected to a 20v DC source, there is nothing to stop Q9 from activating before Q8. Thus, energizing L1 very briefly.So, the question is, is there a way to solve this issue?Am I looking at this wrong? Maybe there is a flaw in my thought process?Thanks!TMI:This is a simplified version of an overly complicated low battery cutout I designed, which does suffer from a race condition (I may have a problem).L1 represents the reset coil of a latching relay, which enables the low cutout circuit. So the relay has a tendency to "buzz" for a moment when the circuit is first energized.

author: fourfathom
author_rank: Super Contributor
timestamp: October 23, 2019, 04:04:43 am
content: Add a capacitor from Q9 base to ground to delay the Q9 turn-on. Add a diode from Q9 base to VCC (cathode towards VCC) to quickly discharge the capacitor when power is removed.What is the purpose of R16?You might want to reduce the value of R12, to get a better-defined Q8 base drive.You might want to put a diode or RC snubber circuit across the relay coil, although in this circuit it's unlikely to be needed as the switching speeds are pretty slow.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 23, 2019, 05:36:11 am
content: beside that, i think you need to reduce R15 a lot to something like 100 or 10R or eliminate entirely, there is a chance Q9 will never turned off due to leakage/divider from R13 to Q9's base, esp on very high VCC, and R15 will also slow down the capacitor discharge.

author: bobleny
author_rank: Newbie
timestamp: October 25, 2019, 05:42:08 am
content: Thanks for the help.fourfathom,I don't know what R16 was for. I built the circuit first and then made the schematic from it (I often do this with simple circuits).When I went to upload it, I asked myself the same question, but I didn't have time to pull it out and test it, so I left it in there.Also, I was too lazy to add the flyback diode to the relay (or the relay for that matter), which I'm glad I didn't because I've never heard of an RC Snubber, so I will have to look into that.Mechatrommer,That's interesting. I didn't realize I built a voltage divider. That is probably the sort of thing someone with experience sees immediately...Now that you mention it though, like R16, I see no use for it.I also noticed, now that you pointed the one out, the voltage divider at the base of Q8. That one I am not worried about through.I wanted to test the race condition with and without the capacitor.So, I built the circuit shown in race-latching.png.As the name implies, I have it configured so if the relay is energized, it latches on so it is obvious if Q9 energized before Q8.First I tried it without the capacitor by setting VCC to 17.6v DC, then manually energized the circuit about 200 times. I also tried energizing the circuit about 200 times with R12 and R13 at 10k.So, in over 400 attempts, the relay never latched.That seems a bit odd to me. Is that just bad luck, or is this circuit just not as susceptible to a race condition as I thought?Are there other commonly used techniques(?) for dealing with potential race conditions?Is there a better way to design this circuit so there is no way for there to be a race condition?Thanks again for entertaining my ridiculous questions.P.S.I also tried this circuit without the latch and without the capacitor a couple hundred times with R12 and R13 at 100k and VCC set to 12v DC so I could make sure the circuit didn't have any unexpected issues.

author: fourfathom
author_rank: Super Contributor
timestamp: October 25, 2019, 06:38:51 am
content: Quote from: bobleny on October 25, 2019, 05:42:08 amProbably the relay needs more than just a quick spike of current to latch, so even if the original circuit has a race condition it is too brief to activate the relay.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 25, 2019, 10:33:06 am
content: yup probably you are trying to solve a race condition that will never give you problem in the first place. relay will need iirc around 200ms to fully activate, the transistor race condition you talking about maybe in order of us spike, not enough to do anything to the relay coil, let alone the mechanical switch arm inside there. to see the brief spike, you need to see it through oscilloscope and then decide if it can give you a problem in the system you install it in, through math analysis or empiric study just like you have done. also make sure Q9 is not drawing significant current during turned off. maybe you think its off but actually operate in linear mode (high resistance) due to leakage, significant idle current through the Q9, non ideal for power saving, but not enough current to energize the relay, idle current of few uA should be acceptable but if you are seeing tens of mA, something is wrong. ymmv.