How to Program the Ultrasonic Sensor

This lesson will demonstrate how to use the ultrasonic distance sensor (Please note: this item is not include in the kit and is a tutorial on how to program it should you purchase one)

Wiring setup 

You will need four female to female jumper cables and an Ultrasonic sensor.

Connect the VCC wire to the V pin of the inventor board. Now make sure the trig pin is connected to pin 27 and echo pin is connected to pin 28. Attach the ground (GND) wire from the Sensor to G on the board. If you get the trig and echo pins mixed up it will not output the correct distance.

 

You are now done wiring it up. Since the holes on the sensor are not big enough to fit the bolts provided in your kit, it is recommended to use some mounting foam. The double sided sticky foam both insulates the exposed metal contacts on the sensor and can be stuck on just about anywhere.

Programming The Ultrasonic Sensor

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

Enter the following before the [crayon-6606b92883ade680303786-i/] method.
[crayon-6606b92883ae7171348573/]

Learn More: What’s a library?

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

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

Learn More

Second we must name which pins we will be using for the ultrasonic sensor. We will make them Global Variables and give them descriptive names.
[crayon-6606b92883aeb850415489/]

Next, inside  [crayon-6606b92883aec517687583-i/] , enter the following.
[crayon-6606b92883aed452242329/]
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, use pinMode() to assign the Trigger pin and Echo Pin
[crayon-6606b92883aef101738118/]

Next, inside  [crayon-6606b92883af2564278313-i/] , we will now create the number variables for the duration and distance that will be measured.
[crayon-6606b92883af3021538220/]
Now type the following code which will turn off the ultrasonic speaker.
[crayon-6606b92883af4945522404/]
Next type the following command which will turn on the trigger speaker and emit a burst of high frequency sound for a very small amount of time 10 microseconds and then turn off the trigger speaker.
[crayon-6606b92883af5894527878/]
Now the way this program is able to measure distance is in this following line. The [crayon-6606b92883af6633366909-i/]  function saves the amount of time it takes for the sound burst to travel back and forth from an object to reach the echo speaker.
[crayon-6606b92883af7463825926/]

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.

Next, code the math that converts the time traveled by the ultrasonic sound into centimeters.
[crayon-6606b92883af8052174734/]
Also type this command to allow for the distance to be displayed on the serial monitor on your computer.
[crayon-6606b92883afa309148600/]
 

Final Code
[crayon-6606b92883afb463567660/]

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.