Ultrasonic sensor measuring...

Thread Starter

russel096

Joined Jan 2, 2012
10
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!
 

JohnInTX

Joined Jun 26, 2012
4,787
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.
Rich (BB code):
// Demo sketch
// is sketch will output distance info via the UART port

// port assignment
// change as may be necessary
const
int trigger=6;
const
int echo=7;
oat
distance;

void setup(){
Serial.begin(9600);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
}

void loop(){

// Trigger US-100 to start measurement
// Set up trigger
digitalWrite(trigger,LOW);
delayMicroseconds(5);

// 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.
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);

//Acquire and convert to mtrs 
//Now.. time how long the round-trip is.  I don't know what the  pulseIn
//function is specifically but I assume it starts a timer or counts up until
//the echo is received.  The time it takes is directly proportional to the
//distance. You are not using a GuzDuino (whatever that is) so you will
//have to write your own counter/timer routine.
  
distance=pulseIn(echo,HIGH);  // Time while the ECHO input (from the processor's
// point of view), pin 3, is HIGH. Stop when its low and return the count/time.

// Here, you convert the raw count to a usable 'distance'.  The actual
// conversion depends on how many counts/inch your counter/timer will
// return. As a rule of thumb, the range is approx. 147.5uSec/inch.
// Your routine counts uS, calculates inches. 

// The factor given here is a distillation of the GuzDuino's counting speed.
//Yours will likely be different.  The math itself may be more involved as //well.
distance=distance*0.0001657;

// send result to UART
//..by converting 'distance' to some ASCII string, a display etc etc.
Serial.println(distance);

delay(50); //Waste some time and CPU resources here.. then do it again
// 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.
}
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:
Top