MCU driving 12VDC Relay - 220V weirdness - Page 1
Forum section: electronics / beginners

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 13, 2021, 04:33:57 pm
content: First time having to use a relay, driven by an MCU output IO. Not exactly a good start and less than anticipated resultsJust want to open & close my garage door (220V) using my phone and BT...What's up with all that spikey AC being feed back to the 3V3 rail when the relay switch both ON & OFF?? Am I dealing with a 'non-isolated' relay here (PN: HF32F/012HS) ?

author: capt bullshot
author_rank: Super Contributor
timestamp: January 13, 2021, 04:49:00 pm
content: Most probably it's coupled into the GND wire of your scope probe. Can you show a photo of your setup?

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 13, 2021, 05:19:03 pm
content: ah man not to be rude or anything but 'the set-up' is just a freak show right now, so I'll omit that (regardless, see attached for your entertainment)just unsoldered that HF32F and used a SSR with jumpers and the 3V3 rail is as flat as it possibly can when switched!!!Guess those mini-fancy-affordable HF32 relays are to be avoided... (wtf) :/Anyone can recommend similar HF32 sized relays/models (ones that work, preferably)? I seriously can't afford the real-estate of those large SSR... heavy too

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 13, 2021, 06:54:48 pm
content: Hey guys sorry to bring this up again but I've just cracked open one of those wicked small relay and they are indeed isolated, or seems like anyway (first pic).Second pic is where it's fitted on the PCB with the third picture demonstrating how a beginner, although handsome, route some high current traces. Well, 1.5A is consider pretty high powered in my benchtop's normal line of work lolQuestion: Does this AC noise convert into lightning and wirelessly jump over my 3V3 rail?! Joking aside, can severe noise be my issue here?? What else do I need to look for...Thanks

author: capt bullshot
author_rank: Super Contributor
timestamp: January 13, 2021, 08:26:30 pm
content: So first, is you circuit working as desired and the noise on the 3V3 rail visible on the scope is your issue?In general your relay driver circuit looks OK, layout could be better. Especially in terms of mixing mains and low voltage tracks. Keep them as far apart as possible and don't run LV traces through mains areas and vice versa. Tracks look fine for 1.5A and wouldn't require tinning. One could add a snubber circuit in parallel to the relay contact to dampen the ringing (depends on the load).The ground clip of the scope forms a quite large loop in your setup. Most probably this is the reason why you see this noise on the scope, it's not there or significantly lower within the circuit. Use the proper probing technique like this:https://www.electronicspecifier.com/products/power/oscilloscope-probing-techniques-for-measuring-power-supply-ripple(random search engine hit).

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 14, 2021, 06:43:23 am
content: Thanks for the great advice and for confirming the relay circuit is adequate, Capt1. The whole circuit is working flawlessly with no or very little noise (more or less 70~80mV). That only if the MCU GPIO do not go high and switch the relay ON or OFF that is. When the relay switches, the MCU crash, the debugger disconnect and something tells me this nasty EMI/spike/burst/whatever is felt around the world and create floods and earthquakes all over (apologies about that).2. Snubber is a good idea. I've just finished reading about its damping properties and think it could work and probably advised regardless (thanks). Before, however, I've put two other similar sized relay parts on order and will try those to see if that HF32 relay is the issue here. Reason I suspect the relay is because I've tested this with a SSR and it was noise-free and working just fine. Something tells me the relay contact is not strong enough and bounces (physically/mechanically) and thus creating this rather weird spikes (btw those spikes are random shaped on the scope when the relay switches; occasionally higher than others, longer, shorter, its all over).3. Ah! The pro measuring technique. Yes you are right, I should put the extra energy and clip-on that spring tip. Although the MCU and debugger are very capable to see the mess better than that probe, they both crash simultaneously when the fault occurs (17V~(neg)-4.5V, no kidd). The scope was just used to confirm what was happening in this case.I'll update/close this Customer Rant Support Ticket once I've received the new relays and confirmed my rookie mistake; sourcing low quality relay parts that is :/ Let's hope its only that, fingers crossed

