Teensy 4.1 end stop flickering between HIGH and LOW when serial monitor is open

TeensyTinkerer

New member
Hello everyone, I recently got a Teensy 4.1 and am thrilled with its speed! (porting a project over from Arduino).

Problem:
I seem to be running into an odd issue, and am wondering if anyone knows what is going on.

I have connected an End Stop (Limit Switch) to the Teensy 4.1. It works fine (the signal pin is HIGH when the end stop is open, the signal pin is LOW when the end stop is closed), as demonstrated in the video by the LED_BUILTIN light staying HIGH when the signal pin is HIGH and LED_BUILTIN going LOW when the signal pin is LOW.

However, as soon as I open the Serial monitor, the LED_BUILTIN flickers and the Serial monitor shows the signal pin flashing between high and low.

Does anyone know why is this happening?

Video: watch video here (it wouldn't upload as an attachment for some reason)

Notes about my setup:
  • Only wires connected to Teensy 4.1 are: SIGNAL_PIN from End Stop* connected to Teensy PIN 10, and Ground from End Stop connected to Teensy Ground
  • Teensy 4.1 has the signal pin set as INPUT_PULLUP
  • When the End Stop is closed, it connects the Teensy PIN 10 to Ground (making it read LOW), when the End Stop is open, the Teensy's internal Pullup Resistor makes PIN 10 read HIGH
  • The End Stop cables and USB cable for Teensy 4.1 are very long (6 feet each)
*Technically on this End Stop (specifications), it's the V pin that I have connected to Pin 10, because the way this limit switch was manufactured, it is V and G that get connected when the end stop is closed. I think this is an insignificant technicality, as it is merely how they chose to label the wires, doesn't change how the end stop is functioning.

Code:
Code:
//End Stop Test
#define SIGNAL_PIN 10

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  pinMode(SIGNAL_PIN, INPUT_PULLUP); //external pullup being used; otherwise use INPUT_PULLUP
  pinMode(LED_BUILTIN,OUTPUT);

}

void loop() {

  if(digitalRead(SIGNAL_PIN) == HIGH)
  {
    Serial.print(SIGNAL_PIN);
    Serial.print(": ");
    Serial.println("HIGH");    
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else if(digitalRead(SIGNAL_PIN) == LOW);
  {
    Serial.print(SIGNAL_PIN);
    Serial.print(": ");
    Serial.println("LOW");    
    digitalWrite(LED_BUILTIN, LOW);
  }


}

Any suggestions appreciated!
 
Last edited:
Oops - problem solved.

There was a semicolon at the end of this line when there should not be...LOL!

Code:
else if(digitalRead(SIGNAL_PIN) == LOW);
 
Missed that in quickly reading it here too.

Was going to note that the ELSE doesn't need to retest with the ELSE IF as the result is either HIGH or LOW.

Another thing that can help - in this case it doesn't in IDE 1.8.19 but DOES in IDE 2.0: Hit Ctrl+T to format the code.
> this often helps with other syntax or confusing compiler errors as the code is uniformly indented and mismatched braces jump out.

The IDE 2.0 editor does this - moving the extra semi-colon to a new line - that makes it stand out nicely:
Code:
...void loop() {

  if (digitalRead(SIGNAL_PIN) == HIGH) {
    Serial.print(SIGNAL_PIN);
    Serial.print(": ");
    Serial.println("HIGH");
    digitalWrite(LED_BUILTIN, HIGH);
  } else if (digitalRead(SIGNAL_PIN) == LOW)
    ;
  {
    Serial.print(SIGNAL_PIN);
    Serial.print(": ");
    Serial.println("LOW");
    digitalWrite(LED_BUILTIN, LOW);
  }
 
Back
Top