ultrasonic sensor + real time clock question

Hi, I've got an ultrasonic sensor, wave shield and DS1307 on my arduino. My project requires that the sensor only operate during the hours of 10:30pm and 7:30am. (it senses motion in an area and produces an audible message once triggered).

Ideally, I'd like the Arduino to enter sleep mode at 7:30am and then 'wake up' at 10:30pm. I have the sleep mode code and tried the sketch. It seems to work. But I don't know how to get the RTC to tell the Arduino when to go to sleep and when to wake up.

I have read many posts on the RTC but I get lost in understanding the code. Can someone give me a hint as to where I might start?

Here's my thought:

currenttime = read time from RTC waketime = 22 hr 30 min (on 24 hour clock) sleeptime = 7hr 30 min

if waketime == currenttime { wake up arduino }

if sleeptime == currenttime { put arduino to sleep }

As an alternative, I could also just disable the sensor or wave shield output during those times (saving power would be nice, but not a requirement). Either way, I don't know how to access the RTC current time and compare it to a variable. Any help would be appreciated.


Here is the hack solution ;)

buy a 24h timer switch, like the ones used for coffee machines, plug the arduino powersupply in the timer switch, adjust the timer...


This dude attached a photodiode to the wakeup pin, so the arduino goes to sleep when dark. You could modify this so tha arduino sleeps when light is on, so when you turn off the light, the arduino does its thing. http://accrochages.drone.ws/en/node/93

Then you use something similar to CMOS inverter http://en.wikipedia.org/wiki/CMOS

I think there are two aspects of your problem which seem to overlap.
Putting Arduino on sleep mode would mean putting the processor in a stopped state to reduce current drawing.
Anyway all the other components (RTC, wave shield, solid states on the board) would continue to absorb, maybe more than the processor itself.
You could then programmatically exit from the sleep state by mean of the internal watchdog timer which runs indipendently from the core.

Here is a complete example.

Beside that, maybe you wanted only to understand how to program an algorithm to activate or not a certain behavior according to the time of the day and that basically could be done with an if inside the loop.
The tricky part is that that means comparing the RTC with some constants, and because time from the RTC comes in “slices” (y m d h m s) you’ll need a
way to assemble and compare them as a whole, (well, in your case only hours and minutes).
Assuming that you are already reading from the RTC then this would be accomplished by something like

// boundaries, these won't change
const int start_time = 22 * 100 + 30;
const int end_time = 7 * 100 + 30;

// variable declaration
int current_time;

// a whole value to compare with
current_time = hours * 100 + minutes;

// inside the boundaries?
if (current_time >= start_time || current_time <= end_time) {
    if (something_happens) {
        // duty now..

I didn’t tested it, it comes without any guarantee… ::slight_smile:

You may want also to give a try to this library and related examples.

Let me know if that’s clear enough :slight_smile:

about the RTC code: http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock If you look at that site, sparkfun link. now in that code:

void getDateDs1307()
  // Reset the register pointer

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.receive() & 0x7f);
  minute     = bcdToDec(Wire.receive());
  hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(Wire.receive());
  dayOfMonth = bcdToDec(Wire.receive());
  month      = bcdToDec(Wire.receive());
  year       = bcdToDec(Wire.receive());

This is the intresting bit for you, globals get new values You are interessed in hour and minute variables after calling the function getDateDs1307(); in your loop

You can discard the " void loop() " used in the example and use your own but keep the bits over the loop, globals (variables) and functions to get and set time.

Just call getDateDs1307(); before comparing time values.


Guys, thanks for the help!

RC, yes, it will eventually be solar powered if I can get that to work reliably.