How to stop false triggering of limit switches by external electrical discharge?
As mentioned in the comments, put a pull up or pull down resistor on the output of the microcontroller pin. You can also put a capacitor in series. The reason you're getting false triggering is because when the switch is open, it is floating. This means the pin doesn't have strong signal pulling it high or low. This is why when you touch something on the printer it is false triggering. It's acting similar to how a touch screen works. The small amount of capacitance from the touch changes the state.
In the drawing I have it setup with a pull down. If you have the other side of the switch connected to gnd, you will need a pull up resistor. With signals that leave the board, I typically put a series resistor right on the input of the pin. This makes a small RC filter with the capacitance of input pin and helps reduce any noise that's introduced on that line. Make sure you put the series resistor at the pin and not on the other side of the pull up/ down resistor. This would create a voltage divider and could cause issues.
EDIT
Since the Switch is normally closed, a pull down or pull up isn't what is causing the issue that the question is asking. Although they are still needed for when the switch is pressed since the pin will be floating when the switch is pressed.
In the datasheet for the driver on page 3 there is a Block diagram. In your schematic, the enable pin, sense pins, and ROSC pin are not connected. None of these pins should be left floating and should be connected according to the datasheet. You could get a lot of unpredictable events by leaving these pins floating.



simulate this circuit &ndash; Schematic created using CircuitLab