SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By encrypted05
#177553
Hello! The US 100 Ultrasonic will be used to detect the presence of an object in just a range of 3 inches. It should be displaying a 1 if an object is detected and 0 if otherwise. I modified the codes from someone but unfortunately, it doesn't work the way it was supposed to be and I'm having a hard time trying to find out what went wrong. I hope someone can help me. Thank you. Below would be the codes and the printscreen of the output.
Code: Select all

/*-----( Import needed libraries )-----*/
#include <NewPing.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define  TRIGGER_PIN  11
#define  ECHO_PIN     10
#define MAX_DISTANCE  14 // Maximum distance we want to ping for (in centimeters).
                         //Maximum sensor distance is rated at 400-500cm.
/*-----( Declare objects )-----*/
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
/*-----( Declare Variables )-----*/
int DistanceIn;
int DistanceCm;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("UltraSonic Distance Measurement");
  Serial.println("YourDuino.com  terry@yourduino.com");
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceIn = sonar.ping_in();
  Serial.print("Ping: ");
  Serial.print(DistanceIn); // Convert ping time to distance and print result 
                            // (0 = outside set distance range, no ping echo)
  Serial.print(" in     ");
  
  delay(100);// Wait 100ms between pings (about 10 pings/sec). 29ms should be the shortest delay between pings.
  DistanceCm = sonar.ping_cm();
  Serial.print("Ping: ");
  Serial.print(DistanceCm); 
  Serial.println(" cm");  
  if ( (DistanceCm <= 7) && (DistanceCm != 0) )
  {
  Serial.println("1");
  }
  else if ( (DistanceCm > 7) && (DistanceCm !=0) )
  {
  Serial.println("0");
  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

// None
//*********( THE END )***********
Image
By waltr
#177557
My guess is that the distance to too small to get the tx/rx circuits turned around to detect the pulse echo.
Try a greater distance to start.
Also, try displaying (print) the raw sensor output to add in debugging.
By Mee_n_Mac
#177560
waltr wrote:My guess is that the distance to too small to get the tx/rx circuits turned around to detect the pulse echo.
Try a greater distance to start.
Also, try displaying (print) the raw sensor output to add in debugging.
The best info I could find on the device is this.
http://www.e-gizmo.com/KIT/images/ultra ... %201r0.pdf
Note the min range of 2 cm. So 3" (7.62 cm) should work but it's close to the min range.

@OP;
So try a longer distance but also open up the max distance allowed, from 14 to 250 cm to allow for some measurement. Change one of the calls to return a time measurement to get the RAW data.
Code: Select all
sonar.ping();
I'd have to look at the library and make sure it's code is compatible w/the waveforms in the above link. Is the trigger pulse correct ? Is the code measuring the PW of the echo, and not a delay (trigger to echo). Is the conversion factor correct ?

What power supply are you using for the device ?
By encrypted05
#177596
Mee_n_Mac wrote:
waltr wrote:My guess is that the distance to too small to get the tx/rx circuits turned around to detect the pulse echo.
Try a greater distance to start.
Also, try displaying (print) the raw sensor output to add in debugging.
The best info I could find on the device is this.
http://www.e-gizmo.com/KIT/images/ultra ... %201r0.pdf
Note the min range of 2 cm. So 3" (7.62 cm) should work but it's close to the min range.

@OP;
So try a longer distance but also open up the max distance allowed, from 14 to 250 cm to allow for some measurement. Change one of the calls to return a time measurement to get the RAW data.
Code: Select all
sonar.ping();
I'd have to look at the library and make sure it's code is compatible w/the waveforms in the above link. Is the trigger pulse correct ? Is the code measuring the PW of the echo, and not a delay (trigger to echo). Is the conversion factor correct ?

What power supply are you using for the device ?
I already changed the distance but it still doesn't work. I used a different code I found from the internet and it worked so the problem is probably in the codes. Unfortunately, the codes that worked, gives a different output to what I prefer and I'm having a hard time modifying it. Here's the codes:
Code: Select all
// 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;
 float 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
 digitalWrite(trigger,HIGH);
 delayMicroseconds(10);
 digitalWrite(trigger,LOW);
 // Acquire and convert to mtrs
 distance=pulseIn(echo,HIGH);
 distance=distance*0.0001657;
 // send result to UART
 Serial.println(distance);
 delay(50);
}
By waltr
#177607
Your next step to debug this is connect a two channel O'scope to the Trigger and output pin os the US device. Then check that the timeing is correct for input trigger and you do see an output pulse. Once you verify the hardware is working then you can start debugging the code.
By Mee_n_Mac
#177614
encrypted05 wrote:I used a different code I found from the internet and it worked so the problem is probably in the codes. Unfortunately, the codes that worked, gives a different output to what I prefer and I'm having a hard time modifying it.
By 'different output' you mean m vs inches and cm ? That's easy to fix. Change the multiplier in this line.
Code: Select all
distance=distance*0.0001657;
To output cm, it should be 100x larger. To output inches it should be 2.54x smaller than that (cm) number.

Also what Arduino are you using ?

The library you tried doesn't use the pulseIn() function. Instead it attempts to setup a timer to do the pulse measurement. The timer names change w/the varying MCUs used. The author appears to have tried to code the library for the 32U4 and 328.