Waking up an Arduino with Input from a Sensor

Currently i am investigating ways to wakeup an Arduino out of deep sleep mode with an IRQ Event. If you think of low power standalone measurement applications, this can be very interesting, because the board can sleep with very low power-consumption (less than 10 milli-amps) until something happens.

It works that way (see code example below): After starting the Arduino you call the function sleepNow() which does two things. Define an interrupt for waking up and attach him and then put the device to sleep. For waking up different patterns can be defined, “LOW”, “RISING” and “FALLING”. If the according event is being observed on the wakeup pin the device will wakeup, proceed with the wakeup function (in this case wakeUpNow()) and then execute the loop again.

In my example i am using a tiny solar cell that can output up to 4volts under full sunlight to trigger the RISING-Event on PIN2. The interesting detail about that is that this sensor can power itself obviously, therefore the Arduino could even shut down it’s PINs during sleep and would still wakeup. This setup could be used to turn on a sensor if a box is opened for instance (think of a intrusion detection in your house or a smart geo-cache for instance). In combination with our ArduFona Board this could then trigger a GSM action or call a WEB-API etc.

In my future research i will try to identify more sensors that can be put to work like this – which is not as trivial as you might think because most sensors are working with a more complex output like I2C or do not provide the right output by other means in the first place. But i think many cases can indeed work like this, ultrasonic-sensors for distance, acceleration sensors with analog outputs etc. – i’ll keep this blog posted.

See it in full operation here:

Here is the code for the above example:

#include <avr/sleep.h>
#define buzzer 9
#define led 13

int wakePin = 2; // pin used for waking up

void wakeUpNow() // here the interrupt is handled after wakeup
{
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, HIGH);
delay(4000);
digitalWrite(buzzer, LOW);
delay(100);
}

void setup()
{
Serial.begin(9600);
pinMode(wakePin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
digitalWrite(led, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(led, LOW);
}

void loop()
{
digitalWrite(led, HIGH);
digitalWrite(buzzer, HIGH);
delay(1000);
digitalWrite(led, LOW);
digitalWrite(buzzer, LOW);
sleepNow(); // sleep function called here

}

void sleepNow() // here we put the arduino to sleep
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
attachInterrupt(0,wakeUpNow, RISING); // use interrupt 0 (pin 2) and run function
sleep_mode(); // here the device is actually put to sleep!!
sleep_disable(); // first thing after waking from sleep:
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
}

About holadiho

stephannoller.eu
This entry was posted in Arduino and tagged . Bookmark the permalink.

2 Responses to Waking up an Arduino with Input from a Sensor

  1. Maik says:

    Any News?

    Like

  2. Malcolm says:

    Good day Holadiho, I would like to implement some of your code in a hoby project I’m working on. The problem I have is using “delay” I would like to replace it with a “timer” function duration of 10 minutes, which can’t be done with “delay” because I am only able to get a duration of 10sec. which is not long enough for my project. I am very new to both code and the Arduino. I have been working on this for the past few months on and off and now at lest understand some of it. Would you be able to provide code to replace the delay in “waking up an Arduino with input from a sensor please. Any help would be deeply appreciated, thanks in advance.

    Like

Leave a comment