Hello!
I am pretty stressed, because my works deadline is coming very fast and I have nothing at this moment
My task is to measure speed with 2 PIR sensors.
Is it possible to do with arduino uno, knowing distance between of zones? I know that I must somehow limit PIR sensing zone, probably with some kind of tube, to get more precise spots where it starts to sense.
I got these kind of PIR: VMA314: PIR MOTION SENSOR – Velleman – Wholesaler and developer of electronics
If it is possible, can someone give code example to start with?
Speed = time per distance. Measure the time it takes between the two sensors getting triggered (record the time using millis() for both), you know the distance, calculate the speed.
And yes you'll have to do a lot of shielding, and you'll only be able to measure the speed of objects much warmer than ambient (such as people).
Thank you for your answer.
Target is to measure cars speed. It does not have to be very accurate, because this is "scientific experiment" and I need to find out how much accurate PIR sensors could be.
What I am hoping to get here is some code examples, because I am total newbie in Arduino and coding.
Do cars trigger your PIR? Did you try that already?
I understand that typical school assignments very often put you to work with some of the worst imaginable tools for the job - but in this case the first thing you should do is try whether you can even register a car. After that you can get a second one, do your (VERY inaccurate) speed measurements, hand it in, and move on.
Lots of code examples out there, just a quick Google query away. Do your own homework.
PIR sensors are probably the worst sensor for a 'speed' gate.
They are designed to react slowly to changes, to stop false triggers.
Probably just ok for grannies in a walking frame.
If you use the sensors you linked to,
then first turn the 'time' pot fully couterclockwise, and the 'sens' pot fully clockwise.
That will give you the shortest reaction time and the shortest 'stay on' time.
Leo..
// First sensor
int led1Pin = 13; // choose the pin for the LED
int pirSensor1 = 5; // choose the input pin (for PIR sensor)
int pirState1 = LOW; // we start, assuming no motion detected
int val1 = 0; // variable for reading the pin status
// Second Sensor
int led2Pin = 12; // choose the pin for the LED
int pirSensor2 = 9; // choose the input pin (for PIR sensor)
int pirState2 = LOW; // we start, assuming no motion detected
int val2 = 0; // variable for reading the pin status
int led3Pin = 8; // choose the pin for the LED
int loopStatus = 0;
unsigned long pir_time1 = 0;
unsigned long pir_time2 = 0;
unsigned long timeCalculation = 0;
unsigned long everyLoopTime = 0;
int pirSensorActiv = 0;
unsigned long timeBetweenSensors = 0;
int sensorState = 0;
// settings
unsigned long settings_sensorSkipTime = 3000;
unsigned long settings_mainLedDelay = 3000;
unsigned long settings_minimumDelay = 50;
volatile int start =0;
volatile int revs = 0;
void setup() {
pinMode(led1Pin, OUTPUT); // declare LED as output
pinMode(led2Pin, OUTPUT); // declare LED as output
pinMode(led3Pin, OUTPUT); // declare LED as output
pinMode(pirSensor1, INPUT); // declare sensor as input
pinMode(pirSensor2, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
everyLoopTime = millis();
// Checking first sensors state
timeCalculation = everyLoopTime - pir_time1;
// if time difference is greater than skip time, than turn on LED and count time
if(timeCalculation > settings_sensorSkipTime){
sensorState = digitalRead(pirSensor1);
if(sensorState == HIGH){
digitalWrite(led1Pin, HIGH);
pir_time1 = millis();
pirSensorActiv = pirSensorActiv + 1;
}else{
digitalWrite(led1Pin, LOW);
pirSensorActiv = 0;
}
}else{
sensorState = digitalRead(pirSensor1);
if(sensorState == HIGH){
digitalWrite(led1Pin, HIGH);
pirSensorActiv = pirSensorActiv + 1;
}else{
digitalWrite(led1Pin, LOW);
pirSensorActiv = 0;
}
}
// Checking first sensors state
timeCalculation = 0;
timeCalculation = everyLoopTime - pir_time2;
// if time difference is greater than skip time, then turn on LED and count time
if(timeCalculation > settings_sensorSkipTime){
sensorState = 0;
sensorState = digitalRead(pirSensor2);
if(sensorState == HIGH){
digitalWrite(led2Pin, HIGH);
pir_time2 = millis();
pirSensorActiv = pirSensorActiv + 1;
}else{
digitalWrite(led2Pin, LOW);
pirSensorActiv = 0;
}
} else {
sensorState = digitalRead(pirSensor2);
if(sensorState == HIGH){
digitalWrite(led2Pin, HIGH);
pirSensorActiv = pirSensorActiv + 1;
}else{
digitalWrite(led2Pin, LOW);
pirSensorActiv = 0;
}
}
// Main LED
// Both sensors has worked
if(pirSensorActiv > 1){
if(pir_time1 > pir_time2){
// if PIR1 sensor was first
timeBetweenSensors = pir_time1 - pir_time2;
} else {
// if PIR2 sensor was first
timeBetweenSensors = pir_time2 - pir_time1;
}
if(timeBetweenSensors > settings_minimumDelay && timeBetweenSensors != 3000){
Serial.println(timeBetweenSensors);
digitalWrite(led3Pin, HIGH);
delay(settings_mainLedDelay);
}
} else {
digitalWrite(led3Pin, LOW);
}
}
I am getting some random outputs in serialprintin, but sometimes, they look like a real ones. Someone have ideas what I am doing wrong?
Your comment is wrong. Wrong comments are worse than no comments. Remove it or fix it.
You set the variable to zero and then a few nanoseconds later you set it to something else. You don't need to press 'clear' like on a calculator. The variable can only hold one value at a time. Just put the new value you want into that variable.
Using one variable to stand for two different things at different times during the program is going to cause trouble. You should really have two of these. You see the generic counter variable i re-used a lot but usually that is in a very local scope - just one for(int i=0; ...) so the value of i has no meaning outside of that scope and you can re-use that same name elsewhere for another for loop.
I think I see your problem...
} else {
digitalWrite(led2Pin, LOW);
pirSensorActiv = 0;
}
If sensor 1 has triggered, but sensor 2 has not yet triggered, you will set the pirSensorActiv back to zero. The next time around the loop, you see that the first sensor was triggered recently, so you ignore it. This variable can never get to 2 unless the sensors are triggered within a few hundred nanoseconds of each other.
In this case I don't think it needs an array. There's 2 sensors which are triggered in order. So why not call them 1 and 2? If there was a possibility to add more sensors in the future, then yes, now is the time to start with an array.