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.
Comments
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.
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?
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.
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??
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.
And you woulnd't happen to know how to use the other modes would you?
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.
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?
http://www.milesburton.com/Main_Page?title=URM37_Ultrasonic_Distance_Measurement_Library