Pages: [1]   Go Down
 Author Topic: Using Ultrasonic Range Finder SRF02 as Motion Detector  (Read 1229 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 40
 « on: May 14, 2012, 04:47:26 pm » Bigger Smaller Reset

Hey guys!!!!

I am doing a project concerning motion detection...
For that I am using 2 Pir sensors and one US sensor. Thus with the PIR sensor, after having some problems, now it's ok.

Although to reach the goal with US, i need to get a code that allow me to see when the difference of distances, mean that somebody crossed the sensor.

I was thinking that the right way should be create an oldvalue and a value, and if( oldvalue-value>20) reports that movement was detected.

I am receiving on Processing the data on the following format [PIR1,PIR2,US]

Probably i need to use something as an array, but I am not good at this point yet, so if somebody could help me I would appreciate

Cheers Arduiner's
I don't know
 « Last Edit: May 14, 2012, 04:57:44 pm by xaral » Logged

Ontario, Ohio
Offline
Full Member
Karma: 1
Posts: 195
 « Reply #1 on: May 15, 2012, 03:51:52 pm » Bigger Smaller Reset

You didn't say exactly why or how you are using two PIR sensors. That makes a lot of difference how you use your Boolean operators (among other things). Are they oriented the same direction?
The two if statements are nested in this case so that both must be true in order to execute your commands, but I'm only guessing at your set up.
If you're more specific with your questions, you'll get more specific and relevant help.

Code:
void loop(){
distCalc();  //  call your distance calculation function for your distance sensor here
if ((digitalRead(PIR1) == HIGH) || (digitalRead(PIR2) == HIGH)) {   // if one PIR sensor OR the other detects movement
if (inches <=36 && inches>=12) {                                              // AND if your distance is between two given values
//do something here;
}
}
}

void calc();{

}

 « Last Edit: May 17, 2012, 10:00:43 am by TeslaIaint » Logged

Offline
Newbie
Karma: 0
Posts: 40
 « Reply #2 on: May 15, 2012, 04:30:56 pm » Bigger Smaller Reset

Hi Tesla!! Thanks again for your answer. You are right, but sometimes is difficult for me to explain everything... I'm using all this sensors to get a better result of motion inside a room... So each sensor will be oriented for a different direction.
If one of this sensors detects motion (PIR) or distance variation (US), it should report me 0. When it detects it should report the value 0 for some seconds.
In other hand when it not detect any movement it returns 1.

The better way is present my entire code (still have a lot of mistakes   )

Code:
#include <Wire.h>

const int PIR1 = 2;
const int PIR2 = 3;
const int LED1 = 4;
const int LED2 = 5;

//Definition of variables to use
int val1 = 0;
int val2 = 0;
int count1 = 0;
int count2 = 0;

int calibrationTime = 20;  //calibration time of PIR sensors

void setup()

{
Wire.begin();          // start the I2C bus

Serial.begin(9600);    // open the serial port:

//Definition of input/output values
pinMode(PIR1,INPUT);
pinMode(PIR2,INPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);

//give the sensor some time to calibrate
Serial.println("Sensor Calibration in Progress");
Serial.println("------------------------------");

for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
digitalWrite(LED1, HIGH);
delay(250);
digitalWrite(LED2, HIGH);
delay(250);
digitalWrite(LED1, LOW);
delay(250);
digitalWrite(LED2, LOW);
delay(250);
}

Serial.println("");
Serial.println("Sensor Calibration Completed");
delay(100);
}

void loop()

{
passiveIR();
ultrasonic();
presence();

}

void passiveIR()
{

}

void ultrasonic()

{

// step 1: instruct sensor to read centimeters
Wire.beginTransmission(112); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)
Wire.write(byte(0x51));      // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission();      // stop transmitting

// step 2: wait for readings to happen
delay(70);                   // datasheet suggests at least 65 milliseconds

// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(112); // transmit to device #112
Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
Wire.endTransmission();      // stop transmitting

// step 4: request reading from sensor
Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

if(2 <= Wire.available())    // if two bytes were received
{
// Serial.print(',');
reading = reading << 8;   // shift high byte to be high 8 bits
// Serial.print(',');

}

delay(250);                 // wait before next reading:

}

void presence()
{

if(val1 == HIGH || val2 == HIGH || f >15){
Serial.print('0');
Serial.print(',');
}

if(val1 == LOW || val2 == LOW || f < 15){
Serial.print('1');
Serial.print(',');

for(int i=0; i<50; i++){
}
}
}

