Building a Smart Alarm Clock - Arduino Project

Building a Smart Alarm Clock - Arduino Project

Tracking sleep patterns in home automation can be difficult without the right sensors. There are examples of people accomplishing this using sensors embedded into their bed sheets, and using the Bayesian binary sensor in combination with presence detection. These are great solutions, though they can be unreliable or intrusive. What I propose for my home automation system is a smart alarm clock.

The idea is to create an alarm clock using a WiFi enabled Arduino micro-controller, an OLED screen, some buttons and (on the software side) a finite state machine1 Now this is not just a regular alarm clock hocked up to the internet…that would be stupid. There is no need to Wifi enable objects without some functionality that makes life easier or saves time in some manner.

The clock is proposed to have a MQTT communication API allowing integration into the existing home automation setup. This opens a number of automation opportunities related to sleep/wakeup time, alarm time, as well as the setting of alarms (based on calendar events, for example). Finite State Machine Definition I attempted to trigger bed and wakeup time automations in the past through use on my phones charging state. Plugging in my phone usually (80% of the time) means that I am going to bed, and unplugging it means I am getting up. This works reliably, except of course when there are changes in my routine such as when I have friends over on a Friday night and playing music through Bluetooth drains my phone battery. At this point, plugging in my phone would have caused the entire house to go into sleep mode as the action of plugging in my phone means “bed time” for HA.

Creating this smart alarm clock, even if it’s just for the purpose of accurately tracking and triggering my bed and wake up times, will add a lot of functionality in itself and increase the reliability of automation triggers.

Requirements

There are a few high-level requirements for this alarm clock (some of which are based on the functionality of my existing alarm clock). I have split this section into software and hardware, because that’s how we roll. The golden rules of home automation still applies:

It has to be at least as useful as the existing alternative and it must enhance your experience in some way. Whether that is through time saved, the elimination of an interface or the ability to completely automate the existing interaction (making it redunant).

Hardware Requirements

  • Large screen (minimum 3 inch is diameter) to display time, alarm time and other relevant information.
  • large (>60mm diameter), unmissable snooze button on top
  • 2 additional buttons for controlling functions. Since we are implementing a finite state machine, I think we can get away with as little as 2 function buttons. (effectively named “select” and “next”, which control different actions depending on the state of the clock.2)
  • 2 alarm buttons (for turning 2 alarm presets on or off, like on my current alarm clock)
  • a loudspeaker or buzzer to produce a loud noise (it’s an alarm clock after all…)
  • light sensor (for dimming the display at night)

Software Requirements

  • display the current time, alarm time, date, weather and any other relevant information (such as how much sleep I am expected to get given the alarm time and current time, and possibly the next days calendar events and such)
  • ability to set an alarm
  • ability to snooze an alarm
  • ability to turn off an alarm
  • send MQTT messages to an existing broker, notifying the network of my wake up time (or bed time)
  • receive MQTT messages for setting alarms (useful for home automation).

Solution Components

The alarm clock requires a WiFi enabled Arduino chip with support for displaying graphics on an OLED screen. We will have to implement the clock’s firmware from scratch to integrate with MQTT, and a user-friendly (but relatively simple) UI will also need to be developed for this project. Since it is a clock this will include little more than text of varying sizes on screen along with a menu structure.

An MQTT API will also need to be defined to allow the control of clock functions from other devices.3

The brains of this alarm clock will be modelled using a finite state machine. There is an extremely useful tool I hope to use to create statecharts and convert them to Arduino code.

A custom Home Assistant component will make the integration of the alarm clock easier.

Use Case discussion

A sample use case demonstrating the need for this alarm clock follows. It is night time on a Thursday and I’m watching TV, there is light in the living room and the TV switch is enabled. It is time for bed though. Home Assistant has already set an alarm for the next day at 7am based on my work routine. As I enter the bedroom, I press the snooze button on the smart alarm clock. This sends an MQTT message through the network, alerting HA that it is “bedtime”. Based on this message, HA can trigger bedtime related automations, such as turning off lights in the living room, checking that all doors are locked and turning on the bedtime mood lighting in the bedroom. This mood lighting fades to darkness over the course of a few minutes, fully automated.

Alarm Mockup Idle State

As my head hits the pillow, I can read the current time in big, easy to read numbers, and beneath the alarm time for the following day. An icon in the lower left indicates tomorrows weather as well as the expected low and high temperatures.

I get comfortable and realize that I need to set a special alarm for Saturday. I press the “select” button and a menu appears with a few options related to setting alarms. I press the “next” button to navigate to the “Set unique alarm” option and press “select”. A different screen appears that takes me through the process of entering alarm information. This alarm will only sound on Saturday as it recurrance option is set to “single occurance, on Saturday”. I confirm the alarm by pressing “select” and the clock goes back to being a clock, displaying the time, which is 10:39pm. The lower right corner of the display displays 8:21h, being the amount of time left before the alarm sounds.

Alarm Mockup Set Alarm State (draft)

I turn around and take note of the dimming function of the display as the mood lighting fades out. After some time I fall asleep. The next day at 7am I am woken by the sound of the alarm clock. I press the big snooze button once - adding 5 minutes, and I press it again – to add another 5 minutes snooze time. That is 10 minutes snooze time. Later, the alarm sounds again and I press the “select” button to turn off the alarm.

Alarm Mockup Alarm State

The clock sends another MQTT message, this time indicating that I am getting up. HA triggers automations related to my morning routine, such as turning on lamps in appropriate areas of the house (such as the kitchen) and turning on the radio to play the Sky News podcast, followed by music from my “Easy Listening” playlist on Spotify.

Conclusion

This post outlines high level requirements and how this smart alarm clock will not only be a fun electronics project and challenge on the software side, but also a genuinely useful addition to my home automation setup.

  1. This is just a software design pattern that helps model state-based behavior for systems that can be in one of many states at any point in time. It creates cleaner code that is easier to enhance and maintain. ↩︎

  2. Example: In idle state, the “select” button would trigger the menu. In this new “menu” state, the “next” button allows scrolling through menu items and the “select” button allows selection of a particular item. On the “Set Alarm Screen”, the two buttons allow selection of alarm hour, minute and repeat mode. I love the concept of states, as we can use the same hardware and reuse it for different software purposes, depending on the state the system is in. ↩︎

  3. MQTT is a simple network communication protocol. Adding this feature to the clock allows controlling the clock through automations from Home Assistant. We could write a custom HA component for this clock to help represent it better in the HA system. Since MQTT is a universal communication protocol, next to any network enabled device can be programmed to communicate with the clock. For example, we might decide to build a mobile app or set alarms using voice commands (via snips.ai) at a later point in time. ↩︎