Hey guys, I'm using a PIR-Sensor for my Project, but most of the day I don't need the sensor, so I wanted to switch it off when my Arduino goes into sleep mode.
#include <avr/sleep.h>
#include <avr/power.h>
int inputPin = 2;
int val = 0;
int switchPin2 = 8;
void setup() {
pinMode(inputPin, INPUT);
pinMode(switchPin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(switchPin2, HIGH); //Here I want to switch the sensor on, to get the value of it.
delay(100);
val = digitalRead(inputPin);
if(val == HIGH){ //If the value is high, the arduino goes into sleep
delay(900);
pwrDown(10);
}else{
}
}
void pwrDown(int sekunden) {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); for(int i=0; i < sekunden; i++) {
//power_all_disable ();
digitalWrite(switchPin2, LOW); //Here I switch it off before the arduino goes to sleep
sleep_enable(); // sleep mode einschalten
sleep_mode(); // in den sleep mode gehen
sleep_disable(); // sleep mode ausschalten nach dem Erwachen
}
}
I cut out parts of my code that can't be the problem
My problem is that the digitalRead of the PIR-Sensor's input pin is always HIGH, even though there isn't anything moving.
connect the PIR without transistor, make sure it works.
connect the transistor to an LED, see whether you can switch the LED. Then at least you know the transistor circuit is correct, and can replace the LED with the PIR later.
By the way, I recall PIR sensors need some time from switching on before they actually work, a few seconds at least. Maybe that's getting in the way? You'll have wait long enough from switching on to taking measurement.