Optically isolated CNC limit switches

Using NJK-5002C Hall Sensors as Limit / Homing switches with GRBL, complete with a simple opto-isolated circuit schematic to give a rock solid operation.

A couple of years ago I built a CNC machine, basing my design around Open Builds Linear rails. To make sure I could home the machine it needed a way to know when each axis was at its limit. Addionaly for safety these could be used to make sure the machine didn’t overrun if you programmed it incorrectly, or something went wrong.

I went for an optical solution with a sensor comprising of an IR led at one side and a photo diode at the other, with a slot in the middle that you could interrupt to break the flow of light. These worked quiet successfully for homing, but I found that when milling I would occasionally get false positives meaning the unit thought it was at it’s limit and it would alarm and stop then just running.

I believe the issue was related to electrical noise being picked up on the circuit. There are ways to combat this with a a capacitor and resistor to built a small filter. However being lazy and pushed for time I just turned off the limit mode and used soft limits on the machine controller. The sensors worked fine for homing and all was good.

Jump forward a couple of years and I’m building a new revision of the machine, I’ll talk more about that in a different post, but I decided to use a different method for my limit switches. The optical switches were a bit fiddly, and it was difficult to get everything nicely (and neatly) aligned.


This time round I opted to use a proximity sensor. I chose the NJK-5002C, which is actually, a hall effect sensor. This means it detects a magnetic field, so when you put a magnet near it it switches. I chose this sensor primarily on cost grounds, as they were less than 2 pounds each and come in a nice industrial housing thats easy to mount and adjust.


I fitted the sensors to my CNC machine with a piece of 50mm angle aluminium, and then glued the magnet with epoxy resin to a small 20 x 40 aluminium joining bracket so I could get some easy adjustment, and simply fitting to the machine. You should be careful as the sensor only detects the magnetic field one way round, so test it before gluing! There is a handy LED on the back of the sensor for testing. So you only need connect the Brown wire to +5v and the Blue wire to 0v, then introduce the magnet and watch for the LED illuminating.

Not wanting to have the same issues of false positives I looked into how professional machines isolate the limit switches and found they use an optically isolated approach. This means that the limit switch is used to switch an LED on inside the opto isolator and that in turn switches on the photo transistor. Using this method electrical noises are actually absorbed by the fact they are not enough to turn the LED on.

I’m using GRBL on an arduino for my controller and that expects its limit switches to pull an input low when the limit is reached. This makes it a very simple circuit using only 2 resistors and an opto isolator, less than £1 all in! You can also put 2 of the circuits in parallel  if you want to have a limit switch at each end, not just one for homing. The 10K pull up resistor is not actually needed as GRBL enables weak pull ups on the input pins.One thing I have noticed with these sensors is that once they have detected the magnet (at around 10 mm from the sensor) they will keep detecting it until you back off by around 1mm. This means that if your using them for homing in GRBL you will need to set your homing pull off to 1.5mm with the $27 setting. If you don’t do this it will only pull off a fraction and then assume the homing switch is faulty due to it not going high.

Leave a Reply

Your email address will not be published. Required fields are marked *