Arduino uno r3 ignores delay()

Hi, i just bought an arduino uno r3, it was included in a starter kit.
I don't know if my unit is broken or i just don't understand something...

If i run this simple script

void setup() {
  Serial.begin(9600);
}

int num = 0;
void loop() {
  delay(125);
  num++;
  Serial.println(num);
}

It starts counting as expected, until it reaches 1500 +- iteratinos. After that it ignores the delay function and prints out rapidly still counting up. It goes until 4000+- and after that it restarts from 0 with the delay working again.

If i wait like 2-3 times this whole process, it does not restart anymore, it is just frozen and doing nothing. Sometimes it prints out garbage in the serial monitor before freezes like these: ⸮⸮ and other characters i have never seen...

If i connect an LCD, it does the same thing but it requires like 20 iterations instead

I tried to connect a humidity sensor and print it to the serial console. It works, again for a few loop cycles, than it resets or reads garbage and freezes.

The arduino is powered from the USB port not from batteries

Edit: Just tried with batteries. It does the same thing, it resets after 150 cycles or prints garbage across the whole LCD...
My test code with batteries:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


void setup() {
  lcd.init();
  lcd.backlight();
}

int x = 0;
void loop() {
   delay(500);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("Hello");
   lcd.setCursor(0,1);
   lcd.print(x);
   x++;
}

Try storing millis() in num, then printing millis() - num. This will tell you how much time has elapsed between loop iterations.

Something really strange must be happening in your Arduino UNO. Without the delay, my UNO counts about 160 times a second. With the delay, it counts a little under 8 times a second and counted well past 5400 without noticeably changing speed.

Hmm. its even better....

With this code runing

void setup() {
  Serial.begin(9600);
}

int num = 0;
void loop() {
  num = millis();
  Serial.println(num);
}

My serial monitor shows an interesting thing.
It starts at 0, goes up to 70, and after that it prints the same 70 over and over again, and freezes

Edit: after multiple runs, it goes up to a random number, some times only to 1, and prints it over and over again. If i turn on timestamps it shows between start and freeze was between

17 - 35 seconds

Garbage i was talking abaout:
⸮T⸮⸮_⸮⸮~⸮⸮⸮⸮⸮⸮⸮}⸮⸮⸮⸮⸮⸮߾⸮⸮⸮⸮W⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮r⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮?⸮C?⸮=⸮X⸮>⸮⸮U⸮
⸮K;⸮⸮⸮|⸮⸮⸮x⸮⸮⸮⸮'⸮⸮t⸮⸮W⸮߿⸮⸮^⸮⸮/⸮⸮⸮⸮mt⸮⸮s⸮⸮zc߯

Without the delay it counts about 200 times a second while it resets or freezes

That's "odd" but millis() returns a type unsigned long and it will overflow with a type int.

On my Arduino UNO it counts up about every 7 or 8 milliseconds and when it goes past 32767 it rolls over to -32768 (as expected) and counts up from there.

Sounds like your PC is having trouble keeping up with the input. If I increase the baud rate to 115200 (12 times faster) the Serial Monitor eventually starts to get bursty (pauses between bursts of output) and eventually crashes due to Java running out of memory. If put in a 500 millisecond delay, the number just keeps counting (overflowing every 32.767 seconds).

Try adding more delay until the problem goes away. That will give you a rough idea of how much data your PC can handle.

@zsolt0005, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

If i add any delay, it will be ignored after a few seconds of run time :confused:

What is your host?

Time to buy a new PC. It sounds like your Serial Monitor can't keep up. Your original sketch was sending 5 characters ("1500\n") every 125 milliseconds. That's only about 40 characters per second (about 400 baud). A modern PC should be able to handle 9600 baud easily.

Fixed that quote up a bit, sry. :expressionless:

a7

I have a Lenovo legion 5 pro (Ryzen 5800H / RTX 3070) so it is not my PC's fault for sure :smiley:
It does the same thing on batteries, if i connect an lcd and count up from 0 with 1000ms delay, it will ignore the delay the same way

Host ?

That host

1 Like