Arduino + motion detector + led

Parts :

– 1 x Arduino nano

– 1 x led high lumi

– 1x R 330 Ω

-1 x R 1kΩ

– 1 x Transistor NPN 2N2222

– 1 x HC SR501 PIR motion detector

ledinfra_bb


#include
#include

void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
digitalWrite(2, LOW);
pinMode(5,OUTPUT);

digitalWrite(5,HIGH);
delay(5000);
digitalWrite(5,LOW);
delay(200);
}

void loop()
{
//Come out of sleep and read state of PIR pin
bool flag = digitalRead(2);
if (flag == true)
{
Serial.println("tripped");
digitalWrite(5,HIGH);
}
else
{
Serial.println("not tripped");
digitalWrite(5,LOW);
}
delay(100);
sleepNow();
}

void sleepNow()
{
Serial.println("Entering Sleep");
// Set pin 2 as interrupt and attach handler:
attachInterrupt(digitalPinToInterrupt(2), wakeUp, CHANGE);
delay(100);
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_PWR_DOWN);

// Set sleep enable (SE) bit:
sleep_enable();

// Put the device to sleep:
sleep_mode();

// Upon waking up, sketch continues from this point.
sleep_disable();
}

void wakeUp(void)
{
Serial.println("wake up");
detachInterrupt(digitalPinToInterrupt(2));
}