MCU: How to deal with common mode noise on input pin? - Page 1
Forum section: electronics / beginners

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 01:29:08 pm
content: Hello,I have a microcontroller (ESP8266 or Atmega328p) which has one GPIO pin configured as "INPUT" with external 4k7 pullup resistor.If I check the voltage of this pin using an oscilloscope I can see that it's near VCC (5v) with very low ripple/noise.A wire with open end is connected to this GPIO pin. Once I touch the open end of this wire, the GPIO pin "goes crazy".The oscilloscope shows very high noise (peak to peak approx. 4v) and this behavior continues as long as I'm touching the wire.Wire with open end? Yes, normally there is a sensor connected, but the user might unplug the sensor...I assume the (common mode) noise has something todo with the (cheap) power supply I'm using. Nevertheless, it's not a solution to replace this psu, because it's mounted in the ceiling. My circuit has to deal with it.Here is a quick video demonstration of the behavior:https://youtu.be/QMgDTJJYQGgI tried to solve this interference by putting an 100n capacitor across the GPIO pin and GND. The oscilloscope showed, that there is no more interference.As an alterantive, I found out, that a very strong pullup of 220R also solves the issue, but of course the quiescent current is way too much...I would like to know, if the approach (fixing it with a cap between gpio+gnd) is correct? Or how would you get rid of these interferences?Any comments are greatly appreciated!Thanks

author: Delta
author_rank: Super Contributor
timestamp: February 28, 2017, 01:36:46 pm
content: Are you *sure* you are using a 4k7 resistor and not 470k?4k7 should be strong enough to prevent that kind of crap.

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 01:40:09 pm
content: Yes I'm sure. I even tried with 1k5 and 1k. It was not "stable" enough. Then I tried a 220R, this was finally stable enough to keep the pin high even if I touch the open end wire. I was also impressed "how low" I had to pick the pull up....PS: In the video above I'm demonstrating the issue without pullup and with an Arduino. The real application is an ESP8266 working with 3.3v and the mentioned pullups.

author: S13
author_rank: Regular Contributor
timestamp: February 28, 2017, 02:58:02 pm
content: So i assume its a digital i/o level that you would like to maintain?You need to know that as long as the injected electric signal is of greater magnitude than your own digital io line will permit, you will always get interference. Perhaps you need to approach this from another angle? Like:- First make sure your input circuit can handle the injected signal. This means making sure it doesnt violate absolute minimum/maximum specs of the GPIO pin. Sometimes the internal clamping diodes of the GPIO pin are sufficient, other times external heavier duty clamping diodes are required (especially when you have external board signals connected to the GPIO).- Then make sure you filter out all unwanted frequencies, which is usually done with a typical low pass filter. High frequency noise is injected most easily so that will take care of some of the noise.- If your noise is in the same frequency range as your digital signal, and your signal is just too damn weak to overcome the noise, then you need to solve the problem on the other end; where the signal is generated. Either lower the output impedance of the signal, or prevent the high noise from injecting in the first place! The last one meaning that you cannot touch the signal and that it has enough shielding.Also your system could be adapted to cope with the noise (which is good practice anyway). Since its just garbage thats being received, the receiving system should be able to detect this and ignore the signal while its being overrun with noise. Most digital protocols have some sort of error detection for this purpose anyway.So there are a few options to choose from. But i guess what is most practical depends on the current implementation of your project.

author: Delta
author_rank: Super Contributor
timestamp: February 28, 2017, 03:19:47 pm
content: You must be doing something wrong with your scope probing technique and / or PSU configuration. There is no way RMI could induce 4Vpp of crap across 4k7. Have you got a decoupling cap across the supply rail and ground? Is your scope ground connect to the circuit's ground?

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 04:04:27 pm
content: Quote from: Delta on February 28, 2017, 03:19:47 pmThe device I'm talking about is a Sonoff. The power supply schematics are available here on page 2:https://www.itead.cc/wiki/images/f/f8/Sonoff-Schmatic.pdfI don't know what I'm doing wrong with my scope. The ground of the scope is connected to ground of the Sonoff board (secondary side) and the probe is actually connected with the GPIO. As you can see in the video, Vpp is approx. 4v. I also don't believe my eyes.My scope is battery powered. If I connect earth ground (from the mains plug) to the secondary sides ground, the noise is also gone...Edit:I have created a second video to quickly explain my issue. Could you please have a look at it?https://youtu.be/BBb1hH1n32UThanks

