Ds1302: an example that runs
Yes. You can use the Ds1302 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Real-time clock, DS1302). The example below, "Start a halted DS1302 and read it each second", runs on the Arduino Uno R3.
Built in on Mokxi: #include <Ds1302.h> and it builds, with nothing to install. Free, and no account needed.
What Ds1302 does
The DS1302 clock on three pins: Ds1302 rtc(ena, clk, dat), init(), getDateTime() and setDateTime() in one burst each, isHalted(), halt() and start(). That is what Ds1302, by Rafa Couto, covers. The sketch below uses it on the Arduino Uno: “Start a halted DS1302 and read it each second”.
The Ds1302 library's interface (Rafa Couto, 1.1.0), for the Maxim DS1302 clock on its three-wire interface.
Use the ds1302 part: its CLK, DAT and RST pins on the three pins the constructor names (RST, CLK, DAT in that order). The clock, the halt bit, write protect and the bursts are all modeled.
The calls follow release 1.1.0 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.
| Library Manager name | Ds1302 |
|---|---|
| Author | Rafa Couto |
| Include | #include <Ds1302.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Real-time clock, DS1302 |
Install it
It builds on every board Mokxi compiles for but ATtiny85.
In Mokxi it is built in: #include <Ds1302.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "Ds1302" by Rafa Couto from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Ds1302 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.0, so a sketch written here builds there too.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
The calls you will use
In the order a sketch meets them.
Ds1302 rtc(4, 2, 3)
Makes rtc, the Ds1302 object the calls below are made on.
rtc.init()
Starts it up. Call it once in setup().
rtc.isHalted()
Returns true or false for "is halted".
rtc.setDateTime(&dt)
Sets the date time to the value you pass in.
Example: Start a halted DS1302 and read it each second
On the Arduino Uno R3. It runs in the browser exactly as printed.
#include <Arduino.h>
#include <Ds1302.h>
// DS1302 RTC instance: RST (ENA), CLK, DAT
Ds1302 rtc(4, 2, 3);
const static char* WeekDays[] = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};
void setup() {
Serial.begin(9600);
// initialize the RTC
rtc.init();
// test if clock is halted and set a date-time to start it
if (rtc.isHalted()) {
Serial.println("RTC is halted. Setting time...");
Ds1302::DateTime dt = {
.year = 26,
.month = Ds1302::MONTH_SEP,
.day = 17,
.hour = 13,
.minute = 45,
.second = 0,
.dow = Ds1302::DOW_THU
};
rtc.setDateTime(&dt);
}
}
void loop() {
// get the current time
Ds1302::DateTime now;
rtc.getDateTime(&now);
static uint8_t last_second = 0;
if (last_second != now.second) {
last_second = now.second;
Serial.print("20");
Serial.print(now.year);
Serial.print('-');
if (now.month < 10) Serial.print('0');
Serial.print(now.month);
Serial.print('-');
if (now.day < 10) Serial.print('0');
Serial.print(now.day);
Serial.print(' ');
Serial.print(WeekDays[now.dow - 1]);
Serial.print(' ');
if (now.hour < 10) Serial.print('0');
Serial.print(now.hour);
Serial.print(':');
if (now.minute < 10) Serial.print('0');
Serial.print(now.minute);
Serial.print(':');
if (now.second < 10) Serial.print('0');
Serial.print(now.second);
Serial.println();
}
delay(100);
}Wiring
- Arduino Uno R3 2 to Real-time clock, DS1302 CLK
- Arduino Uno R3 3 to Real-time clock, DS1302 DAT
- Arduino Uno R3 4 to Real-time clock, DS1302 RST
- Arduino Uno R3 5V to Real-time clock, DS1302 VCC
- Arduino Uno R3 GND to Real-time clock, DS1302 GND
Try Ds1302 without installing anything
Open the example, change the sketch, and keep your version in a free account.