Distance measurement with Ultrasonic PING Sensor

Hi all!

I would like to measure two distances with the Ultrasonic PING Sensor (Parallax 28015) on the same Arduinoboard.
My goal: Detecting a person at the distances between A) 50cm - 80cm and also at B) 100cm - 120cm. I like to have two seperate digital outputs (high, 5V) for the cases A and B.

The examplecode on http://www.arduino.cc/en/Tutorial/UltrasoundSensor works fine! But, what shal i do with these (time?) values to reach my goal?

Thanks for every hint!
FTS

[Arduino 07, Miniboard, MacBook Intel]

hi

there are lots of people in the forum who are better at code than I am, but here's my two cents!
I think you would want something like this, in very rough pseudo code (i.e. fill in the blanks). Check the reference pages to see how to write the boolean conditions in the IF loops. You'll also have to record the sensor values for the IF loops: test to see what value the sensor returns for each of your distance limits.

void loop(){

take sensor reading;  // read the sensor

// test to see if the sensor value is in position A range
if sensorValue (between position A minimum and maximum);{    
   digitalWrite(positionApin, HIGH);                    // set A pin high if sensor in range
}
else {                                         // set A pin low if sensor not in range
digitalWrite(positionApin, LOW);}





// test to see if the sensor value is in position B range
if sensorValue (between position B minimum and maximum){    
   digitalWrite(positionBpin, HIGH);        // set B pin high if sensor in range
}
else {                           // set B pin low if sensor not in range
digitalWrite(positionBpin, LOW);}

}

hi

glad to see you got it working...

you might want to try just using one conditional stattement to test if someone is in the desired range of the sensor, something like:

if (timecount <= 510 && timecount >= 440) // test if timecount value is between 440 and 510

That will make your code easier to read and understand, for later on.

PS: happy new year to you too!

Hi!
I have modified the code with if/else... and some () and it works!
Thanks a lot

/* Ultrasound Sensor 
 *------------------
 *
 * Reads values (00014-01199) from an ultrasound sensor (3m sensor)
 * and writes the values to the serialport.
 *
 * http://www.xlab.se | http://www.0j0.org
 * copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
 *
 * extended 02.01.2007 by aktor
 * used for PING Ultrasonic Range Finder (#28015 from Parallax)
 * detection of two distance ranges for two digital outputs
 */

int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin1 = 13; // LED at digital pin 13
int ledPin2 = 10; // LED at digital pin 10

void setup() {
  beginSerial(9600);                  // Sets the baud rate to 9600
  pinMode(ledPin1, OUTPUT);            // Sets the digital pin 13 as output
  pinMode(ledPin2, OUTPUT);            // Sets the digital pin 10 as output
}

void loop() {
  timecount = 0;
  val = 0;
  pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

  /* Send low-high-low pulse to activate the trigger pulse of the sensor
   * -------------------------------------------------------------------
   */

  digitalWrite(ultraSoundSignal, LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignal, LOW); // Holdoff

  /* Listening for echo pulse
   * -------------------------------------------------------------------
   */

  pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
  val = digitalRead(ultraSoundSignal); // Append signal value to val
  while(val == LOW) { // Loop until pin reads a high value
    val = digitalRead(ultraSoundSignal);
  }

  while(val == HIGH) { // Loop until pin reads a high value
    val = digitalRead(ultraSoundSignal);
    timecount = timecount +1;            // Count echo pulse time
  }

  /* Writing out values to the serial port
   * -------------------------------------------------------------------
   */

  ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

  serialWrite('A'); // Example identifier for the sensor
  printInteger(ultrasoundValue);
  serialWrite(10);
  serialWrite(13);

  /* Lite up LED if the timevalues passed by the echo pulse (distances mesured an compared as dispayed on the serial monitor) 
   * -------------------------------------------------------------------
   */


  //LED1 ON, Pin13, distances 70cm - 85cm   
  if((timecount < 510) && (timecount > 440)){ 
    digitalWrite(ledPin1, HIGH);
  }
  else
  {  
    digitalWrite(ledPin1, LOW);
  }

  //LED2 ON, Pin10, distance 15cm - 30cm
  if((timecount < 190) && (timecount > 100)){ 
    digitalWrite(ledPin2, HIGH);
  }
  else
  {
    digitalWrite(ledPin2, LOW);
  }

  /* Delay of program
   * -------------------------------------------------------------------
   */

  delay(100);
}

finaly, here the tested code with som additions:

