Can't get limit switch to work on Teensy 4.1

Quill213

New member
I am working on an inverted pendulum project and have gotten all of my electronics connected and working with the Teensy 4.1 EXCEPT (surprisingly) a simple limit switch. It is a CYT1073 micro limit switch (Link: https://a.co/d/2MxWryM). I tried to get it to work with all my other electronics connected to the Teensy but it wasn't working so I disconnected everything and left only the teensy itself and the limit switch. I have the limit switch COM connected to ground and NC connected to a digital pin (I have tried many pins and all didn't work). I have also tried INPUT, INPUT_PULLUP, and INPUT_PULLDOWN within my code (attached). I have also tried using a 10K ohm pullup resistor. NONE of it worked. When I test the same exact configuration on my Arduino UNO it works, but not on the Teensy. The crazy thing is that I've used this same exact limit switch with a different Teensy 4.1 with the same exact code and configuration and it worked just as I expected. And yes, I've tried using another CYT1073 limit switch thinking it was maybe faulty but it still did not work. Help would be VERY much appreciated. Also, if it helps, I have attached my very simple wiring diagram.

Code:
#include <Arduino.h>
int limitswitch = 6;

void setup() {
  Serial.begin(9600);
  pinMode(limitswitch, INPUT_PULLUP);
}

void loop() {
  int val = digitalRead(limitswitch);
  Serial.println(val);


}
TCYT.PNG
 
@Quill213: Your connection diagram shows the switch connected to pin 19. Pin 6 is in the same location relative to the microUSB connector, but is on the other side of the Teensy.

Hope that helps . . .

Mark J Culross
KD5RXT
 
@Quill213: Your connection diagram shows the switch connected to pin 19. Pin 6 is in the same location relative to the microUSB connector, but is on the other side of the Teensy.

Hope that helps . . .

Mark J Culross
KD5RXT
I wish it were that easy but no I've tried many pins, I just picked a random pin to connect to the limit switch. But thank you I should have labeled the connection in the image better as well as not include a specific pin in both the code and the drawing.
 
OK, time for some simple troubleshooting.

TEST #1: While running your sketch, simply connect a wire between GROUND and the pin that you have configured your sketch for. You should see the printed value go to "0". Disconnect the wire & you should see the printed value go to "1". Try multiple pins, but make sure that your wire is connected to the same pin that is used/defined in your sketch. If this fails, then the non-working pin may potentially be damaged (accidentally connecting anything greater than 3.3VDC to a pin, no matter for how short a time, will very likely cause damage . . . the T4 is not 5VDC tolerant).

TEST #2: If you have a multimeter, set it for resistance. Connect either of the leads to the COM connection on the limit switch, and the other lead to the other connection on the limit switch that you will be using. While running your sketch, as you toggle your limit switch (activate/deactivate), you should see the resistance indicated on your multimeter toggle between almost 0-ohms (it may not be exactly "0") and infinite resistance (it may be very high resistance). If this fails, then try other combinations of all of the connections on the limit switch, as the pinout may not be what you thought.

TEST #3: Once both of these tests have been completed successfully, then connect your limit switch to your Teensy & try running your sketch again.

Hope that helps . . .

Mark J Culross
KD5RXT
 
I agree with Mark... But in addition, you might also show a picture of the setup. For example it might
show that one or more pins have not been soldered, or there is a short...
 
Back
Top