I have managed to rig a pir sensor to the bored but i couldn't get 2 or more to work. Each sensor runs on 5 volts and has a ground and a signal wire that sends a 2 or 3 volt signal or something like that. I have ran both sensors off a 5 volt power supply in parallel. I have tried to run the signal wires into one that i plug into one input on the bored. But that did not work at all. haha i got the code off make.com but its only for one pir sensor. id like to code it for 2 sensors (or 2 inputs). Can yall shed some light on this? i know vary little about the code but here it is. any help would be greatly appreciated.
// Uses a PIR sensor to detect movement, buzzes a buzzer
// more info here: PIR Sensor Arduino Alarm | Make:
// email me, John Park, at jp@jpixl.net
// based upon:
// PIR sensor tester by Limor Fried of Adafruit
// tone code by michael@thegrebs.com
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
playTone(300, 160);
delay(150);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
playTone(0, 0);
delay(300);
if (pirState == HIGH){
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
Two sensors on one pin makes it challenging to know which one triggered the input. Two sensors on two pins is trivial. What problems are you having?
Using names that mean something is a big help in programming. inputPin means nothing, really. WHAT is connected to the pin? pirOnePin clearly says what is connected to the pin, and makes it far easier to see how to add another PIR sensor. The second one would be connected to the pin defined in jaskIsLateForWork, right?
ok thank you! the problem i am having is that i would like to program the bord to have 2 input pins, one for each pir sensor. right now i have one input for the sensors an it dose not work. and i am a nube to coding
I'm sorry but It's not clear that your question was answered.
I'm, in fact, working on something similar. It sucks to think I can't put more than one sensor on an input.
I don't care to know which sensor triggered it ( at least not at this time ). I just want a pair (or three) to trigger an identical response.
I'm a seasoned coder and it's not clear to me, either, that this is even possible.
You're not alone...
however, I've gotten a single PIR to work VERY well. So I can put some time in if you need further assistance.
It sucks to think I can't put more than one sensor on an input.
You should be able to if you use a diode between each sensor and the pin to prevent them interfering and trying to pull the pin both HIGH and LOW at the same time. Note that I have not tried this.
I'm a seasoned coder and it's not clear to me, either, that this is even possible.
Sure it is. You can connect several echo pins together, and read them with the same code. You won't know which one went high, which seems like crucial information to me.
But, if you are doing something like a backup beeper, and just want to know that you need to stop because something, somewhere, got too close, you could read all the echo pins together.
I'd start with something like this.. just to see how it works:
byte led = 13; // choose the pin for the LED
byte pir1 = 2; // choose the input pin (for PIR sensor)
byte pir2 = 3; // choose the input pin (for PIR sensor)
byte pir3 = 4; // choose the input pin (for PIR sensor)
byte pirState; // we start, assuming no motion detected
byte Speaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup()
{
pinMode(led, OUTPUT); // declare LED as output
pinMode(pir1, INPUT); // declare sensor as input
pinMode(pir2, INPUT); // declare sensor as input
pinMode(pir3, INPUT); // declare sensor as input
pinMode(Speaker, OUTPUT);
Serial.begin(9600);
}
void loop()
{
pirState = digitalRead(pir1) + digitalRead(pir2) + digitalRead(pir3); // read 3 inputvalues
if (pirState > 0) // check if input(s) is HIGH
{
digitalWrite(led, HIGH); // turn LED ON
tone(300,160); // short 'pip'
Serial.println("Motion detected!");
delay(150);
}
else
{
digitalWrite(led, LOW); // turn LED OFF
delay(300);
}
}