I hope you can reach my goal, if can try  to explain better.

 « Last Edit: May 15, 2012, 04:38:12 pm by xaral » Logged

Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19015
I don't think you connected the grounds, Dave.
 « Reply #3 on: May 15, 2012, 04:35:03 pm » Bigger Smaller Reset

The first line of that sketch starts a comment, but where does it end?
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Offline
Newbie
Karma: 0
Posts: 40
 « Reply #4 on: May 15, 2012, 04:39:28 pm » Bigger Smaller Reset

stupid mistake from myself...
hope now it's fine
 Logged

Central MN, USA
Offline
Karma: 35
Posts: 5918
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
 « Reply #5 on: May 15, 2012, 05:01:11 pm » Bigger Smaller Reset

Code:
delay(250);                 // wait before next reading:

This line in your sonic ranger code could make fast object slip by without detection though. If your sonic ranger beam covers say 30 cm width at the point where objects pass, you just have to move at 30cm/0.25s or 1.2m/s at the right timing (Murphy said it will happen) to slip pass the sensor without detection. I can walk or skip faster than that. Is that very necessary or can you reduce it to 25ms?
 Logged

Offline
Newbie
Karma: 0
Posts: 40
 « Reply #6 on: May 15, 2012, 05:11:10 pm » Bigger Smaller Reset

I think you are right. I wrote 250 cause was the delay suggest in Arduino playgroung for this sensor. I will change it and see what happen...

I already updated the code but I'm still having problems

Code:
#include <Wire.h>

//definition of pin on Arduino board
const int PIR1 = 2;
const int PIR2 = 3;
const int LED1 = 4;
const int LED2 = 5;

//Definition of variables to use
int val1 = 0;
int val2 = 0;
int count1 = 0;
int count2 = 0;

int calibrationTime = 20;  //calibration time of PIR sensors

void setup()

{
Wire.begin();          // start the I2C bus

Serial.begin(9600);    // open the serial port:

//Definition of input/output values
pinMode(PIR1,INPUT);
pinMode(PIR2,INPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);

//give the sensor some time to calibrate
Serial.println("Sensor Calibration in Progress");
Serial.println("------------------------------");

for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
digitalWrite(LED1, HIGH);
delay(250);
digitalWrite(LED2, HIGH);
delay(250);
digitalWrite(LED1, LOW);
delay(250);
digitalWrite(LED2, LOW);
delay(250);
}

Serial.println("");
Serial.println("Sensor Calibration Completed");
delay(100);
}

void loop()

{
passiveIR();
ultrasonic();
presence();

}

void passiveIR()
{
Serial.print(val1);
Serial.print(',');
Serial.print(val2);
Serial.print(',');
}

void ultrasonic()

{

// step 1: instruct sensor to read centimeters
Wire.beginTransmission(112); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)
Wire.write(byte(0x51));      // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission();      // stop transmitting

// step 2: wait for readings to happen
delay(70);                   // datasheet suggests at least 65 milliseconds

// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(112); // transmit to device #112
Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
Wire.endTransmission();      // stop transmitting

// step 4: request reading from sensor
Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

if(2 <= Wire.available())    // if two bytes were received
{
// Serial.print(',');
reading = reading << 8;   // shift high byte to be high 8 bits

}

delay(25);                 // wait before next reading:

}

void presence()
{
Serial.print(',');
Serial.print(',');
Serial.print(',');

if(val1 == HIGH && val2 == HIGH && f < 15){
Serial.println('1');
//Serial.print(',');
}
if(val1 == LOW || val2 == LOW || f >15){
Serial.println('0');
//Serial.print(',');
}
for(int i=0; i<50; i++){
}
}

I forgot to tell that initially I was thinking to do the treatment of each sensor on processing sketch, but concerning that I will use the final value in a graph and in an interface, I thought that it is better if the value is simplified...
 Logged

Central MN, USA
Offline
Karma: 35
Posts: 5918
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
 « Reply #7 on: May 15, 2012, 05:19:57 pm » Bigger Smaller Reset

And the problem your are having is ...
 Logged

Offline
Newbie
Karma: 0
Posts: 40
 « Reply #8 on: May 15, 2012, 05:29:08 pm » Bigger Smaller Reset

Sorry. Seems that I'm almost sleeping
Although I think the next code is right