author: capt bullshot
author_rank: Super Contributor
timestamp: January 14, 2021, 07:05:01 am
content: Quote from: Mecanix on January 14, 2021, 06:43:23 amI don't think the particular relay type is the issue. Any mechanical relay will create interferences like that.Your problem really is an EMC issue, the relay part of your circuit is disturbing either the power supply or the MCU. Not so uncommon to be seen.Does the circuit work if the debugger isn't connected? Is there a difference if the load is connected or not?Some things come to my mind:- Check with debugger connected / not connected- Check with load connected / not connected- Look at your power supply decoupling and GND planes whether they're appropriate. (2)- Try the snubber across the relay contact to dampen the source of the interferences.- Run a short and massive "earth" wire from circuit GND to a solid GND point on your computer, e.g. the shell of a DSUB connector. (1)- Connect your circuit GND to earth (either directly or through a capacitor)- Place one or more clamp-on ferrite cores on your debugger cable- Often an Y-type capacitor placed at the right nodes helps (try isolated circuit GND to primary side of the SMPS - e.g. using a 2.2nF Y-capacitor - using the correct capacitor is quite important for your personal safety)(1): Why do I recommend doing so: I've seen more than once USB and debugger to target connections not being able to deal with the slightest common mode interferences, often due to poor shielding / grounding. Bypassing that path with a short wire tells you a story. "Short" is in the ballpark of 10 ... 20cm wire length here.(2) This is one of the most often seen rookie mistakes: Not making a solid enough GND plane and / or poor decoupling.Your layout (especially the mix of mains / LV traces to be seen in one of the pictures) is a potential source of common mode interferences from mains potential to your circuit. As you've experienced, these happen when the relay contact opens or closes.

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 14, 2021, 11:07:34 am
content: Quote from: capt bullshot on January 14, 2021, 07:05:01 amOk, at the risk of being an embarrassment to the entire industry I'll admit I've screwed up here. Thanks for the heads up on things I need to rule out, and that 22uF I've taken out is/was one of them; the "inrush current" and spikes are down to half and the MCU and debugger sorta complies now, although I had one crash since so its a WIP. You were right, looks like the problem is not the relay and more how I've designed the 220V -> 12V > 3V3 rails.Ah man this is getting out of hands... missing the simple 5v input ->3V3 LDO circuits I'm familiar with now I tell ya :/Let me drill down your list of other things to check and I'll update on the progress. So kind to help me out with this, Capt

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 14, 2021, 11:19:59 am
content: Attached also for your entertainment (lol). Let me know if you see anything apocalyptic in there, apart from the 10uF and 22uF on the LDO (I'll get this dialed-in in a few hours and see how it goes...).

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 14, 2021, 02:57:43 pm
content: Quote from: capt bullshot on January 14, 2021, 07:05:01 amok ok I surrender,You WIN, CaptEMC it was, plenty, apparently.At last I've replicated the AC/relay switching remotely onto a breadboard, chopped the traces from under the PCB and fun-routed all that using poor-man shielded cabling - to find out the circuit is behaving perfectly fine and accomplishing what it should be doing without upsetting anything.So. What's next? Trash this 220VAC -> 12VDC -> 3.3VDC sexy board idea and replace with 2 (two) separate boards? One for the AC part and the other for DC with some sort of shielded plate in between? Or have a gigantic PCB designed with traces half a kilometer apart?Lastly. Had to go rant at her self-taught-certified-master electrical engineer and she swears by adding an optocoupler between the MCU's GPIO and the transistor. How relevant this solution is to my issue?

author: capt bullshot
author_rank: Super Contributor
timestamp: January 14, 2021, 07:06:42 pm
content: Quote from: Mecanix on January 14, 2021, 02:57:43 pmNot at all. If the rest of the layout stays the same, the optocoupler won't help at all.If the layout is done right, it will work with roughly the same board size as your current design.Get some lecture about EMI through inductive and capacitive coupling. Or just do what looks natural: Keep the "evil and dangerous" 230V away from your harmless and defenseless 3V3 circuitry. Divide the board into a 230V area and the low voltage area, make a border between these two areas. Place the relays and the power supply module crossing that border, but nothing else. No traces can cross that border. None. Make that border at least 3mm (better 5) wide, no traces allowed within that area (this also takes care of electrical safety, in professional design one would have to do this anyway for said safety).Then minimize the loop areas of the 230V switched circuit by running these traces near to each other (but still keep about 1.5mm distance between L, switched L and N. Your layout has a large loop for the N return from the load connectors as far as I could see, avoid this.The same rule (minimize loop area) goes for the low voltage side: e.g. run supply (and signal too) lines near to their returns (or have a solid ground plane to provide the return path).Look at the photo of the PCB bottom above: One can see one and another low voltage traces running parallel to the tinned mains trace. Don't know what this trace is, but it hasn't a proper return path (the GND plane isn't a plane here, rather two different ones connected somewhere else), and runs in parallel for some cm to an interference emitter. This is a perfect scenario for coupling EMI into your circuit. At least ensure your GND plane is closed around all traces, and keep "evil" and "harmless" orthogonal to each other (not parallel).

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 14, 2021, 08:41:56 pm
content: Evil, Satan & other high-speed Witches seems appropriately defined in this ee-vocation :/ Let me trash this superb and highly-rookiefessional pcb/network and start all over again. Once that lecture and EMI/EMC knowledge sink'ed in this time, ofc.I'll update the thread with pcb iteration #2, hopefully making it worthwhile for you since you've taken some of your time to guide me in the right direction (thanks, btw). Need a break first, let me go watch TV for a day and I'll jump back on this green satanic board asap.brb

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 14, 2021, 08:54:06 pm
content: Attached for your profound amusement, Captain. And as a souvenir for myself! You have to give me credit for having rig this layout in a 4~6hrs late night thoughps: R+ and R- labels/overlays on those front and rear connectors are input are 10k NTCs.

