Teensy 4.0 Serial Monitor Lag

I am running the following sketch on my Teensy 4.0:

bool val;

void setup() {
  pinMode(22, INPUT);
  pinMode(13, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  val = digitalRead(22);
  digitalWrite(13, val);
  Serial.println(val);
  delay(300);
}

The circuit consists of a pushbutton on pin 22 and a 10K pulldown.

Then pressing the button, the LED on pin 13 lights up as expected. The serial monitor, however, at best has a 2-3 second lag, and at worse (and more commonly) is completely unresponsive to the state of the button.

I have tried different baud rates (from 600 to 115200), different delays (no delay to 0.5s), and IDE 1.x.x and 2.x.x with no luck.

I also tried showing the timestamp in the IDE, and it is operating in real time. What is interesting is that even when I unplugged the USB cable, though the IDE was correctly showing that the board was disconnected, the serial monitor was still outputting data for a bit before it stopped.

This all makes me wonder... is the teensy pushing so much data to the laptop that the monitor just can't display it? Is the IDE maybe dropping some of the data?

Maybe it depends on the unseen wiring. Is the pin floating?

Perhaps this should have an INPUT_PULLUP Sorry just saw the 10k pulldown.

What is the other end of the button attached to?

One end of the button is connected to 3.3V, the other end to pin 22. Pin 22 also as a 10K resistor between it and ground.

I am 99% sure this is not an electrical issue, but rather software related, since the LED on pin 13 is behaving as expected (lighting up when and only when the button is pressed), indicating that the uC is correctly reading the state of pin 22.

Sometimes if there is a lot of output stored in the IDE's monitor's window, it slows the adding of new characters. Make sure it is cleared out.

With the delay(300) i'd expect the button to be a bit slow and skippy.

Also, sometimes it takes a bit to negotiate the serial connection. Adding a while(!Serial){;} in setup stalls the execution until the Serial is connected.

I often use single-character print statements so you can get a lot of history into the output window, more than a few seconds. You could change from a println() to a print() and shorten to delay(30) and get much more history and responsiveness.

I tried a shorter delay, tried clearing the serial monitor, tried adding while(!Serial) and using print instead of println - no difference.

Try

Serial.println(val); Serial.flush();
1 Like

Hi @electricviolin.

It is unexpected that it would be a problem with Serial Monitor since Paul Stoffregen has been dedicated to ensuring that the Arduino IDE Serial Monitor can support ludicrous throughput:

That was in the Arduino IDE 1.x days. During the complete rewrite of of the Arduino IDE application for the 2.0.0 release, Arduino set the goal of the stock IDE being able to support all the advanced requirements set by Paul which were not provided by Arduino IDE 1.x (thus forcing Paul to maintain a modified version of Arduino IDE 1.x, "Teensyduino"). That included a Serial Monitor that met Paul's extremely high performance standards, and the feedback we received indicates that the Arduino IDE 2.x Serial Monitor does:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.