Can't get limit switch to work on Teensy 4.1

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: Cylewet 25Pcs AC 1A 125V 3Pin SPDT Limit Micro Switch Long Hinge Lever for Arduino (Pack of 25) CYT1073: Amazon.com: Industrial & Scientific). 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.

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

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

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

It looks to me as though you have the limit switch connected to pin 19, not pin 6.

1 Like

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.

Do you have an LED and 470 to 1000 Ohm resistor?

I have LEDs but only 330 and 10k ohm resistors. I guess I can put two or more 330's in series. What are you suggesting?

Just a data point: uploaded your sketch to a Teensy 4.1 here, hooked up a lever switch and you know what happened... it worked just fine.

Are you sure you've got your board selection and options set properly?

Edit: this is a completely off the wall never in a million years question: you haven't by any chance got one of the original almost 5 year old now beta test boards, have you? The ones where only the ground on pin 1 is hooked up?

1 Like

330 should work.
Connect the limit switch common to 5 volts +, the LS NC to the LED anode (long leg), the LED cathode (short leg) to resistor, other end of resistor to 5V - (GND). Does the LED light? If you press the LS lever, does the LED go OFF?

Yes when unpressed the LED is on and when pressed the LED is off. This works.

So you have a good limit switch, now post a clear picture of your project with all pin and terminal labels shown.

1 Like