How to Program the Ultrasonic Range Sensor from RadioShack

This lesson will demonstrate how to use the Ultrasonic Range Sensor from RadioShack (Please note: this item is not include in the kit and is a tutorial on how to program it should you purchase one)

Wiring up

You will need three female to female jumper cables and of course the RadioShack Ultrasonic sensor.

Hookup the red wire from the “VCC” pin from the sensor to V on the Inventor board. The white wire in this case is connected to the SIG pin which is connected to the Smart Inventor board on pin 4 of PORTD.  The black wire is connected to the ground “GND” pin which can be any of the pins labeled G.

Programming the Ultrasonic Sensor

First, begin the code by including the smart inventor library.

Enter the following before the [crayon-6622528db41b4139426228-i/] method.
[crayon-6622528db41ba594030321/]

Learn More: What’s a library?

A library is a saved set of [crayon-6622528db41bc294004439-i/]  and variables that provide users with improved functionality when coding.

Any code that starts with [crayon-6622528db41bd467070859-i/]  is a function in the SmartInventor library.  Any variable that highlights ORANGE, is an initialized variable from an included library.

Learn More

Now we will assign the pin number for the signal pin. The nice thing about this sensor is that unlike the other ultrasonic range finders this sensor only uses one pin to signal the circuit to emit the sonar pulse or to listen for a sonar pulse. In this case we will use pin 28.
[crayon-6622528db41be228873204/]

Next, inside  [crayon-6622528db41c0713379954-i/] , enter the following.
[crayon-6622528db41c1642715230/]
This line of code will allow the SmartInventor Board to send data back to the computer at a rate of 9600 bits per second.

Serial.begin( )

Serial.Begin( BAUD rate);

This function will establish a high transfer BAUD connection between the remote and the board. The higher the BAUD number the faster the board will process information, but may yield a high chance for errors. The lower the BAUD number the slower the board will process information, but may yield a low chance for errors.

Acceptable BAUD rates range from 300 to 250000. Make sure that the BAUD rate is a number that is found within the lower right corner of the Serial Monitor.

Next, inside  [crayon-6622528db41c2812946856-i/] , we will now create the number containers for the duration and distance that will be measured.
[crayon-6622528db41c4804513800/]
Under this type
[crayon-6622528db41c5607041463/]
This sets the signal pin as an output. The digital write low makes sure the speaker is off.

now begin the emitting of the sonar pulse with this code
[crayon-6622528db41c6702126398/]
This cause the circuit to shoot the out the sonar pulse and fly through the air at the speed of sound.

Now you switch the signal pin from an output pin into an input so that the other ultrasonic speaker may listen and wait for the echo of the sonar pulse.
[crayon-6622528db41c7312206258/]

pulseIn( )

pulseIn(pin, value)

Initiates a timer when this method is used. Once the state changes value on the chosen pin between HIGH to LOW or LOW to HIGH, the timer ends. The output is the amount of time in milliseconds.

Now you can do the necessary math to calculate the distance. So type.
[crayon-6622528db41c8630306706/]
So you divide by two since the sonar pulse traveled twice the distance we are trying to measure.

Then you divide by 29 which converts the time traveled into centimeters.

Now to display the current distance through the serial monitor.
[crayon-6622528db41ca803663906/]
and you are done coding.

Final code
[crayon-6622528db41cb205133511/]

Viewing the Serial Monitor

Once you have uploaded your program, keep the upload cable connected and turn off all dip switches. Now click on the the serial monitor eyeglass icon on the top right.

Make sure you have the baud rate selected at 9600. and you should be able to see the distance output from the ultrasonic sensor.