RTC Arduino Module Tutorial: How to Pair a Real Time Clock

Pubblicato da

Arduino boards and similar microprocessors can accomplish a wide variety of tasks. If, for example, you want to turn on an LED once per second, use the delay() or millis() commands. Unfortunately, small variations in a system's resonator or crystal oscillator may cause the board to keep uneven time.

All About Arduino SerialWhile this time variation is miniscule—a .05% (.0005x) deviation from the actual time—the lag eventually adds up. For example, if you're trying to make an alarm clock, a .05% variation would mean that your system varies from reality by 43.2 seconds per day. That won't make a difference for the first week, but when you're getting to work 20 minutes late after a month, you might start to get annoyed.

So, what is one to do? The answer, of course, comes in the form of an RTC, or real-time clock module. Options include the PCF8523 and DS1307, as well as the DS3231 chip we'll discuss below.

Connect a Real Time Clock (RTC) Module to Arduino

If you want to get started with an RTC module and Arduino, I recommend using a breakout board with the following features:

- DS3231 chip

- Connections that go to the Arduino

- Holder and connections for a coin-cell (CR2032) battery

The battery keeps it ticking even without external power, so if you have to shut down the main system, it will still read the correct time when you power it back on. You can also use a real time clock module to wake your processor up at set intervals for power savings.

For this setup, hook things up as shown here on an Arduino Uno board, connecting 5V and ground to the appropriate connections, and the SDA and SCL lines to the corresponding Arduino connections. On the Uno, A4 is SDA and A5 is SCL, but the lines are also conveniently broken out here next to the reset button where they connect in the picture above. Plug in a CR2032 battery to keep the chip "ticking."

RTC usage_IMAGE 2

RTC usage_IMAGE 3

Program Your RTC Arduino Device

Here's the process:

1. Install the Adafruit RTC library through the Arduino IDE search function or via this GitHub page.

2. Navigate to File - Examples – RTClib and open ds3131.

3. Open a serial monitor for feedback, then upload the program to your Arduino board.

The first time you do this, the serial monitor will note: "RTC lost power, let's set the time!" This loads the time into the real time clock module based on when the sketch was compiled. After that, it will display the current time/date, time since midnight 1/1/1970, and the temperature.

The time will generally be off by several seconds, because it gets the time from the computer when the program compiles. You can also manually set the time by adjusting the rtc.adjust line with a specific date.

If you want to input the time again, disconnect the module and remove the battery. Plug everything in again and reprogram your Arduino as described above.

RTC usage_IMAGE 4

Battery backup keeps the RTC module on time even when the main power is disconnected.

Clocking Your Project

Now you're ready to get started experimenting with more accurate Arduino RTC timekeeping. Try creating your own timekeeping device or automating processes around your home or business. With more development, you're free to explore endless product possibilities.

Visualizza prodotti correlati

CR2032

BeStar Technologies Batterie Visualizza

Visualizza prodotti correlati

A000066 | Arduino Uno Rev3

Arduino Corporation Sistema integrato, schede e kit di sviluppo Visualizza

Ultime notizie

Sorry, your filter selection returned no results.

Non perderti le ultime novità sull'elettronica

Abbiamo aggiornato la nostra politica sulla privacy. Si prega di prendere un momento per rivedere questi cambiamenti. Cliccando su Accetto, l'utente accetta la Politica sulla privacy e Condizioni di utilizzo di Arrow Electronics.

Il nostro sito web mette i cookies sul vostro dispositivo per migliorare la vostra esperienza e il nostro sito. Leggete altre informazioni sui cookies che usiamo e su come disabilitarli qui. I cookies e le tecnologie di tracking possono essere usati per scopi commerciali.

Con un click su “Accept”, voi consentite l'inserimento dei cookies sul vostro dispositivo e l'uso da parte nostra di tecnologie di tracking. Per avere altre informazioni e istruzioni su come disabilitare i cookies e le tecnologie di tracking, clickate su “Read More” qui sotto. Mentre l'accettazione dei cookies e delle tecnologie di tracking è volontaria, una loro disabilitazione potrebbe determinare un funzionamento non corretto del sito web, ed alcuni messaggi di allarme potrebbero essere per voi meno importanti.

Noi rispettiamo la vostra privacy. Leggete qui la nostra politica relativa alla privacy