/* Ultrasound Sensor 
 *------------------
 *
 * Reads values (00014-01199) from an ultrasound sensor (3m sensor)
 * and writes the values to the serialport.
 *
 * http://www.xlab.se | http://www.0j0.org
 * copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
 *
 * extended 02.01.2007 by aktor
 * used for PING Ultrasonic Range Finder (#28015 from Parallax)
 * detection of two distance ranges for two digital outputs
 */

int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin1 = 13; // LED at digital pin 13
int ledPin2 = 10; // LED at digital pin 10

void setup() {
  beginSerial(9600);                  // Sets the baud rate to 9600
  pinMode(ledPin1, OUTPUT);            // Sets the digital pin 13 as output
  pinMode(ledPin2, OUTPUT);            // Sets the digital pin 10 as output
}

void loop() {
  timecount = 0;
  val = 0;
  pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

  /* Send low-high-low pulse to activate the trigger pulse of the sensor
   * -------------------------------------------------------------------
   */

  digitalWrite(ultraSoundSignal, LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignal, LOW); // Holdoff

  /* Listening for echo pulse
   * -------------------------------------------------------------------
   */

  pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
  val = digitalRead(ultraSoundSignal); // Append signal value to val
  while(val == LOW) { // Loop until pin reads a high value
    val = digitalRead(ultraSoundSignal);
  }

  while(val == HIGH) { // Loop until pin reads a high value
    val = digitalRead(ultraSoundSignal);
    timecount = timecount +1;            // Count echo pulse time
  }

  /* Writing out values to the serial port
   * -------------------------------------------------------------------
   */

  ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

  serialWrite('A'); // Example identifier for the sensor
  printInteger(ultrasoundValue);
  serialWrite(10);
  serialWrite(13);

  /* Lite up LED if the timevalues passed by the echo pulse (distances mesured an compared as dispayed on the serial monitor) 
   * -------------------------------------------------------------------
   */


  //LED1 (GREEN) ON, Pin13, distances high   
  if((timecount <= 1300) && (timecount >= 1100)){ 
    digitalWrite(ledPin1, HIGH);
    delay(1200);                  // waits for a second
  }
  else
  {
    digitalWrite(ledPin1, LOW);
  }

  //LED2 (RED) ON, Pin10, distance low
  if((timecount <= 900) && (timecount >= 700)){ 
    digitalWrite(ledPin2, HIGH);
    delay(1300);                  // waits for a second
  }
  else
  {
    digitalWrite(ledPin2, LOW);
  }

  /* Delay of program
   * -------------------------------------------------------------------
   */

  delay(400);   //orig 100
}

I recently made some code for the srf04 and the PING))). I think if you use the pulseIn function, you can get better resolution on the sensor. Here is an example.

#include <WProgram.h>
#include "srf04.h"
#define echoPin         4
#define initPin           3
#define PINGPIN        2
#define SRF04           0
#define PING             1
void setup()
{
    pinMode(initPin, OUTPUT);
    pinMode(echoPin, INPUT);
    Serial.begin(9600);
}

void loop()
{
    Serial.print("SRF04: ");
    Serial.print(getdist(SRF04), DEC);
    Serial.print("  PING:  ");
    Serial.println(getdist(PING), DEC);
}

unsigned long getdist(int sensor)
{
    unsigned long time = 0;
    switch (sensor)
    {
        case SRF04:digitalWrite(initPin, HIGH);
                   delayMicroseconds(10);
                   digitalWrite(initPin, LOW);
                   time = pulseIn(echoPin, HIGH);
                   break;
        case PING:pinMode(PINGPIN, OUTPUT);
                  digitalWrite(PINGPIN, HIGH);
                  delayMicroseconds(5);
                  digitalWrite(PINGPIN, LOW);
                  pinMode(PINGPIN, INPUT);
                  time = pulseIn(PINGPIN, HIGH);
                  break;
    }
    return time;
}

Hi Guys,

What would be the correct coversion factor to convert the pulses received into the cm?

Please help.

Thanx

the constant you are looking for is 0.03434, for instructions on how to use it :smiley: I will direct you to the page that I reference (it's a pdf, so you can download and save it)

http://www.parallax.com/Portals/0/Downloads/docs/prod/audiovis/Distance28015.pdf

I use pings and devantechs; this pdf is my guide.

use my code from the Playground...
add some IF's or CASE's

http://www.arduino.cc/playground/Main/UltrasonicSensor

Question, that pdf that eustace referenced seems to indicate that a pulse width is 29.033 uS per centimeter and 73.746 uS per inch(comments on page 10 for the Javelin). So wouldn't ((pulselength / 73.746)/2) work? It seems to be pretty close when comparing a wall @ 80" with the serial spew and a tape measure.

I missing anything? I have a feeling I am.