2

Im using a Arduino Mega 2560, and I tried to connect a us-100 ultrasonic sensor for distance measurement.

I connected it as shown in the image connection

The code I used was:

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

But It won't work, as in the only output i get is 0.00 cm as shown in the image sm

Can someone tell me if I'm doing something wrong(also, i tried putting two probes between the vcc and one of the gnd pins, and a resistance was shown.This should mean the sensor isn't broken.yes?)? this is my first time using arduino and I greatly appreciate help to fix this.

closed as unclear what you're asking by Nick Gammon Jan 30 '16 at 5:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • To get the result in cm, multiply distance only by 0.01657 ;) – user7097 Feb 2 '15 at 19:24
  • Try sending the value of pulse in via serial to see if that is returning the right value. – Anonymous Penguin Feb 3 '15 at 0:25
  • People can answer your questions much more quickly if you add a direct link to the datasheet in your question. – David Cary Feb 3 '15 at 14:47
  • This questions lacks the details needed to be answerable, and has been abandoned without any attempt to provide them for half a year, so should be closed as off topic. – Chris Stratton Jul 3 '15 at 20:59
  • I'm going to close this question as "unclear what you are asking". A link to the datasheet would help. I can't personally see from the photo what is connected to what. Feel free to edit the question with new information. Thanks for your understanding and cooperation! – Nick Gammon Jan 30 '16 at 5:20
0

Have you removed the "Serial Data Mode" shunt to put the device in "Pulse Width Mode"?

On the other hand, perhaps the "Serial Data Mode" would actually work better for your application?

Where I work, we have a coding style that strongly encourages all "magic numbers" to be documented with a link to the website it came from or a reference to the book and page it came from.

Would something like this work for you?:

void loop(){
    // Trigger US-100 to start measurement
    // at least 50 us high, then set it low,
    // as recommended by
    // https://www.bananarobotics.com/shop/US-100-Ultrasonic-Distance-Sensor-Module
    digitalWrite(trigger,HIGH);
    delayMicroseconds(50);
    digitalWrite(trigger,LOW);
    // Acquire
    unsigned long echo_microseconds = pulseIn(echo,HIGH);
    // debugging
    serial.println( echo_microseconds );
    Serial.println(F(" microseconds"));
    // conversion factor from
    // https://www.bananarobotics.com/shop/US-100-Ultrasonic-Distance-Sensor-Module
    unsigned long distance_mm = echo_microseconds * 34 / 100 / 2;
    Serial.print(distance_mm);
    Serial.println(F(" mm"));
    delay(50);    
}

It might be easier to use the Ping library as described at Arduino-Info: UltraSonic Distance Measurement.

You might find the Arduino Forum: Measuring Distances with Ultrasonic Sensors thread educational.

  • I tried all the above ways you said. None of the works. :/ I tried both serial data mode and PWM. – Vihanga Gamage Feb 5 '15 at 13:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.