gnea / grbl

An open source, embedded, high performance g-code-parser and CNC milling controller written in optimized C that will run on a straight Arduino
https://github.com/gnea/grbl/wiki
Other
3.99k stars 1.59k forks source link

Limit Switch Wiring #96

Open tklus opened 7 years ago

tklus commented 7 years ago

Good Morning all, Would it be possible for someone to add a page to the Wiki that explains the correct way to wire NC limit switches. maybe with a schematic drawn up. I have been confused about this for some time and there is limited info out there that I have seen that shows how to properly connect NC limit switches to GRBL. also it would be nice to know what resistors are required as well. and what GRBL settings need to change.

I had limit switches set up NO and had nothing but problems with false triggers. I would like to get limit switches working in the NC setup.

Thanks! Tim

chamnit commented 7 years ago

@tklus : I'd like to see this as well, but I'm not an electrical engineer and don't want to give bad advice.

f0m3 commented 7 years ago

Well. i just connected my switches between the pin 9 ,10,12 and ground. No resistor needed because off the internal pullup. Maybe the switch is false triggered because of motor wire induction. I soldered in some small capacitor to handle this.

tklus commented 7 years ago

and where did you install the small cap? schematic?

f0m3 commented 7 years ago

I think i did it somehow like this (i cannot go to my nc right now to check it ):

All you need to do for 100% perfect limit switch operation with Zero false triggers is: 3 small capacitors. For each capacitor, connect the negative leg to the ground rail, and the positive to one of the limit sense pins on the controller. On an arduino running GRBL these are pins 9,10, &11/12 (depending on grbl version). For Shapeokos I have found that a .47uf is perfect in all instances.

source: http://www.instructables.com/id/End-Stop-Limit-Switch-Problems/?ALLSTEPS#intro

f0m3 commented 7 years ago

BTW: some people add the capacitor close to the switch, which is useful to debounce the switch. I would prefer to have it close to the arduino to reduce noise AND to debounce. Whenever the switching wires are running close to the motor wires they will pick up some induction and this might be large enough to trigger the limit pin.

tklus commented 7 years ago

something like this? limit switches

luben111 commented 7 years ago

The best solution is to add opto couplers (like TLP185 http://uk.farnell.com/toshiba/tlp185-y-se/optocoupler-phototrans-3-75kv/dp/2524262):

langwadt commented 7 years ago

capacitor close to the MCU pin, series resistor to the switch, it offers a bit of protection against ESD and makes a lowpass with capacitor. A pullup sized so that the switch current at least a few mA, the low impendance is less senstitive to interference and most switches need a minium current to be reliable

gerritv commented 7 years ago

You might want to read this about debouncing: http://www.labbookpages.co.uk/electronics/debounce.html Also:

tklus commented 7 years ago

@luben111, that is a nice shield. are they available for sale?

luben111 commented 7 years ago

@tklus, I could organize some small production of these modules. Let me know do you have interest, I'll calculate what I could achieve as price for assembled module and for a kit.

This is not the last version of the board. The new version has hardware ENABLE protection - after power up the Enable signal stays non active for 1.5s (independently from Arduino board) to avoid spontaneous motor movement. Sometimes after power up you can hear the machine with GRBL to make short noise like "grrr" which can burn your drivers (caused by the initial floating of the pins of the controller after power up) - after adding hardware to hold ENABLE in non active state for 1.5s things are OK.

luben111 commented 7 years ago

BTW, some thoughts about capacitors in parallel to switches:

  1. Adding large capacitor in parallel to the switch contacts is a bad idea - in long term you'll experience malfunction of the switches. The reason is that when the switch is opened the capacitor charges to power supply (5V) and when the switch closes large currents pass through the contacts (peak current could exceed 10-20A for couple of uS). Usually the small switches are rated for lower currents and in combination with moisture the life of the switch may be strongly reduced. If adding capacitors in parallel to switches it should be 10-100nF ceramic, X7R grade.

  2. The electrolitic capacitor has large internal induction so all high frequency noises could not be suppressed by electrolitic capacitor. You can notice that in all power supplies parallel to electrolitic capacitors they put some ceramic ones for suppressing the high frequency noises. So adding electrolitic capacitor on digital inputs has no suppressing effect on high frequency noises.

  3. Microcontroller pins doesn't have Schmitt trigger inputs and by adding large capacitors the voltage level will move slowly. When the voltage is passing slowly through the 0-1 threshold the microcontroller current may increase significantly and some internal high frequency oscillations inside the silicon may occur. This could result in hanging the controller and will make the design more vulnerable to noises.

parnz commented 7 years ago

@luben111 , maybe add a resister in series to limit the hi current when a cap is discharge?

usbcnc commented 7 years ago

Old dot matrix has limit switch parallel with 103 small capacitor. Having large capacitor is not good to reduce noise.

luben111 commented 7 years ago

@parnz - adding serial resistor will increase the life of the switch and reduce the noises but will make much worse the problem with slow moving voltage levels on digital pins without Schmitt trigger. When the voltage changes slowly while passing the 0-1 threshold the inpur works as good analog amplifier (will amplify all high frequency noises around) plus the current consumption of controller jumps.

Also not on last place you'll get delay in switches reaction, for high speeds it might be a problem.

I would not recommend adding resistors on large electrolytic capacitors.

electrokean commented 7 years ago

@luben111 actually the ATmega328 does have schmitt trigger on all digital inputs, but I agree that the capacitors should definitely not be electrolytic, but ideally ceramic with a value in the order of 100nF. Anything significantly larger being required means you need to be implementing other noise reduction techniques, such as lower value pullup resistors, cable ferrites, shielded cables, star grounding, and even optoisolation. BTW, your shield looks really nice :)

