Can I record current date & time in file without a RTC module?

I have now only an Arduino. Can I record current date and time without purchasing a RTC module? Im not gonna connect Arduino with computer.

Example data format:
Date Time Data
10/24/2013 17:32:00 1.5
10/24/2013 17:32:01 1.7
10/24/2013 17:32:02 2.5
10/24/2013 17:32:03 2.1

Thanks.

No clock and no computer. OK. Where do you propose to get the date and time, then?

I bought 30 x DS1307 clock chips from eBay for $US 20.

So that is about 66 cents each. Plus you need a 32.768 kHz crystal. That's about 30 cents.

So for a dollar you can have a proper clock. Why not do it?

Oh, you also need a CR2032 battery as a battery backup. That would be a few more cents.

The Arduino is able to count microseconds, so why can't this be used as time?

The Arduino is able to count microseconds, so why can't this be used as time?

It counts microseconds since when? All that micros(), or millis(), returns is the interval since the Arduino was reset. If you know the time that the Arduino is reset, you can know the current time. But, how do you know the time that the Arduino was reset?

PaulS:

The Arduino is able to count microseconds, so why can't this be used as time?

It counts microseconds since when? All that micros(), or millis(), returns is the interval since the Arduino was reset. If you know the time that the Arduino is reset, you can know the current time. But, how do you know the time that the Arduino was reset?

So... It's possible then?
How do you know the time when it was reset? Look at the time I guess.. Put this into the code, and make a clock. Shouldnt be impossible.

Edit: I must have missunderstood you.

Look at the time I guess.. Put this into the code

Reprogramming the Arduino every time it is reset is easier that getting a clock? Not for me.

PaulS:
Reprogramming the Arduino every time it is reset is easier that getting a clock? Not for me.

What is easier was not the question, I just tried to figure a solution. That's what I thought we were here for.

The Time library works with or without an external RTC. Some method of setting the time initially (probably in the setup function) will be needed. One possibility is to set the time to the compile time and date. However, every time the microcontroller restarts, it will reset to this same date (unless the sketch is re-uploaded). Here is an example showing how to do that. Another possibility is to just enter the time directly on the serial monitor. Here is one example of that, and here is a slightly different one.

Without an external RTC, the Time library uses the Arduino's system clock oscillator as its time base. You may find that this will drift quite a bit, most Arduinos use ceramic resonators that are only accurate to about ±0.5%. I'd certainly agree with some of the comments above, an external RTC is a much better solution.

Well, theoretically... This should be possible to work, even with restarts? I might be wrong here.

If the loop sets current time into setup every 10 seconds, that should keep the time going even if the board restarts. Of course, if the board is without electricity for a while, this will fail.
Possible or not?

If the loop sets current time into setup every 10 seconds

This doesn't make sense. The loop() function should not call setup(). Without an RTC or PC, the "current time" is maintained by adding to a known time as the crystal ticks off time. The crystal is not terrible accurate, though.

that should keep the time going even if the board restarts.

No. The Arduino stops, and all data that it has is lost. Without a new baseline "current time", the "current time" will not be the current time.

Of course, if the board is without electricity for a while, this will fail.

As long as you define "for a while" to be a few nanoseconds or more, I'll agree with you. If you think that "for a while" means any measurable time, I'll have to disagree.

Like I said, I might be wrong.
OK, so it doesn’t work if the board restarts. Although I am not completely convinced this time…
But again, that was not OPs question.

Edit: BTW, got my clock ticking without any “time-modules”. It’s working.
Starting at 02:40, which is current time in Norway right now.

int s;
int m;
int h;
int ct;
int lt = 1;
void setup() {
  Serial.begin(9600);
  h = 2;
  m = 40;
}

void loop() {
  ct = (millis() - lt);
  while(ct<1000) {
    ct = (millis() - lt);
  }
  if(ct >= 1000) {
    s = s + 1;
    ct = 0;
  }
  if (s >= 60) {
    m = m + 1;
    s = 0;
  }
  if (m >= 60) {
    h = h + 1;
    m = 0;
    s = 0;
  }
  if (h >= 24) {
    h = 0;
    m = 0;
    s = 0;
  }
  if(h<=9) Serial.print("0");
  Serial.print(h);
  Serial.print(":");
  if(m<=9) Serial.print("0");
  Serial.print(m);
  Serial.print(":");
  if(s<=9) Serial.print("0");
  Serial.println(s);
  lt = millis();
}

enanthate:
The Arduino is able to count microseconds, so why can't this be used as time?

OK, I'll give you a stopwatch that can measure 10ths of a second.

