Hi all
Ive tried this simple pir buzzer code.
Problema is that as sono as i connection the usb the buzzer goes off even if im not facing the pir.
Pls help
//Declaração das variáveis referentes aos pinos digitais.
int pinBuzzer = 7;
int pinSensorPIR = 8;
int pinLed = 9;
int valorSensorPIR = 0;
void setup() {
Serial.begin(9600); //Inicializando o serial monitor
//Definido pinos como de entrada ou de saída
pinMode(pinBuzzer,OUTPUT);
pinMode(pinSensorPIR,INPUT);
pinMode(pinLed,OUTPUT);
}
void loop() {
//Lendo o valor do sensor PIR. Este sensor pode assumir 2 valores
//1 quando detecta algum movimento e 0 quando não detecta.
valorSensorPIR = digitalRead(pinSensorPIR);
Serial.print("Valor do Sensor PIR: ");
Serial.println(valorSensorPIR);
//Verificando se ocorreu detecção de movimentos
if (valorSensorPIR == 1) {
ligarAlarme();
} else {
desligarAlarme();
}
}
void ligarAlarme() {
//Ligando o led
digitalWrite(pinLed, HIGH);
//Ligando o buzzer com uma frequencia de 1500 hz.
tone(pinBuzzer,1500);
delay(4000); //tempo que o led fica acesso e o buzzer toca
desligarAlarme();
}
void desligarAlarme() {
//Desligando o led
digitalWrite(pinLed, LOW);
//Desligando o buzzer
noTone(pinBuzzer);
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
You can check your code by just using a wire you plug and unplug from 5v to see if your program works. If that works then you know you have a pb with the PIR sensor
--> Have you checked your Pir sensor works? Which one do you have? Is the motion trigger level configurable with a potentiometer on the board (adjustable sensitivity)? How is this potentiometer set? How are you powering it? Some PIR need 5v or more, some output 3v - is that wired the right way? Some PIR have a,build in adjustable delay before they turn off. How is that set?
Then on software side Usually it's good practice to let the PIR sensor calibrate itself for a second minute before starting reading data from it
J-M-L:
Then on software side Usually it's good practice to let the PIR sensor calibrate itself for a second before starting reading data from it
Actually, it takes a lot longer than a second. Most PIR detectors need about 20 to 40 seconds after power-up to calibrate, during which time they will false-trigger a couple of times.
All that's needed is a plain old 'delay()' in setup. It doesn't take long to work out how long the delay should be.
That looks identical to the ones I have here. They use a BS0001 chip.
The best way to detemine the calibration period is by trial and error. Before doing anything more complex, just have the Arduino light an LED when the PIR output is high. Then, time the period after power-up that it takes to settle to a constant low output. Set the "sensitivity" pot to about mid-position for this.
Then, in 'setup()', use a delay about 5-10 seconds longer just to be sure. (The calibration period might vary a bit with temperature. I've never bothered to test for that.)
Edit: You edited while I was typing.
should i test with my multimeter if there's a 5v signal on my 08 port as soon as i plug the usb in?
During calibration, the output goes high a couple of times. Just time until the last time it goes low. A couple of power cycles will be needed to determine which is the last time. You'll soon get the feel of it.