luben111 commented 7 years ago

@electrokean - I'm not sure that Atmel I/O have Schmitt triggers (except for TWI CLK and DATA) http://www.avrfreaks.net/forum/inputs-gpios-avr32-family-have-schmitt-trigger-feature

I used to work many years in Atmel developing their capacitive sensors and we relied on the absence of Schmitt trigger on the inputs to get the QTouch technology running.

In the data sheet you can see the gap between low and high input levels - this is not the Schmitt hysteresis but exactly the dangerous area where the input works as analog amplifier. You need to see explicitly Schmitt trigger features listed for the pin.

There in one more reason to believe there is no Schmitt trigger on inputs - Atmel silicons are very fast (20MHz) and Schmitt trigger design is not compatible with high speed architecture because of different delays on falling/rising edges

electrokean commented 7 years ago

@luben111 OK, but that link is related to AVR32 which is a different class of mcu. I'll admit the Atmel data is a bit unclear, and I wasn't sure a while back, but take a look at the current version of the ATmega328P complete datasheet (as used by Arduino Uno for a typical grbl usage example) http://www.atmel.com/Images/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf See section 18.2 - Figure 18-2 shows a schmitt trigger buffer, and it is described in the text 18.2.5 regarding sleep mode. Same in Figure 18-5. There is a "bow tie" transmission gate shown before the schmitt trigger buffer, but that is effectively an analog switch. Finally Figures 33-25 thru 33-27 show the I/O pin thresholds and hysteresis. The datasheet specifically mentions the "Hysteresis of Schmitt Trigger Inputs" in Table 32-10 related to the TWI characteristics, but all the above still seems to indicate schmitt trigger inputs on all digital inputs to me. If you can show something to the contrary I'd be happy to be corrected.

luben111 commented 7 years ago

@electrokean I have to agree with you that Atmega328 has Schmitt triggers inside - Figure 33-27 is related to the Schmitt characteristics (the hysteresis).

Lemonhawk commented 7 years ago

Wasn't the original question about connecting up the limit switches using the NC pole in order to eliminate all the noise and adding caps and resistor? Also seems that you get a little more protection against broken wires. So where is the discussion about connection of limit switches using NC technique and what's the set up in GRBL.

f0m3 commented 7 years ago

chamnit created a caption in the wiki for limit switch wiring. Maybe some of the experts here could help to make a (or some options) description to make a good limit switch wiring? https://github.com/gnea/grbl/wiki/Wiring-Limit-Switches

tklus commented 7 years ago

@lemonhawk, thanks for getting this back on subject.

As I understand it, you wouldn't need capacitors and resistors in a normally closed system. And grbl has settings to run in a normally closed system.

I would like to know how this should be wired and what settings should be changed in grbl.

Thanks! Tim

tbfleming commented 7 years ago

NO vs NC doesn't reduce noise, it just changes what the noise causes. With NO, noise sometimes makes the switch look like it's pushed with it's not. With NC, noise sometimes makes the switch look like it's not pushed with it is.

vMeph commented 7 years ago

