-3

I am building a clock with an Arduino.

Most clocks have a RTC module or some kind of RTC chip in order to get the time. I do not.

How do i replace this chip with software? Is there a C/C++ library equivalent to the Java library java.util.date?

Can I run a Java program instead of a C program on my Arduino so I can use this Java library? I only know Java and have little experience with C/C++. I have looked into ways to program this clock in Java which I have had success with, but this is only in my computer, not in the Arduino.

  • 2
    you have to do some research before posting a question .... i suspect that you have not bothered to do anything before posting your question because a web search for arduino timekeeping returns good info – jsotola May 28 '18 at 23:10
  • Can i run a java program instead of a c program on my arduino so i can use this java library? Huh? How does switching to the Java language solve the problem on how to get the time? On a PC, your PC would get the time over NTP via an internet connection. The Java library just gets that already stored value. It doesn't get the time for the pure reason it's now written in Java instead of C++. You can use the same mechanism however -- either use a network connection of some sort (e.g. WiFI) and get the time via an NTP / SNTP client, or program the time once and store it in an RTC peripheral. – Maximilian Gerhardt May 28 '18 at 23:18
  • you did not answer the comments in your previous question about this arduino.stackexchange.com/questions/53108/arduino-clock-project – Juraj May 29 '18 at 5:18
  • Possible duplicate of arduino clock project – Chris Stratton May 29 '18 at 14:10
  • 1
    Your question has been flagged for quality. Please see How do I ask a good question about the Arduino? – sa_leinad May 29 '18 at 15:27
4

how do i replace [an RTC] chip with software?

You can't. An RTC is made of basically two things:

  • a carefully trimmed crystal oscillator that has a very stable and well calibrated frequency
  • some logic to count the oscillations and convert that to time and date.

The logic can be easily replaced by software, but the crystal cannot. Any “pure software” solution you may find ultimately relies on counting cycles of the system clock (the signal that clocks the whole microcontroller). On most Arduinos, including the Uno, the system clock is based on a ceramic resonator. These are usually specified with 0.5% frequency tolerance, and they typically have a frequency offset of about ±0.1%. That's a drift of ±44 minutes per month!

You can get better accuracy by calibrating your Arduino's clock, and correcting by software the frequency offset you measure. However, the frequency is not only off, it is also quite unstable, and very sensitive to temperature variations. So even with calibration you end up with a clock that is ridiculously bad compared with the cheapest quartz watch.

is there a c++ library equivalent to the java library java.util.date?

I don't know Java, but there are C++ libraries available for managing time. For example, you could initialize Paul Stoffregen's TimeLib as follows:

setTime(hr,min,sec,day,mnth,yr);  // set initial time
setSyncInterval(-1);  // pretend we don't need to resync

and then periodically query the time with now(). That should be all you need to build a “proof of concept” of your clock: it will tell you the time, but with an awfully bad long-term accuracy.

Note that, once you add an RTC or another good time source, you can add a “time provider” to that library, keep the rest of your code unchanged, and benefit from the accuracy of your new time source.

Can i run a java program instead of a c program on my arduino [...]?

No, you can't. A Java runtime will never fit in your Arduino.

  • 1
    The resonator can be readily replaced with an actual 16 MHz crystal. Some older / kit boards already work that way, as do the arduino on breadboard instructions on the arduino site. The main disadvantage then is power consumption - but your basic Arduino project runs at full speed drawing full power all the time anyway. For the more advanced, activating the clock divider can minimize the amount of circuitry that runs at 16 MHz, peripherals can be disabled, and the processor core itself can be suspended leaving just a hardware timer counting ticks. – Chris Stratton May 29 '18 at 14:12
1

Having looked at you other question it seems you are trying to run before you can walk. You may be a good java programmer, but you need to get your head around C++ before you go all in and build a clock.

To answer this question, yes there are ways to get software to keep time, but when you power it up, how do you know what the time is? This is where having a RTC will help, there are plenty of bare ICs out there and even ones with libraries.

If you had a network then you could grab the header data from the network packets, but you are running this on a Mega, which seems an odd choice for something as simple as a network time clock, surly a nano would do, or an ESP8266.

These links will help you, but personally I think you need to break the problem down into smaller chunks.

https://www.arduino.cc/reference/en/

https://playground.arduino.cc/Code/Time

Not the answer you're looking for? Browse other questions tagged or ask your own question.