Buzzer problem

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>

</mark> <mark>[/code]</mark> <mark>
tags added.

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.

Indeed meant a minute, not a second (10-60 secs according to various datasheet)

J-M-L:
Indeed meant a minute, not a second (10-60 secs according to various datasheet)

Yep, that sounds better. :slight_smile:

The (cheap Chinese ex-eBay) ones I use need ~35 seconds.

i have a sm104 pir
should i test with my multimeter if there's a 5v signal on my 08 port as soon as i plug the usb in?

joerack:
i have a sm104 pir

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.

Ok I've made a couple tests

I've tried connecting the 08 port to the breadboard, one end to the PIR and one end to the multimeter.

If the pir is connected I get immediately a 3.28v signal
if it's no connected, the port remains at 0v.

a delay of 5s solves the problem tho

there's two pots on the pir sensor. I don't know which one to change

joerack:
Ok I've made a couple tests

I've tried connecting the 08 port to the breadboard, one end to the PIR and one end to the multimeter.

If the pir is connected I get immediately a 3.28v signal
if it's no connected, the port remains at 0v.

a delay of 5s solves the problem tho

It's very likely that it will go high again shortly afterwards. As mentioned, the full calibration time is much longer than 5 seconds.

The 2 potentiometers you have won't change the need for calibration - suggest you wait for 30 seconds

One of the potentiometer is to set the dwell time of the HIGH state (how long it stays HIGH after triggering)

The second potentiometer is a trimmer to adjust the sensitivity

on the PIR boards I have the dwell time is labeled T with a letter next to it and Sensitivity S with a letter next to it