i use to have alot false triggers, I dont know how you guys have wired but i have my NC switches with the internal pullup and a external pullup with 1k resistor conected to the arduino IOREF pin and a capacitor betwen the arduino swuitchitcitch pin and GND and never had false triggers again

vMeph commented 7 years ago

But like @tklus asked would be nice to have this type off information on wiki page with the correct schematics for NC switches.

tklus commented 7 years ago

@vMeph, is yours hooked up like the schematic I posted above? although I don't have the resistors drawn in.

vMeph commented 7 years ago

@tklus your schematic shows wired to NO Switch

i have set all machine with shielded cables and i have wired my switches has NC like this, and on grbl settings changed like $5=1, never add problems again with false triggers

nc switch

One off the reasons i changed to NC switches was cause the noise issues and also cause it seems to me that NC switches are a better choise, cause lets supose a switch goes bad if they wired in NC the machine will stop, if is with NO machine will not stop

i would agree with what @chamnit saied " but I'm not an electrical engineer and don't want to give bad advice"

Would be great to have someone that is a electrical engineer and undestands what would be the better choise for plug in NC switches and provide information with clear schematics on wiki page, cause it seems that there is alot debates on this subject and some says is like this and other says is like that, and becomes a litle unclear in what to do

tklus commented 7 years ago

@vMeph just for clarification and learning, see the attached markup of your pic schematic markup

Lemonhawk commented 7 years ago

VMeph, I don't think you need the resistor or cap in your diagram. The limit switch ports are set to use the internal pullup and in normal operation the pin is held to ground, thus preventing noise triggering the limit actuation. If all we need is to set $5=1 then we're done. I think we're all looking for agreement on this. I would really be weary of putting a 47uf cap on one of the Arduino pins - I think one of the above diagrams showed 0.47 uf, but typical noise caps would be 0.01 or 0.001 to kill HF noise. With an NC I think the caps are worthless.

tklus commented 7 years ago

@lemonhawk, that is what I was hoping for as well.

Lemonhawk commented 7 years ago

Yes the diagram by vMeph is for 2 limit switches on a single axis. If you only have a single switch the lead from the left switch (com) would go to GND.

Lemonhawk commented 7 years ago

In all this confusion, it's the $5=1 that would be nice to have some verification that it does flip the limit switch sensing from low to high.

vMeph commented 7 years ago

@tklus Arduino=limit pin IOREF provides the voltage reference that the microcontroller operates those switches is in one axis on one end and on other end

@Lemonhawk i had done before with out the external resistor and i add problems still, it looks like the internal pull up is to high and by adding the external resistor it will be in paralel with the internal pullup and the resistence will be lower, when i had only the internal pullup my machine would trigger arround 15 minutes while doing a job, sense i change things like the diagram above my machine can go for hours and never had a false trigger again

im not a electrical engineer and like tkus i was looking arround in how to wire NC switches i seen so many types of schematics and so many talks about it, and it seemed to me that no one agree with something lol

like you say the internall pull up of arduino is good enought, well for me it wasnt, i still had false triggers

thats why would be good to have someone that is a electrical enginee rthat could come up with the correct way of doing things

langwadt commented 7 years ago

the internal pull-up is definitely too high, it is spec'd at some like 20-50K that is only a few hundred uA at 5V, afaict even microswitches with gold contacts spec minimum currents of +1mA

Lemonhawk commented 7 years ago

I can see the use a 1K pullup, but it's the 47 mf that seems excessive.

vMeph commented 7 years ago

i have updated above post sorry didnt see the new posts maybe the 47mf a litle exessive, mabe can go lower,but sense it worked for me i never had it change

Lemonhawk commented 7 years ago

Seems like it would need to be really nasty noise to pull gnd up to something that would trigger. Any idea where the noise comes from? It would be best to try eliminating it rather than putting bandaids all over.

vMeph commented 7 years ago

no idea where the noise was coming from, but it looks to me that most of this noise issues seems to be related with those that use cnc shield, maybe cause all circuit is packed all together in one small board and there is alot frequecies all close to eatch other and will cause triggers sense 5v is a small signal. but couldnt agree more that putting bandaids all overs is not the way to go about it, thats why would be nice to have all to agree in how to proper conect NC and NO switches and put into wiki pages

Lemonhawk commented 7 years ago

Makes me wonder about using shielded wire. Seems like the cap would put a real load on the limit switch ports.

jahnj0584 commented 7 years ago

Would it be possible to tune the limit switches to filter out any noise, and set the level at which a trigger is accepted, sort of like voice activated microphones on Skype