Code:
if(val1 == HIGH && val2 == HIGH && f < 15){
Serial.println('1');
//Serial.print(',');
}
if(val1 == LOW || val2 == LOW || f >15){
Serial.println('0');
//Serial.print(',');

the first condition if(val1 == HIGH && val2 == HIGH && f < 15) isn't being detected. Arduino is just printing 0s

I will get crazy at the end of this... seems simple but there's always a little problem
 « Last Edit: May 15, 2012, 05:30:59 pm by xaral » Logged

Central MN, USA
Offline
Karma: 35
Posts: 5918
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
 « Reply #9 on: May 15, 2012, 06:28:39 pm » Bigger Smaller Reset

What do you get from these when you have no obstacles and when you do?

Code:
Serial.print(',');
Serial.print(',');
Serial.print(',');

I assume if you don't have obstacles then you get zero? Or do you have a back drop that reflects the sound back so there is a fixed distance without obstacles?
 Logged

Offline
Newbie
Karma: 0
Posts: 40
 « Reply #10 on: May 15, 2012, 07:05:45 pm » Bigger Smaller Reset

No comments for all my mistakes on code...
The problem was just linked with a wrong position of variable declaration...
Now I hope I can use this on Processing

Code:
#include <Wire.h>

//definition of pin on Arduino board
const int PIR1 = 2;
const int PIR2 = 3;
const int LED1 = 4;
const int LED2 = 5;
const int pinPot=A0;

//Definition of variables to use
int val1 = 0;             //PIR1 value
int val2 = 0;             //PIR2 value
int reading = 0;          //US value
int oldreading = 0;       //oldUS value
int count = 0;            //counter to keep LED turned on
int calibrationTime = 5;  //calibration time of PIR sensors

void setup()
{
Wire.begin();          // start the I2C bus

Serial.begin(9600);    // open the serial port:

//Definition of input/output values
pinMode(PIR1,INPUT);
pinMode(PIR2,INPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);

//give the sensor some time to calibrate
Serial.println("Sensor Calibration in Progress");
Serial.println("------------------------------");

for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
digitalWrite(LED1, HIGH);
delay(250);
digitalWrite(LED2, HIGH);
delay(250);
digitalWrite(LED1, LOW);
delay(250);
digitalWrite(LED2, LOW);
delay(250);
}

Serial.println("");
Serial.println("Sensor Calibration Completed");
delay(100);
}

void loop()
{
passiveIR();
ultrasonic();
presence();
}

void passiveIR()
{
}

void ultrasonic()
{
// step 1: instruct sensor to read centimeters
Wire.beginTransmission(112); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)
Wire.write(byte(0x51));      // command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission();      // stop transmitting

// step 2: wait for readings to happen
delay(70);                   // datasheet suggests at least 65 milliseconds

// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(112); // transmit to device #112
Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
Wire.endTransmission();      // stop transmitting

// step 4: request reading from sensor
Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112

if(2 <= Wire.available())    // if two bytes were received
{
reading = reading << 8;   // shift high byte to be high 8 bits
}

delay(25);                 // wait before next reading:
}

void presence()
{

if((val1 == HIGH) && (val2 == HIGH) && (f < 15)){
Serial.print('1');
Serial.print(';');

}
if((val1 == LOW) || (val2 == LOW) || f >15){
Serial.print('0');
Serial.print(';');
count = 1;

if(count > 0){
count=count+1;
digitalWrite(LED1, HIGH);
}

if(count == 50){
digitalWrite(LED1, LOW);
count = 0;
}
}
}
 Logged

Offline
Newbie
Karma: 0
Posts: 1
 « Reply #11 on: October 11, 2012, 02:26:41 am » Bigger Smaller Reset

Hello! I want to ask about coding on how to combine piezo code with sensor SRF02 code to arduino UNO? I hope you will help me to solve my problems  thank you for any responds. I appreciate it so much :')
 Logged

Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19015
I don't think you connected the grounds, Dave.
 « Reply #12 on: October 11, 2012, 03:04:54 am » Bigger Smaller Reset

Quote
I hope you will help me to solve my problems
Well, maybe, when you tell us what your problems are.
 Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Offline
Newbie
Karma: 0
Posts: 20
 « Reply #13 on: October 12, 2012, 06:37:10 am » Bigger Smaller Reset

@xaral : I'm working on a rather similar solution (motion detection). I've tried both sensors: PIR and US. I've made different tests and finally dropped the US for this use case. It is not the right sensor for your job and let me explain at least one of the reason, which in my case was the main.

The US sensor and the PIR sensor have really distinct detection area. You will end up with having one sensor detecting a motion and the other not, and vice versa. In my particular case, I had to be in a really narrow area to be able to detect a motion with the US sensor whereas the PIR sensor was detecting the motion...

If you want an advice, forget your US sensor for any motion detection solution and focus only on PIR sensor(s). To avoid false positive, keep your two sensors, choose a position where their detection areas cross each other and only trigger an action if BOTH detects a motion.

Good luck.
 Logged

 Pages: [1]   Go Up