Ardunino Nano and limit switch problem - Page 1
Forum section: electronics / general

author: regpye
author_rank: Newbie
timestamp: June 14, 2019, 03:53:01 am
content: I am a CNC hobbyist and am developing a circuit board for my machines using a Arduino Nano as the processor. I am by no means an electronic expert, just learning as I go.I use EstlCAM software and firmware for the boards. EstlCAM allows all limits to be connected together and goes through a homing routine one axis at a time until all are completed. This allows just one pin of the Nano to be used for all limits.I am using SN04-N limit switches and they are all connected together in parallel and all triggered at once when the machine reaches any of the limits. This is working fine except the SN04-N really need more than 5 volts to operate successfully each time and I am using the 5 volt power from the Nano to supply them. Most times they will work, but on occasion one will fail to operate. The working range for the NS04-N should be between 10-30 volts.To overcome this I have made them be supplied from their own power supply using a 7808 to produce an 8 volt power supply to them, and put four 1N4007 diodes in series to drop the voltage down from the signal line to the Nano. This seems to be working OK. Maybe I could use a 5 volt zener in place of 4x 1N4007? I also supply the Nano on the vin line from this same power source.I have tried using a 7805 connected between the NS04 signal out and the limit pin on the Nano to reduce the higher voltage down to an acceptable 5 volts so as not to destroy the Nano.Although this does work there is a problem that the LED on the limit switch is illuminated all the time and brightens slightly when triggered.I have also designed a board with a variable power supply, so I can adjust the voltage feeding the limits to a higher voltage using a LM338 (had them on hand). The Nano can also be supplied with this same power through the Vin.I would like to be able to connect the signal out from the SN04-N to the Nano using an opto coupler instead, but I don't know how to go about that. I have a number of opto couplers CNY17-3 single and PC817 single and also quad.Thanks

author: mariush
author_rank: Super Contributor
timestamp: June 14, 2019, 04:08:36 am
content: You could just power the switches with 12v or more, as they're designed.If it's just an on/off signal, you could simply use an optocoupler ... the high voltage lights up the led inside the optocoupler, the light sensitive cell picks the light and you get your 5v signal on the arduino pin.Seehttps://www.electronics-tutorials.ws/blog/optocoupler.htmlAlso see this, more detailed with more examples:https://circuitdigest.com/tutorial/opto-coupler-types-working-applicationsNot sure using diodes is smart, as the forward voltage can vary depending on the current. At very low currents, the forward voltage may be very low... the 1n4007 datasheets show 0.6v at 10mA but no values below that amount.

author: sokoloff
author_rank: Super Contributor
timestamp: June 14, 2019, 04:12:55 am
content: Are you sure you want to use all the limit switches together in a wired-OR configuration? The Nano has a lot of input pins and it would seem like you (or your users) would benefit from having distinct limit switch inputs [particularly in a case where a single axis has a limit switch in both directions].

author: mariush
author_rank: Super Contributor
timestamp: June 14, 2019, 04:15:12 am
content: Indeed. Even if you don't have enough pins, you can use a shift register to read 8 input signals using only two wires... you can even chain multiple such shift registers and still use only 2 wires to get the state of all inputs.

author: regpye
author_rank: Newbie
timestamp: June 14, 2019, 04:53:19 am
content: The switches are being powered with the higher voltage, but the problem is that the signal output from the switch is higher than 5 volts which would destroy the Nano. It is this output that I need to reduce down to 5 volts or a little less.

author: regpye
author_rank: Newbie
timestamp: June 14, 2019, 04:57:04 am
content: Yes I have tried it both ways as there are a number of ways of setting the limits in the firmware.After using the system a number of different ways I found that the homing sequence is best for me using the system as all checking at the same time because the firmware takes care of the sequencing and works really well. It also solves many wiring problems too.

author: regpye
author_rank: Newbie
timestamp: June 14, 2019, 04:58:58 am
content: Yes I realize that it is not an optimal solution, but I tried it anyway and found it does work, but there must be a better way of doing this, so that is why I posted.

author: regpye
author_rank: Newbie
timestamp: June 14, 2019, 05:01:51 am
content: There are enough pins that can be configured. I used to use 3 pairs of limits all on different pins.It still could be done that way, but I found that the results were the same using the simple way of wiring and it causes less problems in the wiring and setup.

author: regpye
author_rank: Newbie
timestamp: June 14, 2019, 05:13:52 am
content: Quote from: mariush on June 14, 2019, 04:08:36 amThanks for those links, very helpful.Maybe the opto coupler would be the best way to go as I can use the high voltage to operate the limit and the output could be coupled to the Nano pin. The way these limits work, the limit is providing power to the Nano pin until triggered, then the voltage drops to zero.If I feed power from the Nano 5v pin to the opto coupler output and through the limit pin that should work I think? The Nano would only get the 5 volts required and the limit could work at its higher voltage to make it work more reliably. When the limit is triggered the opto would stop conducting and the 5 volt would be cut off to the limit pin.

author: sokoloff
author_rank: Super Contributor
timestamp: June 14, 2019, 11:07:01 am
content: It sucks that this video doesn't have a voice explanation or linked blog post with details, but it's exactly what you want to do:IMO, you want one per sensor, but that's up to you. I'll talk below about how to do only one channel's worth, and I'll try to be very explicit about how to connect things up and try to stick with the parts they use in that video. You can figure out how/if to expand that to multiples.On your proximity switches, using the typical wiring colors, give a 10-30VDC supply with positive going to the brown wire and negative of that supply to the blue wire, and black is the output of the switch. Connect the black wire through a resistor to pin 1 of the optoisolator. Connect the negative of the power supply (the blue wire) directly to pin 2. Calculate the resistor value based on the voltage seen at the black wire. Target 10-20mA of current, so pick a resistor that gives you something in that range. For 30V, use 2.2K ohm. For 10V, use 670 or 1K ohm. You can alternatively wire the positive side of the supply (the brown wire) directly to pin 1 and connect pin 2 to a resistor and then the other side of that resistor to the black wire. This will invert the "sense" of the switch (which you can also do in code, of course).Connect pin 3 on the isolator to Arduino GND.Connect pin 4 on the isolator to an Arduino Digital pin that has been configured as an INPUT_PULLUP. (This way, you don't have to wire an external pullup resistor.) Be careful to never configure that pin as a digital output in your code.That will give you the power supply the prox switches need to work reliably and let the Arduino only ever see its own Vcc (via the internal pullup resistor) or GND (via the optoisolator turning on and the transistor inside pulling that pin to digital low).This is another annoying no-words video, showing the connections:They use the second wiring method I describe above. (Note also that R2 in that diagram is the input pullup resistor contained within the AVR microcontroller itself, so you don't need to physically install it, just enable it in code with INPUT_PULLUP pin mode instead of just INPUT.)Once you get it working and want to make it more bulletproof over time and be more immune to failing in the field, connect a diode in the reverse direction (across pins 2 and 1, to prevent any transients from blowing up the LED inside the opto).