On Jan 20, 2017 8:40 PM, "Lemonhawk" notifications@github.com wrote:

Makes me wonder about using shielded wire. Seems like the cap would put a real load on the limit switch ports.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/gnea/grbl/issues/96#issuecomment-274216628, or mute the thread https://github.com/notifications/unsubscribe-auth/AQlzDJLA8P3nEMXiUbV0_BKKSxmXWjMLks5rUVPigaJpZM4LnDGT .

electrokean commented 7 years ago

I'm an electrical and software engineer (note "little e" engineer as I never completed my degrees, but I do have nearly 30 year industry experience)

Noise filtering will generally be required for both NO and NC for correct operation and homing. The schematic diagram by vMeph is correct, but the capacitor value is way too large. A ceramic capacitor with a value of 100nF is going to be much better at handling HF noise, and too large a value may affect correct homing pull off. A 47uF capacitor is generally only available in electrolytic form, and those are not good at high frequency due to high ESR (even low ESR ones have a much higher ESR than a ceramic).

As discussed above between luben111 and myself, the ATmega in an Arduino does include Schmitt trigger buffers on its inputs, so they have hysteresis and can handle slowly rising or falling inputs reasonably well (slow is a relative term, but I'm talking anything more than a few tens of microseconds). But if you want to build something that is "future proofed" for upcoming ARM controllers, they may not have native Schmitt trigger inputs and you'd do well to avoid larger capacitors than absolutely necessary.

In regard to not needing the filtering for NC switches, that is incorrect. Even though there is a connection to GND there is finite resistance in any cable or connection, and HF noise can couple into the cable to induce spikes. You need more copper to reduce the cable resistance, but you want flexible cable on a CNC which means less copper & higher resistance. I wont bore you with details, but a connection to GND is not necessarily always at GND potential - a trick for young players!

Another thing that becomes important with NC switches is what is called contact wetting, which is a minimum current needed to flow through the switch to keep the closed contact areas clear of oxidisation. This is one reason why the internal Arduino resistance may be insufficient. Without this your switches can prematurely fail, making use of NC no better than using NO contacts, but this does also apply to an NO switch that is rarely used. See https://en.wikipedia.org/wiki/Wetting_current Edit: I see now that langwadt mentioned the minimum microswitch currents above, which is typically 1mA, so 5v/1mA give a max resistor of 4.7k, and Arduino internal pull-up current sources are roughly equivalent to 10 times that. A 1k 1/4W 5% resistor is a good choice, commonly available, and the lower resistance will provide 5mA at IOREF=5V. Any lower value is probably a bad choice though, unless you've done the various research and calculations.

If using shielded cable (good idea) then the shield should connect to ground at only one end (normally the powered end), and the shield generally should not be used to carry any current. This for limit switches you really want two core shielded cable (like stereo audio cable).

I also strongly recommend adding ferrite "beads", especially on your USB cable, but also on any other cables you may be concerned about. These should be close to the ends of the cables. For thinner wires you can put two or three passes of wire through a torroid ferrite, but for thicker multi-core cables like the USB you can use a split clamp-on ferrite. See https://en.wikipedia.org/wiki/Ferrite_bead

The most common noise source is brushed DC motors which generate small arcs (spark gap transmitter), but noise can come from any cable carrying current that is switching on and off - i.e. your stepper or servo motors, or even from something like a lamp on the same power circuit. Also there are much more indirect noise sources you may not even associate with a false trigger, like your AC heat pump switching on/off, a family member using a kitchen appliance, a neighbour using a ham radio, or an electrical storm many miles away.

Sorry about the long comment - hopefully not too many typos :) Happy if someone wants to use part or all of this in a wiki article.

jahnj0584 commented 7 years ago

Safety wise, a NC switch is better. IF the wires short or break, they will give a false trigger. This is better then homing on a NO switch that has a broken wire and will never complete the circuit, making your machine run off the tracks or who knows what!

On Sat, Jan 21, 2017 at 12:15 AM, Kean Maizels notifications@github.com wrote:

I'm an electrical and software engineer (note "little e" engineer as I never completed my degrees, but I do have nearly 30 year industry experience)

Noise filtering will generally be required for both NO and NC for correct operation and homing. The schematic diagram by vMeph is correct, but the capacitor value is way too large. A ceramic capacitor with a value of 100nF is going to be much better at handling HF noise, and too large a value may affect correct homing pull off. A 47uF capacitor is generally only available in electrolytic form, and those are not good at high frequency due to high ESR (even low ESR ones have a much higher ESR than a ceramic).

