Part 2 We have seen how to turn an LED on and off at intervals which is fascinating for maybe a minute or two at most but how about using the same principle to change the brightness of an LED ? I will use an LED on pin 10 in this example as that pin supports PWM output. The basis of the program wi… Answer from UKHeliBob on forum.arduino.cc
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › time › millis
millis() | Arduino Documentation
This function returns the number of milliseconds passed since the program started.
🌐
Arduino Forum
forum.arduino.cc › projects › tutorials
Using millis() for timing. A beginners guide - Tutorials - Arduino Forum
October 2, 2017 - Part 1 It is not usually long before new Arduino users discover that although the delay() function is easy to use it has side effects, the main one of which is that its stops all activity on the Arduino until the delay is finished (not quite true, I know, but that is usually how the problem ...
Discussions

[SOLVED] How to correctly use millis() for delay
Hello, I'm wondering if i'm doing this right. I'm trying to use the millis() function to delay another function precisely. I want to know if I'm declaring the variables right, if I'm fetching the potentiometer value right and if I really need floating points to do this. More on forum.arduino.cc
🌐 forum.arduino.cc
19
0
April 30, 2023
Millis() instead of delay and loop() instead of for-loop
One of the most frequently asked questions by beginners is how to handle delays and for-loops in loop() without blocking other functions. In this thread I like to publish some small sketches do demonstrate the use of millis() instead of delay() and how to implement the functionality of a for-loop ... More on forum.arduino.cc
🌐 forum.arduino.cc
19
2
April 2, 2023
🌐
Reddit
reddit.com › r/arduino › psa: you're probably using delay() when you want to use millis().
r/arduino on Reddit: PSA: You're probably using delay() when you want to use millis().
May 23, 2023 -

One of the most frequent recommendations I make when auditing Arduino code comes to the difference in use cases for millis() and delay(). This little blurb should help you to differentiate the two and understand why you would use one over the other.

First, millis() is an essential function in programming that returns the elapsed time in milliseconds since the board began running the current program. Unlike other timing functions, millis() is non-blocking, meaning it does not interrupt the flow of your program. Instead, it allows you to check the passage of time at any point in your program. This is particularly useful in scenarios requiring simultaneous tasks or tasks at varying intervals. For instance, if you're operating an LED light while gathering sensor data at different intervals, millis() allows you to do both independently.

Blinking Light Example:

#define LED_PIN LED_BUILTIN
#define BLINK_INTERVAL 1000  // Blink every 1000 ms (1 second)

unsigned long previousMillis = 0;
bool ledState = LOW;

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= BLINK_INTERVAL) {
    previousMillis = currentMillis;  // Remember the time

    ledState = !ledState;            // Toggle the LED state
    digitalWrite(LED_PIN, ledState);
  }
}

While it may require a bit more complexity in the code to store timestamps and check time differences, the benefits of non-blocking multitasking outweigh this additional complexity.

It's sibling, delay(), is used to pause the execution of the current program for a specified number of milliseconds. Unlike millis(), delay() is a blocking function, meaning it stops all other operations on the board for the duration specified. For example, if you are blinking an LED and use delay(1000), the LED will turn on, the program will pause for one second, and then the LED will turn off. While delay() is a simpler and more straightforward function to use, it is best suited to simpler tasks that do not require multitasking.

Blinking Light Example:

#define LED_PIN LED_BUILTIN

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);  // Turn the LED on
  delay(1000);                  // Wait for a second
  digitalWrite(LED_PIN, LOW);   // Turn the LED off
  delay(1000);                  // Wait for a second
}

If the task is time-sensitive or requires simultaneous operations, delay() might not be the best option due to its blocking nature.

I hope this clears up the "why" of each! Please ask any questions

