Built-in project · Arduino Uno R3

Alarm clock on an Arduino Uno

A DS1302 keeps the time and a TM1637 shows it; at 7:00 a piezo beeps until the button stops it. It starts at 6:59:50, so wait ten seconds. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino DS1302 RTC: an Alarm Clock With Code

Alarm clock · Arduino Uno R3live0.000 s 0.00x
Press Run, wait for 7:00, then press the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Alarm clock: a DS1302 clock module, a TM1637 display, a piezo and a button.
//
//   pins 4, 5, 2  the DS1302's DAT, CLK and RST (ThreeWire's IO, SCLK, CE)
//   pins 7, 6     the TM1637's CLK and DIO
//   pin 8         the piezo; its other leg to ground
//   pin 3         the button, to ground (INPUT_PULLUP: pressed reads LOW)
//
// The display shows the hours and minutes with the colon blinking once a
// second. At 7:00 the alarm beeps in fours until the button is pressed; the
// clock module was set to 6:59:50, so it goes off ten seconds after Run.

#include <ThreeWire.h>
#include <RtcDS1302.h>
#include <TM1637Display.h>

const int PIEZO = 8;
const int BUTTON = 3;
const uint8_t ALARM_HOUR = 7;
const uint8_t ALARM_MINUTE = 0;

ThreeWire myWire(4, 5, 2);
RtcDS1302<ThreeWire> Rtc(myWire);
TM1637Display display(7, 6);

bool ringing = false;
bool doneToday = false;
uint8_t lastSecond = 99;

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: alarm clock");
  pinMode(BUTTON, INPUT_PULLUP);
  display.setBrightness(0x0f);
  Rtc.Begin();
  if (Rtc.GetIsWriteProtected()) Rtc.SetIsWriteProtected(false);
  if (!Rtc.GetIsRunning()) {
    Serial.println("clock was stopped: starting it");
    Rtc.SetIsRunning(true);
  }
}

void loop() {
  RtcDateTime now = Rtc.GetDateTime();
  if (now.Second() != lastSecond) {
    lastSecond = now.Second();
    uint8_t colon = (now.Second() % 2 == 0) ? 0b01000000 : 0;
    display.showNumberDecEx(now.Hour() * 100 + now.Minute(), colon, true);
    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.println(now.Second());
  }

  bool alarmTime = now.Hour() == ALARM_HOUR && now.Minute() == ALARM_MINUTE;
  if (alarmTime && !doneToday && !ringing) {
    ringing = true;
    Serial.println("alarm!");
  }
  if (!alarmTime) doneToday = false;

  if (ringing) {
    // Four short beeps, then a rest, until the button.
    for (int i = 0; i < 4 && ringing; i++) {
      tone(PIEZO, 2000, 80);
      delay(150);
      if (digitalRead(BUTTON) == LOW) ringing = false;
    }
    delay(300);
    if (digitalRead(BUTTON) == LOW) ringing = false;
    if (!ringing) {
      noTone(PIEZO);
      doneToday = true;
      Serial.println("alarm off");
    }
  } else {
    delay(50);
  }
}

Parts list

6 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

How it is wired

9 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 8; Piezo speaker pin 1
  • Arduino Uno R3 pin 7; Four-digit display, TM1637 pin CLK
  • Arduino Uno R3 pin 6; Four-digit display, TM1637 pin DIO
  • Arduino Uno R3 pin 5; Real-time clock, DS1302 pin CLK
  • Arduino Uno R3 pin 4; Real-time clock, DS1302 pin DAT
  • Arduino Uno R3 pin 3; Pushbutton pin 1a
  • Arduino Uno R3 pin 2; Real-time clock, DS1302 pin RST
  • Arduino Uno R3 pin 5V; Real-time clock, DS1302 pin VCC; Four-digit display, TM1637 pin VCC
  • Ground: Arduino Uno R3 pin GND; Real-time clock, DS1302 pin GND; Four-digit display, TM1637 pin GND; Pushbutton pin 2a; Piezo speaker pin 2

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.