Help in US 100 Ultrasonic Sensor codes

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.

/*-----( 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 )***********

What mode have you got selected on the sensor ? Serial or Pulse Width ?

UKHeliBob: What mode have you got selected on the sensor ? Serial or Pulse Width ?

Tbh, I don't really know how to determine that, but I do think it's Serial (based on the codes).

encrypted05: Tbh, I don't really know how to determine that, but I do think it's Serial (based on the codes).

There is a jumper on the back of the sensor board that sets the mode. See US-100-Ultrasonic-Distance-Sensor-Module

When the jumper is present, the sensor outputs the distance as binary serial data, otherwise the sensor outputs a single pulse that has a width which represents the distance measured.

If it is Serial mode then change to Pulse Width mode because you are using that mode in the program.

You are aware that 14cm is only 5.5inches? So for testing purposes I would try increasing MAX_DISTANCE to something quite large until you see something other than '0' for both In and cm outputs.

Regards,

Graham

UKHeliBob: There is a jumper on the back of the sensor board that sets the mode. See US-100-Ultrasonic-Distance-Sensor-Module

If it is Serial mode then change to Pulse Width mode because you are using that mode in the program.

I did have a jumper, I'll go get some more male ports tomorrow and see if it the code is finally going to work. Thanks!

ghlawrence2000: You are aware that 14cm is only 5.5inches? So for testing purposes I would try increasing MAX_DISTANCE to something quite large until you see something other than '0' for both In and cm outputs.

Regards,

Graham

Okay, thanks! :)

encrypted05: I did have a jumper, I'll go get some more male ports tomorrow and see if it the code is finally going to work. Thanks!

The way I read the page, if the jumper is missing then the sensor is in Pulse Width mode which is what you want.

Does the simple example sketch on the NewPing library page work ?

It is also worth checking your wiring. I know that you will say that it is correct and that you have checked it but do it again. Is the sensor connected directly to the Arduino or via a breadboard ? Take the 4 wires off the sensor and the Arduino and put them back again. Are the trigger and echo pins connected to the correct Arduino pins and not, possibly, reversed ? Measure the voltage at the sensor pins. Is it correct and the right polarity ?

UKHeliBob: The way I read the page, if the jumper is missing then the sensor is in Pulse Width mode which is what you want.

Does the simple example sketch on the NewPing library page work ?

It is also worth checking your wiring. I know that you will say that it is correct and that you have checked it but do it again. Is the sensor connected directly to the Arduino or via a breadboard ? Take the 4 wires off the sensor and the Arduino and put them back again. Are the trigger and echo pins connected to the correct Arduino pins and not, possibly, reversed ? Measure the voltage at the sensor pins. Is it correct and the right polarity ?

I removed the jumper already and it was still not working, then I added a male port in place for the other ground, and still, it wasn't working. But when I tried a different code (which is below), a code that measures the distance, it worked regardless of the jumper. I really want the other code to work for it was supposed to provide the desire output.

// 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);
}

The code that you say works uses different pins for trigger and echo than your original. Which pins have you got trigger and echo wired to ?

UKHeliBob: The code that you say works uses different pins for trigger and echo than your original. Which pins have you got trigger and echo wired to ?

Trigger is connected to pin 6, and the echo to pin 7.

encrypted05: Trigger is connected to pin 6, and the echo to pin 7.

Oh I get it! Thank you so much! It's working now :)

I'm glad it is working now that it is wired correctly.

From my reply #7

It is also worth checking your wiring. I know that you will say that it is correct and that you have checked it but do it again.

Are the trigger and echo pins connected to the correct Arduino pins

Just an FYI to others reading this. Apparently you have to unplug the US-100 from power to change the mode by pulling or replacing the jumper on the back.