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-666c9cdb29e34149713786-i/] method.

Learn More: What’s a library?

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

Any code that starts with [crayon-666c9cdb29e41946367320-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.

Next, inside  [crayon-666c9cdb29e43262364013-i/] , 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, use pinMode() to assign the Trigger pin and Echo Pin

Next, inside  [crayon-666c9cdb29e48918314362-i/] , we will now create the number variables for the duration and distance that will be measured.
Now type the following code which will turn off the ultrasonic speaker.
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.
Now the way this program is able to measure distance is in this following line. The [crayon-666c9cdb29e4c988774700-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.

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.
Also type this command to allow for the distance to be displayed on the serial monitor on your computer.

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.