As part of one of my first projects I'm trying to use a limit switch to tell a stepper motor to stop when the switch is pressed.
I used this tutorial (BITE SIZE ARDUINO – 3 PIN SNAP-ACTION LEVER SWITCH – Killer Robotics) to try and get the basic idea of using a limit switch with an arduino but after trying to change things to get it to do the task mentioned above, the motor still keeps going when I press the switch. I'm not sure if there's a problem with my code or circuit (attached).
Start simple, forget about the motor. Use print statements to see if you are correctly detecting the switch as you operate it by hand. Once you know you can "see" the switch add some more logic with print statements saying "turn motor off" and "turn motor on". Once that is working add the motor.
Plasterboard:
Yes it does and when I run it the motor will start when I press the switch, so I guess I've adapted the code and/or circuit incorrectly.
Switches come with 'normally open' or 'normally closed' contacts or both.
Use a multimeter to find out if prressing the switch makes or breaks the connection between the contacts that you are using.
According to your wiring diagram you have NC connected to the input and +5V connected to C. Therefore when you press the switch the input goes from HIGH to LOW. There is nothing wrong with this arrangement you just need to change if (pressSwitch == HIGH) to if (pressSwitch == LOW).
ToddL1962:
According to your wiring diagram you have NC connected to the input and +5V connected to C. Therefore when you press the switch the input goes from HIGH to LOW. There is nothing wrong with this arrangement you just need to change if (pressSwitch == HIGH) to if (pressSwitch == LOW).
I've done that and the motor will run when power is supplied but when I press the switch, the motor just keeps going. Is it something to do with it being in the void loop() ?
Comment out the motor movement code and just insert serial print statements to indicate if the switch is HIGH or LOW. Make sure your switch is behaving properly. That way you will know if it is a switch problem or motor problem.
ToddL1962:
Comment out the motor movement code and just insert serial print statements to indicate if the switch is HIGH or LOW. Make sure your switch is behaving properly. That way you will know if it is a switch problem or motor problem.
Ok, so I've commented out the motor movement code and replaced it with " MOTOR ON " and " MOTOR OFF ". When I run this and view the serial monitor, it displays " MOTOR ON " when I don't press the switch and " MOTOR OFF " when I press the switch, so the switch seems to be alright. I'm using the Adafruit NEMA 17 12v stepper motor and the Adafruit motor shield v2.3. I have the motor connected to the shield at M3 and M4, and the shield is connected to the arduino at 5v, gnd, a4 and a5 as instructed here: https://cdn-learn.adafruit.com/downloads/pdf/adafruit-motor-shield-v2-for-arduino.pdf?timestamp=1582308732 on pg 41.
Have I missed something and Connected the shield incorrectly?