Fighting with computers

Computers are not always friendly.

Thursday, December 17, 2009

Ultrasound ranging on Arduino


I've recently bought a sonar circuit for distance and temperature measurements. Actually you need to know the temperature if you want to get an accurate distance measurement as it affects sound's propagation speed.

Less than $20 shipped, the URM37 unit is compact and full of interesting features: It has a dual interface as it can use RS-232 levels or TTL levels. Be careful because it defaults to RS-232 and these voltage levels can damage many 5V devices, including Arduino inputs.

It has several operating modes but I've used the serial interface. URM37 library comes with a modified software serial port library that allows you not to use any two available digital pins to interface with the sensor. This way arduino hardware serial port is free to be used for code uploading.

Another cool feature of the sensor is that it can control a RC servo so you can mount the sensor in a mobile platform to get the distance measurement at different angles.

12 Comments:

  • At 11:06 pm, Anonymous vierito5 said…

    Seems pretty cool!

     
  • At 10:20 am, Blogger Charell said…

    I am trying to use this sensor too, could you please tell me how to communicate in serial mode?? I've tried every way I could think of to send and receive the data but it's not working =(

     
  • At 12:11 pm, Blogger Miguel Sánchez said…

    Hi Charell,

    You can get the datasheet for more info.

    What I used was the library from this page.

    Please note that the module can work with RS-232 levels too. Be careful because connecting an RS-232 output to an Arduino pin can destroy it. If a pin is dead it can no longer be used (till you replace the microcontroller). If you have doubts about a certain I/O pin, use a push button or led to make sure it is operational.

     
  • At 4:34 pm, Blogger Charell said…

    Oh, yea thanks. And I came across those, but I'm using mikroC to code... I tried to follow the logic, but I have been unsuccessful thus far. It seems like it should be pretty simple but I'm not sure what I'm doing wrong.


    Re: only two servo's?
    Reply #11 - Today at 11:20:17 | | Modify | Remove Here's what I've been trying:

    char commandString[4] = { 0x22, 0x00, 0x00, 0x22 };
    int command;
    int high;
    int low;
    int sum;

    // Initialize Software UART for the sensor @ 9600 baud
    error = Soft_UART_Init( &PORTB, 0, 2, 9200, 0 );

    if( error > 0 ){

    // Loop forever
    while(1);

    }

    // Wait for UART module to stabilize
    Delay_ms( 100 );


    // Enable 16 bit distance reading ( @ 135 degrees )
    // 0x22 + Degree + NC + SUM === 0x22 + 0x17 + 0x00 + 0x39
    for( i = 0; i < 4; i++ )
    Soft_UART_Write( commandString[i] );


    // Return reading
    // 0x22 + High(dist) + Low(dist) + SUM
    // Invalid read: 0x22 + 0xFF + 0xFF + SUM
    commandUSART = Soft_UART_Read( &error );
    highDataUSART = Soft_UART_Read( &error );
    lowDataUSART = Soft_UART_Read( &error );
    sumUSART = Soft_UART_Read( &error );



    _______________


    but I wasn't getting any data back and realized that I'm getting errors reading (after checking by putting the if(error>0) statement, that was used after UART initialization, after each read)



    I want the sensors to take a reading from straight in from of them...

    Do I need a delay or anything between sending the command and reading the distance it sends back?

     
  • At 6:03 pm, Blogger Miguel Sánchez said…

    I can see you use a soft UART. Please make sure your connections are correct (TX, RX). If I reckon correctly pin 7 has to be HIGH to enable the sensor.

    It seems response time is around 60 ms. Not sure what timeout (if any) have your soft_UART_read calls.

    Please note that the same sensor can be used in PWM mode, where you just get a pulse which width is proportional to the distance (no need for a UART!). You can see the sample code for the BASIC stamp in the forum.

     
  • At 9:13 pm, Blogger Charell said…

    Thank you for your responses. Well we hooked it up to the oscillascope to verify that we were sending the command to the sensor and also that the sensor was sending something back. So the problem must be in reading the distance data correctly. I've tried with the 60ms delay you suggested, and I've also tried with a 75ms delay --> because that's what this site said http://www.yerobot.com/forum/index.php?topic=6.0

    I'm currently send the command bytes ( 0x22 0x00 0x00 0x22 ) back to back with no delay. Then delaying before reading back the distance data. And finally reading the received data bytes back to back ( 0x22 highDist lowDist SUM ).

    ---No success =\

    Do I need a delay between sending/receiving each specific byte as well??

     
  • At 9:25 pm, Blogger Miguel Sánchez said…

    I'm sorry to hear that but, no, I do not think any delay is needed anywhere.

    I mentioned the delay because you asked for it. Most serial receive code will patiently wait for the next received character.

    If you've verified there is some data sent and received (data, not a single pulse) then I'd say you'd good.

    If you are using an Arduino I'd suggest you to do the connections as in the examples and use the library code first. Once you're sure everything is ok you can move back to your mikroC. I'm sorry but if just the baudrate is not right you're not going to make sense of it.

     
  • At 5:20 am, Blogger Charell said…

    Is it supposed to be making a ticking sounding noise? Because it wasn't doing that before...

    And you woulnd't happen to know how to use the other modes would you?

     
  • At 12:15 am, Blogger Miguel Sánchez said…

    Mine does not make any sound. That is why they call it ultrasound.

    I guess there is something wrong with your unit. A clicking sound might indicate that no oscillator output is sent to the ultrasound speaker, just DC (you can check this if you have an oscilloscope around).

    Anyway I've only used the unit with Arduino and the library I mentioned. For me it was a trouble-free ride.

    You may want to check with the manufacturer, maybe you've got a defective unit. I'm sorry I cannot be more helpful.

     
  • At 5:04 am, Anonymous distance measurement said…

    Having a ultrasonic range finder can be very useful in many different projects, as it can detect objects, I can use it to make alarm systems (cars usually use this kind of sensor), Obstacle-Avoiding Robot and many other things that you may have in mind.

     
  • At 3:48 am, Blogger Linux Circle said…

    How do I read the temperature exactly?

    Which pin should I read from is it number 7 the NC pin?

    I cant get TX and RX to do anything, but PWM works well for distance.

    Please teach me how to use this thing for reading temperature using serial?

     
  • At 7:30 am, Blogger Miguel Sánchez said…

    Have a look at this page: library and sample code is available, temperature measurement too.

    http://www.milesburton.com/Main_Page?title=URM37_Ultrasonic_Distance_Measurement_Library

     

Post a Comment

<< Home