🌐
Programming Electronics Academy
programmingelectronics.com › home › arduino sketch with millis() instead of delay()
millis() instead of delay() with Arduino [Guide + Code]
November 13, 2023 - Let’s take a look at the first five seconds of a program. The little dot (in the picture below) represents where millis is on the timeline. As soon as we power up Arduino, the millis function starts counting. You can see the value is increasing and moving to the right along the time axis.
🌐
Norwegian Creations
norwegiancreations.com › 2017 › 09 › arduino-tutorial-using-millis-instead-of-delay
Arduino Tutorial: Using millis() Instead of delay() – Norwegian Creations
September 5, 2017 - The first advantage we’ll discuss is accurate timing. Code-wise, we covered this in the last chapter. With millis() we can ensure that the loop runs as often as we want, regardless of the execution time (obviously as long as the execution time is less time the desired period).
🌐
Instructables
instructables.com › circuits › arduino
Arduino Timing Methods With Millis() : 4 Steps - Instructables
July 17, 2019 - Arduino Timing Methods With Millis(): In this article we introduce the millis(); function and put it to use to create various timing examples. Millis? Nothing to do with lip-syncers… hopefully you recognised milli as being the numerical prefix for one-thousandths; that is multiplying a …
Find elsewhere
🌐
Tronixstuff
tronixstuff.com › 2011 › 06 › 22 › tutorial-arduino-timing-methods-with-millis
Tutorial: Arduino timing methods with millis() – tronixstuff.com
April 15, 2025 - To put it simply, the millis function makes use of an internal counter within the ATmega microcontroller at the heart of your Arduino. This counter increments every clock cycle – which happens (in standard Arduino and compatibles) at a clock ...
🌐
Industrial Shields
industrialshields.com › blog › arduino-industrial-1 › industrial-arduino-millis-vs-delay-248
✅Industrial Arduino Millis() vs Delay()
February 24, 2026 - It will return the number of milliseconds that have passed since the PLC Arduino board started running the current program. It has a time limit of approximately 50 days, after this time, it will overflow and go back to zero.
🌐
GitHub
github.com › ZakKemble › millis › blob › master › arduino › millis › millis.cpp
millis/arduino/millis/millis.cpp at master · ZakKemble/millis
Lightweight millisecond tracking library. Contribute to ZakKemble/millis development by creating an account on GitHub.
Author   ZakKemble
🌐
Little Bird Electronics
learn.littlebirdelectronics.com.au › arduino › millis-function-and-arduino
millis() function and Arduino | Little Bird Guides
millis() is a built-in method of the Arduino library, and it returns the number of milliseconds that the sketch has been running, or since the board has been powered up.
🌐
Code-reference
code-reference.com › arduino › time › millis
arduino time millis Programming | Library | Reference - Code-Reference.com
February 16, 2024 - Description Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days. Parameters None Returns Number of milliseconds since the program started (unsigned long)
🌐
Adafruit
learn.adafruit.com › multi-tasking-the-arduino-part-1 › using-millis-for-timing
Using millis() for timing | Multi-tasking the Arduino - Part 1 | Adafruit Learning System
November 3, 2014 - Instead of relying on delay() to time the blinking. BlinkWithoutDelay remembers the current state of the LED and the last time it changed. On each pass through the loop, it looks at the millis() clock to see if it is time to change the state of the LED again.
🌐
Programming Electronics Academy
programmingelectronics.com › home › doing multiple timed things with arduino: unleash the millis()!
Doing multiple timed things with Arduino: Unleash the millis()! - Programming Electronics Academy
May 31, 2019 - Think of the algorithm as our plan of action. Looking at this it appears we could use the Arduino millis () function to set up the timing for these events, and we could use analogRead () to read the LDR and the temperature sensor values.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
[SOLVED] How to correctly use millis() for delay - Programming - Arduino Forum
April 30, 2023 - Hello, I'm wondering if i'm doing this right. I'm trying to use the millis() function to delay another function precisely. I want to know if I'm declaring the variables right, if I'm fetching the potentiometer value righ…
🌐
Stack Overflow
stackoverflow.com › questions › 71474892 › how-to-use-arduino-uno-millis-function
how to use arduino uno millis function() - Stack Overflow
This looks very wrong to me: if(unsigned long)(millis()-nowtime>300) The parenthesis don't group what we want to group. Fixing that might fix the compile problem. To use millis() you'll have to update nowtime and store it outside of the loop ...
🌐
Bald Engineer
baldengineer.com › home › millis() tutorial: arduino multitasking
millis() Tutorial: Arduino Multitasking - Bald Engineer
March 31, 2022 - This function returns the number of milliseconds the current sketch has been running since the last reset. At first, you might be thinking, well that’s not every useful! But consider how you tell time during the day.
🌐
Programming Electronics Academy
programmingelectronics.com › home › millis() arduino function: 5+ things to consider
millis() Arduino function: 5+ things to consider - Programming Electronics Academy
April 6, 2023 - Have you heard of the Arduino millis() function? Did you know that it gives you access to the Arduino internal timer counter hardware which can be used for the timing of different events? We will discuss this and more in the video tutorial below.  Topics in this lesson What is a hardware ...
🌐
Arduino Forum
forum.arduino.cc › projects › tutorials
Millis() instead of delay and loop() instead of for-loop - Tutorials - Arduino Forum
April 2, 2023 - One of the most frequently asked questions by beginners is how to handle delays and for-loops in loop() without blocking other functions. In this thread I like to publish some small sketches do demonstrate the use of millis() instead of delay() and how to implement the functionality of a for-loop ...
🌐
Instructables
instructables.com › circuits › arduino
Counting the Time With the Arduino Millis()-function : 5 Steps - Instructables
June 28, 2024 - Counting the Time With the Arduino Millis()-function: Hey, in this instructable I will show you how you can make use of the millis function from the arduino to create a clock. Keep in mind, that the oscillator from the arduino chip is not very precised. If you don't have the space or pins left for a re…