Ultrasonic sensor measuring...

Discussion in 'Programmer's Corner' started by russel096, Mar 11, 2013.

  1. russel096

    Thread Starter New Member

    Jan 2, 2012
    Hello everyone... Im hoping i could get some help here for my project. I am doing a motion sensor project with the z8f042a as my MCU and a US-100 ultrasonic sensor.. Ive read the datasheet of the sensor, and the thing is i need to configure that only object within 10cm will be detected...

    The codes goes like this:
    The Trigger port must be set to HIGH for 10us
    If echo is detected the duration of high time in the echo pin must be computed in seconds then converted to distance..
    Use an if statement to set r=1 if object is within 10cm range and r=0 if not.

    I am using ZSD II for coding and cant do it myself... All the other parts of the codes works... Just this part with the sensor... Please help!
  2. JohnInTX


    Jun 26, 2012
    Any sonar ranging basically does:
    Ping the sonar to emit a sound pulse.
    Time how long the return takes in some machine-convenient units (counts etc)
    Convert the machine units into something usable i.e inches, cm etc.

    This code is scabbed from the datasheet with my annotations. You will have to write your own version that does essentially the steps given.
    Code ( (Unknown Language)):
    1. // Demo sketch
    2. // is sketch will output distance info via the UART port
    4. // port assignment
    5. // change as may be necessary
    6. const
    7. int trigger=6;
    8. const
    9. int echo=7;
    10. oat
    11. distance;
    13. void setup(){
    14. Serial.begin(9600);
    15. pinMode(trigger,OUTPUT);
    16. pinMode(echo,INPUT);
    17. }
    19. void loop(){
    21. // Trigger US-100 to start measurement
    22. // Set up trigger
    23. digitalWrite(trigger,LOW);
    24. delayMicroseconds(5);
    26. // Start Measurement - GENERATE 10us PULSE ON TRIGGER LINE (pin 2). //This generates the sonar burst and the sound pulse begins its trip to the //target and back.
    27. digitalWrite(trigger,HIGH);
    28. delayMicroseconds(10);
    29. digitalWrite(trigger,LOW);
    31. //Acquire and convert to mtrs
    32. //Now.. time how long the round-trip is.  I don't know what the  pulseIn
    33. //function is specifically but I assume it starts a timer or counts up until
    34. //the echo is received.  The time it takes is directly proportional to the
    35. //distance. You are not using a GuzDuino (whatever that is) so you will
    36. //have to write your own counter/timer routine.
    38. distance=pulseIn(echo,HIGH);  // Time while the ECHO input (from the processor's
    39. // point of view), pin 3, is HIGH. Stop when its low and return the count/time.
    41. // Here, you convert the raw count to a usable 'distance'.  The actual
    42. // conversion depends on how many counts/inch your counter/timer will
    43. // return. As a rule of thumb, the range is approx. 147.5uSec/inch.
    44. // Your routine counts uS, calculates inches.
    46. // The factor given here is a distillation of the GuzDuino's counting speed.
    47. //Yours will likely be different.  The math itself may be more involved as //well.
    48. distance=distance*0.0001657;
    50. // send result to UART
    51. //..by converting 'distance' to some ASCII string, a display etc etc.
    52. Serial.println(distance);
    54. delay(50); //Waste some time and CPU resources here.. then do it again
    55. // Sometimes, this is necessary to let the sonar recover from the ping and to eliminate multiple echos that can occur when the sonar is close to the target or enclosed in a tube, etc.
    56. }
    Before its done, your code should take into account what happens if it does not get an echo. The timing should have a max value and return an error if it times out. Otherwise, a missed echo can lock up the code waiting on an input that will never occur.

    Using the data. A motion detector will look for changing distances as targets move towards/away from the sensor. Once you get the ranger working, you'll have to add the code to trigger on changing distances or objects within a specific range.

    Have fun.
    Last edited: Mar 18, 2013