Skip to content

Instantly share code, notes, and snippets.

@circuitsenses
Created November 28, 2017 15:17
Show Gist options
  • Save circuitsenses/4dc61ef89324898dbe3182fe6714de04 to your computer and use it in GitHub Desktop.
Save circuitsenses/4dc61ef89324898dbe3182fe6714de04 to your computer and use it in GitHub Desktop.
PIR sesor with power saving
#include <avr/sleep.h>
const int32_t led_pin = 13;
const int32_t pir_input_pin = 2;
int32_t pir_state = LOW;
volatile int32_t pinval = 0;
void setup()
{
pinMode(led_pin, OUTPUT);
pinMode(pir_input_pin, INPUT);
Serial.begin(115200);
Serial.println("Armed");
Serial.flush();
attachInterrupt(digitalPinToInterrupt(pir_input_pin), pir_irq_handler, CHANGE);
}
void loop()
{
// put your main code here, to run repeatedly:
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_disable();
}
void pir_irq_handler()
{
sleep_disable();
pinval = digitalRead(pir_input_pin);
if(pinval == HIGH)
{
digitalWrite(led_pin, HIGH);
if(pir_state == LOW)
{
Serial.println("Target movement Detected");
Serial.flush();
pir_state = HIGH;
}
}
if(pinval == LOW)
{
digitalWrite(led_pin, LOW);
if(pir_state == HIGH)
{
Serial.println("Target Lost");
Serial.flush();
pir_state = LOW;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment