1

So the new Pi has a built in RTC and for $5 I can attach a rechargeable battery that lets the RTC tick on even when the power is cut. This is great because I want to emulate the wake up behavior of an additional device like the often out of stock PiJuice (but at a fraction of the cost).

I learned how to set wakeup alarms with code like this:

$ sudo sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm" 
$ sudo sh -c "echo `date '+%s' -d '+ 3 minutes'` > /sys/class/rtc/rtc0/wakealarm"

which will set an alarm to turn the pi back on. But I can't find many more details about using this wake alarm

Questions:

  1. If the /rtc0/wakealarm is set to a time in the past, what is the
    behavior? Does it ignore it? Does it re-start immediately?

  2. Can one set multiple wakealarms at the same time? ie. wake up at 4:20am and 5:45am and 6:30am

  3. Is there an easy way to schedule wakeups in a format similar to cron where I give the wakealarm a format like 0 0 * * 1-5 to tell it to only wake up on weekdays?

  4. linux has a command called rtcwake which seems more featured, but when I tried using examples i found around the internet on a pi, i get errors. Can a pi use rtcwake?

3
  • from my tests, if i set a wakealarm for 1 minute in the future, and then wait 5 minutes, and then shut down the pi, it seems to turn itself back on. So from my tests it seems like the answer to question 1 is: re-start immediately. Can anyone confirm?
    – blorgggg
    Commented Apr 22 at 20:14
  • 1
    Have you looked at the dtoverlay documentation?. There are some RTC options available there that may interest you. I've not yet tried the Pi5's inbuilt RTC, so I can't comment on that, but I assume it's treated like most of the other RTCs supported in the device tree.
    – Seamus
    Commented Apr 22 at 20:53
  • Thanks for sharing that! yeah i do see some documentation of it, but nothing that seems to help with these questions.
    – blorgggg
    Commented Apr 23 at 18:49

2 Answers 2

1

Thanks to @seamus for lots of good advice. I ended up writing a script and figuring out the answers to these, so I'll try to post a consolidated answer set here. Q3 is the most useful because it shows working code for programming and scheduling a Pi5 to wakeup like you previously only could do with additional hardware like a pijuice.

Q1 - If the /rtc0/wakealarm is set to a time in the past, what is the behavior? Does it ignore it? Does it re-start immediately?

For the Pi 5, it seems like if a wakealarm is set in the past, and you shut down the pi, it will immediately wake itself back up. This might not be true for other devices, but in my testing on Bookworm, this is what happens

Q2 - Can one set multiple wakealarms at the same time? ie. wake up at 4:20am and 5:45am and 6:30am

no, the wakealarm is pretty much just a holder of a single date in epoch time, or empty if nothing is set. You cannot set multiple dates. Thanks @seamus for extra confirmation about this

Q3 - Is there an easy way to schedule wakeups in a format similar to cron where I give the wakealarm a format like 0 0 * * 1-5 to tell it to only wake up on weekdays? No, until now! I wrote a script that I had previously used for programming a pijuice from an easily editable CSV file, and i converted it to use a cron - like notation for scheduling series of wakeups. It looks at your cron-like command, finds the soonest date, and sets the wakeup to that. You can have this script run at @reboot and it will keep your wakeup schedule for your device

I made my own little script for people to use to easily schedule your Pi5 auto-wakeup in a cron-like style! https://github.com/Digital-Naturalism-Laboratories/Mothbox/blob/main/Software/Scheduler_Pi5.py

Q4 - linux has a command called rtcwake which seems more featured, but when I tried using examples i found around the internet on a pi, i get errors. Can a pi use rtcwake?

Thanks again @seamus for your advice here. yeah it doesn't seem like you can.

1
  • 1
    Sorry I've been so long in following up - if you left me a message when you posted this I didn't see it. Anyway... This looks great! It's interesting to contrast your software approach against my hardware approach. I was going for a true zero-power state, and the only way to achieve that with older Pis is to actually remove the power (which I did with a 'latching relay'). AIUI, the Pi5 allows you to enable a power save mode which gets power consumption really low - but not completely OFF :(
    – Seamus
    Commented May 19 at 4:38
1

You may find more details through searching, but I learned (mostly) through trial and error. I may be able to help with one or two of your questions, but this is based on my experience with the DS3231 RTC chip which I've used exclusively for my RTC applications.

Q1: If the /rtc0/wakealarm is set to a time in the past, what is the behavior? Does it ignore it? Does it re-start immediately?

This will depend on the RTC chip used, and maybe on the driver implementation as well; for the DS3231 on Raspberry Pi: it is ignored.

Q2: Can one set multiple wakealarms at the same time? ie. wake up at 4:20am and 5:45am and 6:30am

Some time ago I read what I took to be a definitive statement from a kernel developer who worked on the RTC drivers. He said something to the effect that, "Multiple (RTC) alarms will never be supported in Linux". Take that for what its worth... some kernel devs are fascists, so it may or may not have changed.

That said, the DS3231 chip does have two alarm registers, and the times stored in those registers may be declared completely independently of one another. In this case, it is the Linux driver which prevents setting multiple wakealarms; if you're not using the Linux driver, then the answer is, "Yes, one can set multiple alarms at the same time."

Q3: Is there an easy way to schedule wakeups in a format similar to cron where I give the wakealarm a format like 0 0 * * 1-5 to tell it to only wake up on weekdays?

Like Q2, there are two answers - depending upon whether you're using the Linux driver or not. Unless it's been very recently changed, the only format for the wakealarm file (i.e. using the Linux driver) is in epoch time. You can verify this is the latest information by reading the current documentation in the README file for the i2c-rtc overlay.

Internal to the DS3231, the alarm times are formatted iaw BCD; this is described in detail in the DS3231 data sheet.

Q4: linux has a command called rtcwake which seems more featured, but when I tried using examples i found around the internet on a pi, i get errors. Can a pi use rtcwake?

No. rtcwake is a command for "real computers" - which the Raspberry Pi has not yet manufactured. The RPi5 represents a step in that direction, but is not there yet.

2
  • It's weird, I feel like when the RPI 5 announced that it would have a built in RTC everyone would be super excited and posting lots of things about using it, but I have only really seen a couple posts in the internet from like october and november talking about how to possibly use it, but not much more. Anyway, I'm looking forward to helping contribute something to hopefully answer Q3 :) Thanks for your other advice, im going to study these things a bit more too!
    – blorgggg
    Commented Apr 23 at 23:01
  • 1
    I'll look forward to hearing of what you learn.
    – Seamus
    Commented Apr 24 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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