author: capt bullshot
author_rank: Super Contributor
timestamp: January 15, 2021, 09:50:23 am
content: That's quite a good example of how not to do it, indeed

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 17, 2021, 01:08:36 pm
content: Hey Captain, hope you are enjoying your weekend. Thought I'd update this thread and yourself on the progress, we all strive for profound entertainment with this lockdown so here it goes;1. From the lot of switching devices I've received I've settled on the SSR type. So no more 1946 era mechanical relay style contact arc'ing, tear & wear and potentially end of world situations. Nice little kit those SSRs; zero-crossing (resistive load only though), no heat on constant 3A (or un-noticeable), built-in snubber circuit, 500Hz switching capable and all that good jazz.2. Trashed the first iteration of the pcb. That's gone now, farewell, thanks to you (lol). Started working on the second iteration and following your advice about keeping Evils & Nasties inside that 220V boundary. Nothing else crosses it.... well, in a perfect world anyway.3. Coffee4. Get the MCU and logical part of the circuit fitted.Let me know if you are impressed, or more disappointed ;-) Have a good one man, and thanks again!

author: capt bullshot
author_rank: Super Contributor
timestamp: January 17, 2021, 05:31:40 pm
content: Your layout looks like a good starting point.I don't know if you treat your mains wires interchangeable (both could be L or N). Anyway, if you label one of them "L", it would be better to place both the fuse and the SSR in this line, not having the fuse in L and the SSR in N.I don't know your particular SSR type. Some types are available in the same package and pinout as a relay (I vaguely remember such from Omron), so you could test both with the same layout.

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 17, 2021, 07:11:46 pm
content: Thanks, Captain. Was left under the impression it didn't really matter whether L(brown) or N(blue) as they can be reversed, with earth tied to N, if I'm not mistaken. I've labelled them as such for ref although they really should be both labelled AC. I could be seriously wrong here, I'll double check to ensure I'm not sending my entire continent into a blackout when plugged. #EvilsHappy to swing this over the L line, if this is how its done standard/schematic-wise, you know?Attached with labelled routing (ignore the WIP noise in the 12V/3V3 area, I'll rig this up later)

