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-673683f070e7e658521708-i/] method.
[crayon-673683f070e84796696329/]

Learn More: What’s a library?

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

Any code that starts with [crayon-673683f070e87549649404-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-673683f070e88138575360/]

Next, inside  [crayon-673683f070e8a303312115-i/] , enter the following.
[crayon-673683f070e8b912278847/]
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-673683f070e8c925170124-i/] , we will now create the number containers for the duration and distance that will be measured.
[crayon-673683f070e8d313188544/]
Under this type
[crayon-673683f070e8e699860599/]
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-673683f070e8f346610564/]
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-673683f070e90868742259/]

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-673683f070e92074663259/]
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-673683f070e93497171134/]
and you are done coding.

Final code
[crayon-673683f070e94957858567/]

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.