author: Delta
author_rank: Super Contributor
timestamp: February 28, 2017, 04:37:37 pm
content: Try putting a 0.1uF ceramic cap between Vcc and Gnd. Basically decouple the top of your pull-up resistor to Gnd.

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 04:48:32 pm
content: Quote from: Delta on February 28, 2017, 04:37:37 pmI have done that on my breadboard. Did not change anything.Nevertheless on the Sonoff board itself there are already a few 100nf between vcc and gnd.Or did I connect it false? Attached is a picture.

author: Benta
author_rank: Super Contributor
timestamp: February 28, 2017, 05:10:53 pm
content: You've just demonstrated how well your body works at picking up stray fields (eg, 50 Hz) from your surroundings.

author: AndyC_772
author_rank: Super Contributor
timestamp: February 28, 2017, 05:14:50 pm
content: Quote from: Benta on February 28, 2017, 05:10:53 pmThis ^^^The simple answer is, don't touch wires connected to MCU input pins. Use a connector which makes doing so difficult. Attach the male connector to the sensor and the female to the MCU.

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 05:36:53 pm
content: Sure I won't make it easy to grab the wire, but you all know end users, they might do stupid things and I just need to make sure, that my microcontroller does not do stupid things. What about my possible solution (adding a 100n between gpio and gnd) mentioned in my first post? Is this an acceptable way to filter the noise ?

author: S13
author_rank: Regular Contributor
timestamp: February 28, 2017, 06:18:32 pm
content: I think you need to realize that your system should be able to cope with garbage digital signals. The digital protocol should handle it.Just make sure your GPIO pin cannot be damaged by the injected interference and you are good to go.

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 06:26:42 pm
content: Yes in the meantime my firmware can detect a floating input and disabled/ignores the interrupts.Regarding input protection I need to implement clamping diodes, right? But shouldn't they be already inside of the MCU?Thanks

author: AndyC_772
author_rank: Super Contributor
timestamp: February 28, 2017, 06:28:36 pm
content: Quote from: ranger81 on February 28, 2017, 05:36:53 pmYou always need to know the characteristics of the noise you're trying to filter, and the signal you don't want to significantly affect.Touching the wire typically injects a relatively high voltage, high impedance signal at 50 Hz (plus some higher harmonics). You'll see the effect if you touch a scope probe tip, and hear it if you touch a line level input to an audio amplifier.The impedance of a 100n cap @ 50 Hz is given by Xc=1/2(PI)*50*100n = 31.8 kOhms. This is quite high compared to the 4k7 resistor you already have, so no, I don't think it's likely to help much.I think a better solution is to prevent physical access to the pin by careful connector choice, coupled with software which is robust enough to recognise and ignore noise with these characteristics. You might also consider adding additional ESD protection to the pin too.

author: Gyro
author_rank: Super Contributor
timestamp: February 28, 2017, 06:31:17 pm
content: Quote from: Benta on February 28, 2017, 05:10:53 pmOn the contrary, you are showing what happens when your body is theonlyground and you have significant leakage current from your power supply!.You are using a battery powered floating scope so your body forms the only ground path when you touch the input. You need to check the magnitude of leakage through the PSU, it must be significant if it is generating valid logic levels across 4k7 or less!Power off, perform resistance tests primary side to secondary. If satisfactory (ie. capacitive noise coupling only). Then grounding the secondary side is your logic 0V is the only reliable solution.Be safe!

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 06:35:33 pm
content: Quote from: AndyC_772 on February 28, 2017, 06:28:36 pmThanks for your answer, in my latest try I have removed the 4k7 resistor again and only have a 100n between the pin and GND. This seems to solve all my issues. No more noise, but the sensor is still working (if connected). Even touching the wire does not induce any noise.So actually this helps a lot and solved the issues I'm having. But is this solution robust and acceptable? I'm an electronics newbie and I would just like to confirm that I'm not doing something completely stupid...