author: capt bullshot
author_rank: Super Contributor
timestamp: January 18, 2021, 06:41:18 am
content: Looks like a two layer board?I'd strongly recomment routing GND and VCC first, ensuring all GND pins are connected as short as possible together. You can't count on polygon fill to fix it all up, it'll leave you with a lot of badly connected islands instead of a useful GND plane.Your WIP noise is a good example, the (green) traces block a large area that should rather be GND.

author: srb1954
author_rank: Super Contributor
timestamp: January 18, 2021, 10:54:59 am
content: What are those ceramic caps sticking up in the air with the long leads?If these are supply decoupling caps their effectiveness will be reduced by the inductance of those long leads. The most important thing with decoupling caps is to minimise any inductance associated with them by using the shortest possible tracks and connecting them as close as possible to the IC that they are decoupling.

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 18, 2021, 01:14:48 pm
content: Quote from: srb1954 on January 18, 2021, 10:54:59 amhaha well spotted! Those two added caps are there to please the ADC, essentially cutting noise from those NTCs. Added one 10uF for each NTC, works now...Added them in the schematic and part of the design now (C2 & C3).Quote from: capt bullshot on January 18, 2021, 06:41:18 amCorrect! As cheap as they come. Once I have this prototype 'half-working' I'll swing the final version onto a 4-layers pcb and get that RF network tuned up by a friend. Hopefully it won't require a 3rd prototype iteration and move on 4-layers right after this one (thanks to your help btw, Captain). That's the plan anyway.Quote from: capt bullshot on January 18, 2021, 06:41:18 amTrue that lol For those high density 2-layers I normally have the back plane powered and the top one grounded. I just flood the whole thing with vias and have the rules satisfied. Your approach of populating power & ground first is certainly the best. Anyway... I'll shoot this version out as-is; see if the SSRs are doing their job and see if that debugger is less-crashing. Should be sufficient to get me to write the firmware until that 4-layer gets finalized.I'll update, and show-off that "final" Rookiefessional version as soon as this gets done!

author: capt bullshot
author_rank: Super Contributor
timestamp: January 18, 2021, 01:40:06 pm
content: Is this a copper trace around the 230V part? You should remove this before you order the board.Nothing wrong with a two layer board, usually I place GND on both sides and stitch them together using vias.Anyway, I wouldn't do more than one revision for a simple board like this (except the antenna, that's not part of my expertise). Too much hassle ordering and populating all the revisions.

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 18, 2021, 02:05:13 pm
content: Quote from: capt bullshot on January 18, 2021, 01:40:06 pmOh :/ Man thank god you're helping me out with this, can I offer you anything in return, Captain? And yes, it is copper. well it was... I've removed it now.Quote from: capt bullshot on January 18, 2021, 01:40:06 pmSimple board, hmm "for you" it is. I'm neither a RF guy, even less electronics so I'll have to send this out to someone who knows what he's doing for the network matching/tuning. PS: mechanical engineer (13y/exp) here; specializing in CAE with a whole lot of CAD/CAM. Small company, 6 employee including Mrs Mecanix and myself in the team (PLM solutions provider). EE/electronics is outsourced, and still is, however I can see some hope we'll be able to bring this in-house before year 2085 lolI'll update. Thanks for everything again, Captain. Top man... I owe you

author: Mecanix
author_rank: Frequent Contributor
timestamp: January 24, 2021, 01:14:42 pm
content: Hey Captain, sweet, you've fixed it... working flawlessly now!!!Had the live debugging ON all while the SSR switching 1.85A @ 1Hz for nearly an hour now and all is looking good.My tinnitus is gone, my phone's 5G works again, and no more earthquakes and flooding on my continent... we've conquered Evils and got'm trapped! hahaAnyway. Time to go write some code and a firmware, finally. Just wanted to stop by and say thanks again man

author: capt bullshot
author_rank: Super Contributor
timestamp: January 24, 2021, 06:35:52 pm
content: Quote from: Mecanix on January 24, 2021, 01:14:42 pmBonus points for that. Please send me one to bring some progress into my total world domination plan.