Have nap, so you don't know what the current time is.

When you wake up, use the stopwatch to work out the time.

enanthate:
Like I said, I might be wrong.
OK, so it doesn't work if the board restarts. Although I am not completely convinced this time..
But again, that was not OPs question.

Edit: BTW, got my clock ticking without any "time-modules". It's working.

Pull the plug and let us know how it works. You've implemented a software RTC, very similar in concept to what the Time library does. If the power fails for a minute, three hours from now, the sketch will restart at 02:40.

You quoted my whole post, except the sentences containing questionmarks, "I might be wrong", and "Theoretically". Notice that I was only asking, not claiming.

If the loop sets current time into setup every 10 seconds

This doesn't make sense. The loop() function should not call setup(). Without an RTC or PC, the "current time" is maintained by adding to a known time as the crystal ticks off time. The crystal is not terrible accurate, though.

Really? Can you not at all imagine that a complete rookie was thinking there might be a way to change values in setup()?
Tbh, I still don't understand why that is not possible. Would be quite handy.

that should keep the time going even if the board restarts.

No. The Arduino stops, and all data that it has is lost. Without a new baseline "current time", the "current time" will not be the current time.

All data is lost? When I disconnect my Arduino, and connect it back in, the same program is running. It's not lost at all. Only the temporary data is lost, therefor handy to change the setup(), so that we could set the current time into setup() every 10 seconds. If there is an error to the board that makes it restart, this would be fine, and the clock would keep counting, being 10~seconds off.

Of course, if the board is without electricity for a while, this will fail.

As long as you define "for a while" to be a few nanoseconds or more, I'll agree with you. If you think that "for a while" means any measurable time, I'll have to disagree.

A bit of a small issue, but I don't understand why you keep saying agree and disagree. I was simply askin, so how can you agree or even disagree with me..?
To answer your "question"; when I suggested it would fail without electricity for a while, this was considering that the setup() was changeable(remember, I was asking if it was). If it would be without electricity for "a while", it would still kindof work, except that the time would be "a while" off. Again, considering the setup() was changeable. I was asking that.

OnTopic: Asking everyone now. In example, setup() says that x = 1. Is there any way a loop-code could change this, so that when the board restarts, x would be for example 0? Without the loop() changing x directly. Does this make sense?

I am quite aware of that, thank you anyway.

enanthate:
Does this make sense?

No. After you reset the Arduino it has no concept of what the current time is unless some outside source tells it. If you want to know the real-world time then a real time clock is by far the simplest way to achieve that. There are other ways possible, but they all involve the Arduino somehow being told what the current time is by some external source.

I see what you mean, it's quite obvious. Although, I don't see its relevance. Read the original question again.
Sorry, if you were referring to my sentence you quoted, let me rephrase it.
The Arduino is able to count microseconds, so why can't this be programmed to display time?
Hope this makes more sense.

PeterH: I understand that, and I absolutely agree on getting an RTC is way easier. But as the rookie I am, I am able to have fun with these simple challenges. This is a new world to me, and I'm learning everyday, and enjoying it.
It seems to me you don't quite understand my question, but I might be wrong. I mean, I was asking about changing x in setup(), not current time. :wink:
Like you say, the Arduino has no concept of what the current time is. That is, unless we give it the current time. I understand now that this is not possible, but wouldn't it be helpful if this actually was possible? Why is it not? For other situations as well, I can imagine it could come handy to change the setup (aka the STARTUP), so that whenever the Arduino restarts it could theoretically stay pretty much the way it was before restart, letting us lose as little data as possible. Am I the only one thinking this could be handy?

I'm tired, not thinking straight, and it's very late, Im out of here. Cya'll tomorrow :slight_smile:

And as I'm ending my night here on the forum, I might carefully suggest that we keep just a tiny littlebit more focus on questions and solutions. I'm not trying to be a douche, but lets face it, there are quite a few posts that couldv'e been avoided. XD

Good night! 8)

enanthate:
I was asking about changing x in setup(), not current time. :wink:
Like you say, the Arduino has no concept of what the current time is. That is, unless we give it the current time. I understand now that this is not possible, but wouldn't it be helpful if this actually was possible? Why is it not?

No, it's not making sense. What you're trying to do is not possible because it requires the Arduino to know the current time, and it has no way to know that unless it gets that information from somewhere, which is why we are suggesting solutions that enable it to do that.

One thing to be aware of if using the MilliSecond count from the Arduino is that it resets to 0 every 43 days or so....
Can be a trap for the unwary.

Thinking outside the box, another alternative would be a GPS chip....and a timezone offset in code.