Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

Virtuabotix Ultrasonic library and coding guide

The Virtuabotix Ultrasonic library and coding guide is mean’t to help you understand and use your Virtuabotix Ultrasonic sensor, and the technology that is used to control and read measurements from it. The sensor uses similar technology and protocol to other ultrasonic solutions, so this information may help you to understand some of the more general principles at play with Ultrasonic sensors as well.

Versalino™ Libraries & Projects


Library icon.pngUltrasonic Sensor LibraryCurrent version 2S0A Released 6 May 2012.

Virtuabotix Ultrasonic Timing Diagrams

The Virtuabotix Ultrasonic is a specially screened version of the HC-SR04 that is tested to guarantee operation in single line mode at 5V. In general performance characteristics are the same as well. Below is the standard usage of an HC-SR04 and many other split Trigger/Echo devices. In general Ping, and Ultrasonic sensors that do not have digitally modulated output can be triggered by pulsing the input high for at least 10 uS (microseconds) and taking a Pulsin of the output signal.

The outputted high level pulse when timed should provide the time it took at the speed of sound for your Virtuabotix Ultrasonics signal to travel to and from an obstacle and back to the sensor itself. Because of the simplicity of the output one simply needs to divide by 2 and then the speed of sound to determine the distance of an obstacle.


What is really great about the Virtuabotix Ultrasonic however is that you can simply tie the Trigger and Echo pins together and use it more like you would use a one wire ping sensor. This requires very quickly changing from an input to an output, but can very much be achieved on just about any 8 Mhz or better micro-controller like the Arduino.


Arduino™ Libraries & Projects

Simplest Possible Code

If you are not a fan of Arduino Libraries, and you don’t need all the features you can just use this simple function to read out distances from you Ultrasonic Sensor.





Using multiple sensors in the same projectYou can use multiple sensors in the same Arduino sketch with some simple modifications.

1) Prior to the setup loop, if you choose to define names for pin numbers, make sure echo and trigger pins are uniquely defined for each sensor.


2) Also prior to the setup loop, initialize each ultrasonic sensor individually with a unique identifier (ultrasonic_left& ‘ultrasonic_right).


3) In your setup function, set all TRIGGER pins to OUTPUT mode and your ECHO pins to INPUT mode.


4) In your loop or function, create the ultrasonic timing variables (microsec_left&microsec_right) for each sensor using the labels from step 2 (ultrasonic_left&ultrasonic_right)


5) Also in your loop or function, run the measurement command using values from steps 3 and 4, which returns a float. In this example, it returns a value in centimeters.


*NOTE*If your program has multiple functions, your ultrasonic timing variable (STEP 4) must be in the same function as your measurement command (STEP 5).

Library Hardware Compatibility

Hardware Compatibility


Compatibility Issues

If you are aware of a platform that this library does not work with please add it to the list with as much detail as possible.

Library Features

Current Features

  • One Wire support (Achieved by tying the Trigger and Echo pins together, and using the same pin number for trigger and echo when initializing the Ultrasonic Class)
  • Two Wire support
  • Versalino Compatible
  • Includes BUS independant functions
  • still fully drop in compatible with older code (standard deviation excluded)
  • Arduino IDE 1.0 compatible
  • first library to support a maximum distance timeout

Library Version History

Joseph Dattilo (Virtuabotix LLC) – Version 2S0A (05/06/2012)

  • Complete library re-write adding Versalino support.
  • Improved one wire.
  • added three timing overloads to help to eliminate timeout problems.
  • rewrote Pulsein function completely eliminating the possibility of an infinite loop while waiting for the pulse to go low.
  • Added custom timeout overload for timing.
  • Added distance based timeout overload for timing that automatically calculates the max timeout.
  • Removed standard deviation, statistical operations being moved to a dedicated analysis library.

Joseph Dattilo (Virtuabotix LLC) – Version 1S1A (04/21/2012)

  • Added one wire support

Joseph Dattilo (Virtuabotix LLC) – Version 1S0A (02/28/2012)

  • Added Arduino 1.0 Compatibility

cnobile – HCSR04 Library (08/10/2011)

ITead studio. – Concepts (01/20/2010)

No comments yet.

Leave a Reply