author: S13
author_rank: Regular Contributor
timestamp: February 28, 2017, 06:45:21 pm
content: So now you dont have any pull up at all?Dont you need a pull up for a default level?

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 06:50:02 pm
content: Quote from: S13 on February 28, 2017, 06:45:21 pmIt seems not, without sensor connected the pin stays low and with sensor connected (and the PIR is triggered) the pin is high.Everything seems to be stable so far, even without pullup or pulldowns.

author: S13
author_rank: Regular Contributor
timestamp: February 28, 2017, 06:58:15 pm
content: Quote from: ranger81 on February 28, 2017, 06:26:42 pmPossibly the internal diodes of the chip will suffice. But a more robust external clamping diode could be added if needed without adding too much to your BOM. That will at least protect your GPIO pin from damage from induced noise and such.But know that for ESD pulses you could need something more than that. For ESD, high frequency filtering and/or transient suppressors are most often a must-have. But this one is tricky to verify. Just use a very robust over-done transient protection solution and assume you are "safe", or go the route of testing and verifying with an ESD gun (which is fun btw!)

author: ranger81
author_rank: Contributor
timestamp: February 28, 2017, 08:08:09 pm
content: I guess when using the 100n between the pin and gnd any additional protection seems to be obsolete. As I said, even if I touch the wire, there are no more spikes visible on the scope.Since the microcontroller board is already a commercial product (Sonoff) it's pretty hard or impossible to add any components to the circuit board itself.

author: S13
author_rank: Regular Contributor
timestamp: March 01, 2017, 08:00:48 am
content: You could still add a protection circuit between your sensor and the Sonoff board, if required. Of course its up to you to decide how robust you would like to make this circuit. If its just a one-off home project and it works according to your own requirements, then leave it as it is. If you make this for a commercial application and it works according to applicable standards, then also leave it be (but this would require a bit more rigorous testing of course).

author: ranger81
author_rank: Contributor
timestamp: March 01, 2017, 09:11:15 am
content: Regarding the protection circuit between Sonoff and Sensor board. What would you suggest?I found the attached protective circuit. What kind of diodes should be used? And the series resistor should be picked to limit the current to the max. my input pin is capable to handle, right?Thanks!

author: AndyC_772
author_rank: Super Contributor
timestamp: March 01, 2017, 09:17:29 am
content: If your pin is a digital input, it'll be high impedance provided the pin voltage is within the CPU's supply rails. If it goes outside the rails, then you can expect the current to increase dramatically, and that's a condition to be avoided.I typically use one of two techniques. Either:a) a TVS diode with an appropriate rating connected to the pin, which limits the range of voltages on the pin by virtue of its own design, orb) a pair of Schottky diodes to VCC and GND, which limit the range of voltages on the pin to whatever VCC and GND happen to be, plus or minus their own forward voltage (which is typically small enough not to be a problem).

author: S13
author_rank: Regular Contributor
timestamp: March 01, 2017, 01:47:33 pm
content: Quote from: ranger81 on March 01, 2017, 09:11:15 amYes that is a suitable circuit. You could opt to place the resistor in front of the clamping diodes and possibly add a capacitor to GND to create a low pass filter (the cuttoff frequency should of course be higher than the frequency of your signal). This would protect the clamping diodes better for ESD pulses for example. This is especially true when not using a TVS. Clamping diodes, resistors and capacitors are cheap, TVS devices are expensive.The downside is that leakage current of the clamping diode could become more of an issue because you are adding more input impedance over which an undesired voltage drop could form (just something to be aware of).For clamping diode you could try the simple BAT54, or perhaps a BAS40-04 (this one has a dual configuration inside a SOT23 package). But there are other suitable schottky diodes both in SMD and through-hole to be found.

author: danadak
author_rank: Super Contributor
timestamp: March 01, 2017, 04:57:24 pm
content: The scope shot shows you are picking up 10 Mhz ? If so whats inyour lab putting out RF, 10 Mhz.Is it a clock/crystal on the board and board grounds are damagedin the area the crystal is located ....?Or 10 Mbs Ethernet transceiver clock somehow not terminated/grounded correctly ?Regards, Dana.

