element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum any one help me
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 10 replies
  • Subscribers 403 subscribers
  • Views 748 views
  • Users 0 members are here
Related

any one help me

Former Member
Former Member over 14 years ago

i bought UltraSonic Distance Sensor ( Serial Ascii O/p) from rhydolabz.

i connect this sensor to arduino uno board but this sensor didn't give the correct distance

i want arduino program for this sensor.

 

i use this program

 

/* Ping))) Sensor
 
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.
    
   The circuit:
    * +V connection of the PING))) attached to +5V
    * GND connection of the PING))) attached to ground
    * SIG connection of the PING))) attached to digital pin 7

 

   http://www.arduino.cc/en/Tutorial/Ping
  
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe

   This example code is in the public domain.

 

*/

 

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

 

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

 

void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

 

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

 

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

 

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
}

 

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

 

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

but this program for decimal o/p

but my sensor ASCII output

how can i get decimal value(correct distance)

  • Sign in to reply
  • Cancel
Parents
  • billabott
    0 billabott over 13 years ago

    The data sheet clearly states "The serial output data consist of six ASCII bytes ie xxx.x<CR>"  and "Range: 2 cm to 4 m".  What it should have said is "Range in cm:  '002.0'  to '400.0'".  The dist_to_Obj string may need to be parsed* if you want to convert it to an integer (16 bits) value.  I would recommend dist_in_mm to retain maximum precision and the value range would be 20mm to 4000mm (4 m). I would not use a single unsigned integer (8 bits) unless the value is retained at a reduced precision in cm with a value range of 2cm to 256cm.  This serial ascii data output format is great for easily displaying on the Serial Monitor screen when running a sketch but may be adding to the challenge of quickly knowing when a robot needs to stop its forward motion.

     

    *Before you ask about "parsing a string to a number"

    please read this arduino forum discussion.  Then,

    you will understand when I say "Aal izz well".

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    billabottb Can u give me the proper code for ittt?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 13 years ago in reply to Former Member

    Sorry, but I think not.  Sounds like you have a school assignment and you want someone to do the thinking for you.  I would not deny you the joy of learning.  Press on with trying to understand it yourself.  The code provided above in this discussion almost gets you there.  My points and any online dictionary will clear the fog for you.  [Unless you have a drug habit of some sort that interfers with your brain function.]   Run the code above and see what you get.  Then you can start working out how to convert it to an integer.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    OOkkk Welll lets seee what can happen with thiss Any ways thank uu :-)

    Aal izz well .....

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 13 years ago in reply to Former Member

    I will throw you a bonus point to consider.  The accuracy the of ultrasound measurement depends of  knowing the temperature and humidity in the 4 meter space in front of the sensor.  Does the rhydolabz device account for that or just make some general assumption? What is the error percentage?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    they say that The sensor outputs the error message only if the distance is out of range ie above 4.3 m and

    The percent error over the sensor’s operating range of 0 to 70 ° C is significant, in the
    magnitude of 11 to 12 percent

    The percent error over the sensor’s operating range of 0 to 70 ° C is significant, in the

    magnitude of 11 to 12 percent

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Former Member
    0 Former Member over 13 years ago in reply to billabott

    they say that The sensor outputs the error message only if the distance is out of range ie above 4.3 m and

    The percent error over the sensor’s operating range of 0 to 70 ° C is significant, in the
    magnitude of 11 to 12 percent

    The percent error over the sensor’s operating range of 0 to 70 ° C is significant, in the

    magnitude of 11 to 12 percent

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • billabott
    0 billabott over 13 years ago in reply to Former Member

    Q: How does it go?

    A: Aal izz well.

     

    In the code that C. Gustavvson provided above, did you modify it yet and get it working?

    I have added some commented code for you to consider and try out.  I hope yours is similar.

     

    void loop()

    {

      char buffer[20];

      int i=0;

      char c;

      int dist=0;           // let us say it is in mm

    // read and buffer sensor output

      while((c=sonic.read()) != 0xd){

        buffer[i++]=c;

    //  What is being done on the following 2 lines of code?  If the next character

    //  is number then multiply the partial distance result by 10 and add that digit.

    //  If the next character is period then skip over the scaling up of the distance value.

    //  if(c != '.'){

    //      dist = (dist*10) + (c-'0');

    // }

      }

      buffer[i]=0;       // this makes the buffer a 'null terminated string'   null = binary zero

    //  That is COOL!

    //   Because the sting variable buffer was uninitialized and filled with random junk.

    //   If you want to see for yourself;  just comment out the line above and include the two lines below

    // buffer[i]='z';

    // buffer[19]=0;

    //  An ERR<cr> code can also be return from the ascii ultrasound sensor

    //  what should be done about that?

      Serial.println(buffer);     // will print ERR just fine if needed

    // However, the dist will be some strange huge number  -  might even cause an overflow

    //  if(buffer[0]='E') {

    //     dist = 4000;

    //     Serial.print("More than ");

    //  }

    //  Serial.print(dist);
    //  Serial.println(" mm");

    // billabott tested the code and it seems to work  image

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube