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
🌐
Programming Electronics Academy
programmingelectronics.com › home › arduino sketch with millis() instead of delay()
millis() instead of delay() with Arduino [Guide + Code]
November 13, 2023 - In other words, when you upload your sketch to your Arduino, as soon as the upload is complete, the clock starts. Millis returns the number of milliseconds that have passed since this upload was completed. Essentially, it’s a timer for how ...
🌐
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 presents itself).
Discussions

Arduino millis Timer
Hello everyone, I’d like to share a simple and useful function for creating non-blocking delays in Arduino using millis(). This function allows you to perform tasks at specific intervals without blocking the rest of your code, unlike the delay() function. Here’s the code: // Variable to ... More on forum.arduino.cc
🌐 forum.arduino.cc
19
2
March 3, 2025
Arduino Multitasking Tutorial - How to use millis() in Arduino Code
Hello all, I am new to the Arduino UNO and MEGA 2560 and electronics in general. I found this tutorial Arduino Millis Tutorial - How to use millis() in Arduino Code for Multitasking Arduino Multitasking Tutorial - I created, it as instructed, and the led's blink as shown in the video of the ... More on forum.arduino.cc
🌐 forum.arduino.cc
6
0
May 9, 2020
PSA: You're probably using delay() when you want to use millis().
If you're lazy ellapsedMillis also works really well. To my eyes it also results in more easily readable code, I use it a lot. More on reddit.com
🌐 r/arduino
53
363
May 23, 2023
Programming a Timer for an Accelerometer
Are you having trouble planning how to do this, or implementing your plan in code? More on reddit.com
🌐 r/arduino
6
2
November 28, 2017
🌐
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.
🌐
Seeed Studio
seeedstudio.com › home › multitasking with arduino – millis(), rtos & more!
Multitasking with Arduino - Millis(), RTOS & More! - Latest News from Seeed Studio
September 29, 2021 - time = millis() // Returns the number of milliseconds passed since the Arduino board began running the current program.
🌐
TutorialsPoint
tutorialspoint.com › arduino › arduino_millis_function.htm
Arduino - millis () function
unsigned long time; void setup() { Serial.begin(9600); } void loop() { Serial.print("Time:"); time = millis(); //prints time since program started Serial.println(time); // wait a second so as not to send massive amounts of data delay(1000); }
🌐
CircuitDigest
circuitdigest.com › microcontroller-projects › arduino-multitasking-using-millis-in-arduino
Arduino Millis Tutorial - How to use millis() in Arduino Code for Multitasking
April 14, 2022 - If 1000ms has passed, then the LED blink and again comes to same state. And this goes on. That's it, we have learnt to use millis instead of delay. This way it won’t halt the program for particular interval. Interrupts in Arduino works same as in other microcontrollers.
Find elsewhere
🌐
Best Microcontroller Projects
best-microcontroller-projects.com › home › arduino hardware › arduino millis
Secrets of Arduino millis: How it works and how to use it.
The millis() function returns the current time in milliseconds (1/1000th of a second) from when you powered up the board (or reset it). It gives you a way of measuring time from within your program, which is quite different to the delay() function ...
🌐
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.
🌐
Arduino Getting Started
arduinogetstarted.com › reference › arduino-millis
millis() | Arduino Getting Started
5 days ago - const unsigned long TIME_INTERVAL = 1000; unsigned long previousMillis; void setup() { Serial.begin(9600); previousMillis = millis(); } void loop() { if (millis() - previousMillis >= TIME_INTERVAL) { previousMillis = millis(); Serial.println("Arduino References"); } } ... Please note that the return value for millis() is of type unsigned long, logic errors may occur if a programmer tries to do arithmetic with smaller data types such as int.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Arduino millis Timer - Programming - Arduino Forum
March 3, 2025 - Hello everyone, I’d like to share a simple and useful function for creating non-blocking delays in Arduino using millis(). This function allows you to perform tasks at specific intervals without blocking the rest of your code, unlike the delay() ...
🌐
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 ...
🌐
DeepBlue
deepbluembedded.com › home › blog › arduino millis() function (timer vs delay) tutorial
Arduino millis() Function (Timer vs delay) Tutorial
August 17, 2023 - The Arduino millis() is a timer-based function that returns to you the time elapsed (in milliseconds) since the Arduino board was powered up. Which can be used to create a time base for various events in your applications (like LED blinking ...
🌐
Bald Engineer
baldengineer.com › home › millis() tutorial: arduino multitasking
millis() Tutorial: Arduino Multitasking - Bald Engineer
March 31, 2022 - You might also want to look at this delayed event millis example. ... Gone through it, few understand , few not. actually i want to learn how to make function and call it when ever required. Background- I connect 20 nos. LED with Arduino UNO and able to address it individually. also able to write different pattern, but to repeat the pattern I need to write the same program lines again.
🌐
Delft Stack
delftstack.com › home › howto › arduino › arduino millis
Arduino millis() Function | Delft Stack
March 4, 2025 - the millis() function returns the number of milliseconds since the Arduino board began running the current program. This value is stored as an unsigned long, which means it can count up to about 50 days before it rolls over to zero.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Arduino Multitasking Tutorial - How to use millis() in Arduino Code - Programming - Arduino Forum
May 9, 2020 - Hello all, I am new to the Arduino UNO and MEGA 2560 and electronics in general. I found this tutorial Arduino Millis Tutorial - How to use millis() in Arduino Code for Multitasking Arduino Multitasking Tutorial - I created, it as instructed, and the led's blink as shown in the video of the ...
🌐
GitHub
github.com › Koepel › Fun_with_millis
GitHub - Koepel/Fun_with_millis: Small Arduino examples using the millis() function. · GitHub
The Arduino millis() function can be used in almost every situation. These are small demonstration sketches to show how to use millis() in different situations.
Starred by 15 users
Forked by 2 users
Languages   C++
🌐
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

🌐
Rachel De Barros
racheldebarros.com › arduino-projects › how-to-use-millis-arduino-multi-tasking
How to Use millis(): Arduino Multi-tasking
September 19, 2024 - millis() is a function in the Arduino programming environment that returns the number of milliseconds since the current program started running on the Arduino board.