author: Christe4nM
author_rank: Unknown
timestamp: March 01, 2017, 06:42:14 pm
content: For interference to happen you need 1) a source 2) a coupling path and 3) a receptor/victim. To mitigate the problem: Make sure the source becomes quiet/silent. Eliminate possible coupling paths. Immunise the victim circuit.Here are some thoughts that came up. I don’t pretend to know how or what exactly, but this is where I’d start:1) The source in this case is very likely the switch-mode power supply. In any case, it completely lacks any kind of filtering:- There is no common mode (CM) choke or better capacitor-CM choke-capacitor filter on the mains side. This means all kinds of unwanted RF easily enters the mains cables, which in turn are quite good at becoming an antenna for this kind of signals. This is your first likely source of conducted and radiated noise.- There is no proper output filter on the DC side; there is only the L-C circuit of the converter. That alone will not do at all. Again, you need at least an C-L-C filter, but rather a C-CM choke-C filter. This filter on the DC output should prevent conducted noise to enter your wires, which in turn prevents conducted noise to become radiated noise.- That the noise is measured at about 10MHz and maybe the switching frequency of the supply is way below that doesn’t say anything. The noise generated by the supply if probably broadband from 10’s of kHz into the GHz. It just so happens that you pick up this frequency content. Then again, also look around the lab for possible sources.- You could try to put ferrite clamps around the input and output wires of the supply. Alternatively try looping them a few turns through a toroid ferrite core. However, this is often done as last resort.2) Coupling path(s)There are multiple possible coupling paths in this case:- The wires between your supply and your (bread)board, as well as your scope probes. They are relatively long, and thereby great for picking up radiated noise.- The lack of grounding of your board: it should either be completely floating OR board ground connected to mains earth through as low impedance connection as possible. Instead you have a bit of both: your USB cable to your laptop/pc provides a path to mains earth, albeit one with quite some impedance. Any unwanted, coupled-in, noise currents returning over this path and thereby create a noise voltage. The rest of the circuit is floating, but becomes referenced to mains earth when you touch it. It is then that you are providing another path to mains earth, and again a bad one, so the existing noise starts to appear as a noise voltage.- I’d recommend to connect only two wires between your supply and your (bread)board: DC positive and DC negative/gnd. Twist those together so they become coupled to each other and in turn a bad antenna/receptor. Then on your board place a good filter (C-L-C for example) and make sure you only take power from that ‘new’ supply point, which should be clean(er). I’d also recommend to connect your circuit’s DC ground to mains earth. (Do remember that you DC circuit is now no longer floating, but mains earth referenced.)3) A receptor/victimThe Arduino board is quite badly laid out when it comes to proper grounding IMHO. The best way to connect a GPIO to any input is with the signal running right next to its return conductor. On a PCB this means running right over an unbroken ground plane. With wires you’d do best to twist the ground connector together with the signal wire. Ground loops are no problem if everything is properly grounded. Noise currents will then be able to take the shortest route home, and not go through parts of your circuit where they turn into interference.- Every wire going off your board needs to be filtered for RF, every single one. This to make sure outside noise stays out, and inside noise stays in. Start with a small value capacitor between signal and circuit ground right at every off-board connector.

author: ranger81
author_rank: Contributor
timestamp: March 02, 2017, 05:50:01 am
content: I'm going to create a small pcb which is attached between the Sonoff board and the sensor with the attached circuit for protecting my MCU input. Is there anything to change?I moved the series resistor 1k in front of the clamping Schottky diodes. Initially I had it after the diodes but the scope was still showing spikes up to 4.6v, so I moved the resistor in front of the clamping diodes and now the "spikes" are within the supply voltage. I guess this is correct?Thank you all for your support!

author: S13
author_rank: Regular Contributor
timestamp: March 02, 2017, 08:27:44 am
content: Moving the resistor in front of the clamping diodes does make the clamping more efficient as you have noticed. Nice to see you have a low pass filter in there as well. Its always good practice to filter out any unnecessary frequency components, especially higher frequencies since they cause the most trouble and interference.Just a fyi, there are so many variations on input protection, but this is a good starting point. There are other options like ferrite beads / chokes, tvs diodes etc which can also provide filtering and protection (also at higher frequencies) but since your application is a very low frequency interface what you have now should be a nice setup.