As discussed above between luben111 and myself, the ATmega in an Arduino does include Schmitt trigger buffers on its inputs, so they have hysteresis and can handle slowly rising or falling inputs reasonably well (slow is a relative term, but I'm talking anything more than a few tens of microseconds). But if you want to build something that is "future proofed" for upcoming ARM controllers, they may not have native Schmitt trigger inputs and you'd do well to avoid larger capacitors than absolutely necessary.

In regard to not needing the filtering for NC switches, that is incorrect. Even though there is a connection to GND there is finite resistance in any cable or connection, and HF noise can couple into the cable to induce spikes. You need more copper to reduce the cable resistance, but you want flexible cable on a CNC which means less copper.

Another thing that becomes important with NC switches is what is called contact wetting, which is a minimum current needed to flow through the switch to keep the closed contact areas clear of oxidisation. This is one reason why the internal Arduino resistance may be insufficient. Without this your switches can prematurely fail, making use of NC no better than using NO contacts, but this does also apply to an NO switch that is rarely used. See https://en.wikipedia.org/wiki/Wetting_current

If using shielded cable (good idea) then the shield should connect to ground at only one end (normally the powered end), and the shield generally should not be used to carry any current. This for limit switches you really want two core shielded cable (like stereo audio cable).

I also strongly recommend adding ferrite "beads", especially on your USB cable, but also on any other cables you may be concerned about. These should be close to the ends of the cables. For thinner wires you can put two or three passes of wire through a torroid ferrite, but for thicker multi-core cables like the USB you can use a split clamp-on ferrite. See https://en.wikipedia.org/wiki/Ferrite_bead

The most common noise source is brushed DC motors which generate small arcs (spark gap transmitter), but noise can come from any cable carrying current that is switching on and off - i.e. your stepper or servo motors, or even from something like a lamp on the same power circuit.

Sorry about the long comment - hopefully not too many typos :)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/gnea/grbl/issues/96#issuecomment-274233567, or mute the thread https://github.com/notifications/unsubscribe-auth/AQlzDExLInItzYZ5WR09lsWrMlOBxTv0ks5rUYZpgaJpZM4LnDGT .

electrokean commented 7 years ago

@jahnj0584 agreed in the case of a broken wire, but not in the case of a short. Both problems would be apparent during a homing cycle, but both could also happen to moving wiring during operation. As I mentioned above even NC switches can have problems if not properly used, so it isn't a 100% clear argument.

Lemonhawk commented 7 years ago

It seems to me that an antenna (the NO connection) is much better at collecting noise than a circuit to ground (the NC connection).

tbfleming commented 7 years ago

NC is an antenna when the switch is pressed.

luben111 commented 7 years ago

@Lemonhawk - about collecting noise, if the switch is closed the wire works as coil (loop) so any change in the magnetic field around will result in induced voltage. For "poor man" CNC connecting directly the switches to the Arduino board is acceptable but not for professional usage. Consider buffering the home switches in some way (transistor, optocoupler, some TTL, CMOS buffers, etc.), when the switch wires are connected galvanically to the controller pins any ESD and high voltage spikes can make the program counter to jump to random position or cause spontaneous change of RAM/registers.

Opto couplers are not expensive, fast enough, they are very reliable, galvanically isolating the input from output, they are simply the ideal buffering for all inputs for CNC controllers. I could continue the list of the benefits for using the opto couplers.

vMeph commented 7 years ago

@luben111 could you place a schematic in how you would make a conection by using optocoupler?

electrokean commented 7 years ago

@vMeph see the schematic that @luben111 included in his comment from a few days above https://github.com/gnea/grbl/issues/96#issuecomment-273569806

vMeph commented 7 years ago

Thanks sorry didnt pay atention

tklus commented 7 years ago

@luben111, I like your shield but I am using a gshield for my stepper drivers. Would it be possible to make a small circuit board that has a ground, limit pins in and limit pins out with octocoupler built in? That way it will be easy for anyone to connect limit switches with octo couplers.

I bet there would be some interest if we ran a small batch of boards. I am not an e engineer so I wouldn't know where to begin. If someone would make a board design that would work, I could probably have the boards made and sell them at cost. Or, I have seen designs that can be posted to prototype houses and you can order the boards in small batches. Then we could install components our selves.

Thanks! Tim