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 void setup(); method.

Learn More: What’s a library?

A library is a saved set of functions()  and variables that provide users with improved functionality when coding.

Any code that starts with SmartInventor._____  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.

Next, inside   void setup(); , enter the following.

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   void loop(); , we will now create the number containers for the duration and distance that will be measured.

Under this type

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

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.

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.

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.

and you are done coding.

Final code

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.