Hardware: PIR motion sensor (https://www.adafruit.com/products/189), Arduino UNO and a piezo
Please see included connection diagram (using Fritzing) and the code.
Problem:
When I use the Arduino DC jack/barrel to supply power (either via battery 9v or wall (usb) adapter 5v), it seems like the motion sensor is being triggered all the time, i.e. the buzzer/LED stays on even after movement stops. I have to unplug the power supply from the DC jack to stop it.
When I supply power to Arduino either via the Arduino usb port (connected to computer) or via Arduion's Vin and GND (battery), all works fine, i.e. senses motion, buzzer beeps/LED on; no more motion, buzzer stops/LED off.
PIR sensor Time dial setting is min, Sensitivity dial setting is max. I randomly tried different Time and Sensitivity positions, the problem remains.
I am not sure what I did wrong. I found this post viewtopic.php?f=25&t=19057&p=98770&hili ... wer#p98770, and wonder if it is related.
Any thoughts/ideas would be appreciated.
Code:
Code: Select all
/*
Ref. to learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir
*/
const int piezoPin = 9;
const int ledPin = 13; // choose the pin for the LED
const int pirPin = 2; // choose the input pin (for the PIR sensor)
int val = 0;
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare PIR sensor pin as input
pinMode(piezoPin, OUTPUT);
}
void loop()
{
int val = digitalRead(pirPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if motion detected
beep();
delay(100);
digitalWrite(ledPin, LOW); // turn LED off
delay(500);
}
}
void beep()
{
tone(piezoPin, 500); // using tone() interferes with pwm pin 3 and 11
delay(150);
noTone(piezoPin);
delay(500);
}