# Why does millis() does not start at zero?

**URL:** <https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886>\
**Category:** Programming\
**Created:** [November 20, 2022, 11:41am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886 "2022-11-20T11:41:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![miumiu](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@miumiu](https://forum.arduino.cc/u/miumiu)\
**Post date:** [November 20, 2022, 11:41am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/1 "2022-11-20T11:41:57Z")

</div>

Hello!

I wanted to use millis to define my delta t for a project, apparently millis does not start at zero and I am wondering why.

Even if I use the example code of arduino, millis starts at 3000

```cpp
unsigned long myTime;

void setup() {
  Serial.begin(9600);
}
void loop() {
  Serial.print("Time: ");
  myTime = millis();

  Serial.println(myTime); // prints time since program started
  delay(1000); // wait a second so as not to send massive amounts of data
}

```

Is there a way to to get timer start at zero?

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 20, 2022, 11:46am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/2 "2022-11-20T11:46:49Z")

</div>

millis() does start at 0.

Every time you rest the MCU the millis() count starts at zero.

If you wait a few days millis() will start counting at 0 again.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 20, 2022, 11:50am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/3 "2022-11-20T11:50:46Z")

</div>

> [@Idahowalker](#):
>
> If you wait a few days millis() will start counting at 0 again.

yeah... a few...  
after 49 days...

---

<div class="post-metadata">

**Author:** ![miumiu](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@miumiu](https://forum.arduino.cc/u/miumiu)\
**Post date:** [November 20, 2022, 11:51am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/4 "2022-11-20T11:51:22Z")

</div>

But why does it not start at zero once void loop starts?

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 20, 2022, 11:51am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/5 "2022-11-20T11:51:52Z")

</div>

The ESP32 rolls over once every 207 years.

---

<div class="post-metadata">

**Author:** ![touch1337](https://avatars.discourse-cdn.com/v4/letter/t/bb73d2/32.png) [@touch1337](https://forum.arduino.cc/u/touch1337)\
**Post date:** [November 20, 2022, 11:52am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/6 "2022-11-20T11:52:49Z")

</div>

That's not what the millis() function does. If you want that functionality just create a variable at the start of the loop which gets set to zero each time.

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 20, 2022, 11:53am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/7 "2022-11-20T11:53:25Z")

</div>

millis() start counting when the CPU has pronounced its a live not when it starts running your code.

millis() time comes from the cycle counter.

Words have been provided as cluses so one can do research.

Look at File|Exmaples|02.Digital|BlinkWithoutDelay.

> [@Demonstration code for several things at the same time](https://forum.arduino.cc/t/demonstration-code-for-several-things-at-the-same-time/217158):
>
> NOTE for newcomers. The demo is entirely contained in this Post and Reply #1. There is no need to read further unless you are interested. ...added 25Sep2014 There have been a few occasions recently where newcomers seemed to have difficulty applying the "blink without delay" and "state" concepts to a sketch that is intended to manage a number of concurrent actions. Its very time consuming to respond with suitable code examples - particularly as every case is a little different. I have prepared…

No amount of arguing or what about is going to get make millis() start at 0 when you want it to.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 20, 2022, 11:54am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/8 "2022-11-20T11:54:14Z")

</div>

> [@miumiu](#):
>
> I wanted to use millis to define my delta t for a project

Why do you need that millis() starts from predefined value? The absolute value of millis() doesn't matter at all, all arduino timers uses the difference between millis() values at start and finish

---

<div class="post-metadata">

**Author:** ![miumiu](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@miumiu](https://forum.arduino.cc/u/miumiu)\
**Post date:** [November 20, 2022, 11:54am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/9 "2022-11-20T11:54:19Z")

</div>

I made a function to calculate delta t, but since millis does not start at 0, delta does not start at 0 as well:

```cpp
unsigned long oldTime = 0;

unsigned long CalculateDeltaTime() {
  unsigned long TimeNow = millis();
  unsigned long deltaTime = TimeNow - oldTime;
  return deltaTime;
}

```

---

<div class="post-metadata">

**Author:** ![miumiu](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@miumiu](https://forum.arduino.cc/u/miumiu)\
**Post date:** [November 20, 2022, 11:55am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/10 "2022-11-20T11:55:24Z")

</div>

I am getting data from sensors and I want to assign the data to the time they werde recorded

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 20, 2022, 11:55am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/11 "2022-11-20T11:55:59Z")

</div>

You should change your code this way, for example:

> [@miumiu](#):
>
> `unsigned long oldTime = millis();`

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [November 20, 2022, 11:57am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/12 "2022-11-20T11:57:11Z")

</div>

> [@miumiu](#):
>
> why does it not start at zero

It does, you probably miss the first few lines when connecting to the board.

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 20, 2022, 11:57am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/13 "2022-11-20T11:57:41Z")

</div>

what about

```cpp
/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW; // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

```

if the OP was to look to see how to use millis() when it does not start at 0.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 20, 2022, 11:58am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/14 "2022-11-20T11:58:10Z")

</div>

> [@miumiu](#):
>
> I want to assign the data to the time they werde recorded

and what is your problem? It is irrelevant to the fact, start millis() from zero or not, just assign data to the current value of millis()

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 20, 2022, 11:58am UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/15 "2022-11-20T11:58:44Z")

</div>

> [@miumiu](#):
>
> I am getting data from sensors and I want to assign the data to the time they werde recorded

Letting us know right off the bat you wanted time stamps would have saved. Use an RTC

> **[Arduino RTC Tutorial: Using DS1307 RTC with Arduino - Latest Open Tech From...](https://www.seeedstudio.com/blog/2019/11/19/arduino-tutorial-using-ds1307-rtc-with-arduino/#:~:text=Step%20by%20step%20instructions%20using%20the%20RTC%20with,USB%20cable.%20...%202%20Step%202%3A%20Using%20Software)**
>
> Do you want to maintain hours, minutes, and seconds, as well as, day, month, and

---

<div class="post-metadata">

**Author:** ![miumiu](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@miumiu](https://forum.arduino.cc/u/miumiu)\
**Post date:** [November 20, 2022, 12:06pm UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/16 "2022-11-20T12:06:29Z")

</div>

yeah, but wouldn't I miss the data from t=0? like if a want to make a graph later on, then there would be no data for when t=0,1,2

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 20, 2022, 12:07pm UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/17 "2022-11-20T12:07:46Z")

</div>

Why not use an RTC?

If the first time stamp is at 3000 then call that 0.

---

<div class="post-metadata">

**Author:** ![miumiu](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@miumiu](https://forum.arduino.cc/u/miumiu)\
**Post date:** [November 20, 2022, 12:08pm UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/18 "2022-11-20T12:08:57Z")

</div>

I'd prefer if I wouldn't have to use an additional hardware component

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 20, 2022, 12:10pm UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/19 "2022-11-20T12:10:14Z")

</div>

Then use a relative count as time 0. In some 47 odd days the millis() count will start all over again anyways.

Say when I first read millis() the value is 12. If 12 is my starting point then I store that number and call it my relative 0. When the next count comes is, say 15. The abs(12-15) gives me 3 millis! Later on when the count is 3012 then I can do abs(12-3012) to get the thing.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 20, 2022, 12:11pm UTC](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886/20 "2022-11-20T12:11:24Z")

</div>

> [@miumiu](#):
>
> if a want to make a graph later on, then there would be no data for when t=0,1,2

please look to the post #11 - there is a solution to your problem

[Next page](https://forum.arduino.cc/t/why-does-millis-does-not-start-at-zero/1055886.md?page=2)
