Connect with us

Arduino ultrasonic range detector

Discussion in 'Microcontrollers, Programming and IoT' started by mollekake, Jan 9, 2015.

Scroll to continue with content
  1. mollekake

    mollekake

    37
    0
    Jun 30, 2014
    Hello!

    So for some time i've been on and off with this small project i have. I started out with programming it with assembly and PIC, got done and put it aside.
    Now i got myself a couple ardunio's uno and nano.

    I can get the range detector to detect distances and turn on different leds for each distance and so on.

    what i want to do now is to make it blink faster the closer an object is, and slower the further away an object is.
    I can't seem to make that work.

    Got any tips?
     
  2. hevans1944

    hevans1944 Hop - AC8NS

    4,468
    2,083
    Jun 21, 2012
    Sounds like a fun project! Are you pulsing an ultrasonic transmitter transducer and measuring the arrival time interval of the echo? Do you use a narrow range gate that sweeps from minimum range to maximum range to allow you to acquire the echo? How does the range detector work? How do you discriminate multiple simultaneous reflections from targets at different ranges? Please upload schematics and pictures.
     
  3. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    Well. Sounds like you can currently detect distance.
    You could try storing the value of distance into a variable, and then using that variable for the duration of a sleep command. The greater distance will cause a longer delay which will slow the rate at which you turn the LED on and off.

    There are other methods, but this sounds the easiest right off the bat. Of course, sharing code will help ;)
     
  4. mollekake

    mollekake

    37
    0
    Jun 30, 2014
    I'm using an HC-SR04 ultrasonic transmitter. It sends a signal on one pin, receives on another. I'm using the built in pulseIn function to determine the time and distance, which times out when the ping takes too long to come back.
    here is my current blinking version, but this makes it blink more/less only on given distances, i want it to be smooth, if you understand.

    Code:
    void setup(){
    Serial.begin(9600);
      pinMode(A0, OUTPUT);
      pinMode(A5, INPUT);
      pinMode(13, OUTPUT);
    }
    
    long avg = 0;
    int i = 0;
    int j = 0;
    int rate = 0;
    
    void loop(){
      long duration = 0;
      long distance = 0;
     
      //Pulse
      digitalWrite(A0, HIGH);
      delayMicroseconds(10); //minimum delay
      digitalWrite(A0, LOW);
      duration = pulseIn(A5, HIGH, 15000);//starts timer, will time out
      distance = (duration/2) / 29.1; //getting distance in cm
     
      //Getting average
      i++;
      avg += distance;
      //delay(2);
      if(i == 5){
       j = avg / 5;
       i = 0;
       avg = 0;
       Serial.print(j);//printing average distance to serial
       Serial.println();
      }
     
     
    
      //Setting blink "rate"
      if(j < 10){
       rate = 25;
      }else if(j > 10 && j < 30){
       rate = 50;
      }else if(j > 30 && j < 50){
       rate = 75;
      }else if(j > 50 && j < 100){
       rate = 100;
      }else if(j > 100){
       rate = 0;
      }
     
      //Blink
      if(rate > 0){
        digitalWrite(13, HIGH);
        delay(15);
        digitalWrite(13,LOW);
        delay(rate);
      }
         
      delayMicroseconds(50);//delay to prevent false detect from ping too far away
    }
     
  5. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    You could try simply changing delay(rate) to delay(j)
    Or you can do something like rate = j / 2
     
  6. mollekake

    mollekake

    37
    0
    Jun 30, 2014
    Thanks for the input! Seems like i just needed to get someone to kickstart me :p it works pretty well now, reduced the average calculating to only 2 cycles to prevent too much delay, and changed to: rate = j*2
    Now it works pretty well! The downside to ultrasound is the sound, on clothes and things that have an angle, it gives of a false reading, because the sound doesn't return.
    Any ideas on how to get around that? Could always look at the possiblity of 2 sensors
     
  7. Gryd3

    Gryd3

    4,098
    875
    Jun 25, 2014
    I'm not aware of any way to possibly detect soft sound absorbing materials with Ultrasound.
    I would first look at any sensitivity adjustments that can be made, as cloth should return 'some' sound. If it was imperative to detect the distance from a soft material like that, you may need to look into an optical solution. Unfortunately though, I can only hypothesise as I have no experience with range detection other than a simple IR LED / Detector threshold detector.
     
  8. mollekake

    mollekake

    37
    0
    Jun 30, 2014
    Yeah i was thinking about IR, how does it do on distances? I assume it doesn't work very well above 1m
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-