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

Libraries

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.

virtuabotix-ultrasonic-hc-sr04-timing-diagram

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.

virtuabotix-ultrasonic-one-wire-timing-diagram

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.

unsignedintreadDistance(uint8_tmyTrigger,uint8_tmyEcho){
unsignedintduration=0;//initializetobesurethatitgetsavalid0start
unsignedintdistance=0;

pinMode(myTrigger,OUTPUT);//setupthetriggerandechopins
pinMode(myEcho,INPUT);//placethisbelowthetriggerpointforsinglewiremode

digitalWrite(trigPin,LOW);//makesurethetriggerisoff
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);//triggerthesensor
delayMicroseconds(10);
digitalWrite(trigPin,LOW);//turnoffthetrigger
duration=pulseIn(echoPin,HIGH);//readthenumberofmicrosecondsforecho
distance=(duration/29.1)/2;//translatedistanceintoinches
//soundtravelsat29.1ųs/cmandwehadtogothereandbacksodivideby2
returndistance;
}

Guides

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.

#defineECHO_PIN_LEFT4
#defineTRIGGER_PIN_LEFT5
#defineECHO_PIN_RIGHT6
#defineTRIGGER_PIN_RIGHT7

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

Ultrasonicultrasonic_left(TRIGGER_PIN_LEFT,ECHO_PIN_LEFT);
Ultrasonicultrasonic_right(TRIGGER_PIN_RIGHT,ECHO_PIN_RIGHT);

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

pinMode(TRIGGER_PIN_LEFT,OUTPUT);
pinMode(ECHO_PIN_LEFT,INPUT);
pinMode(TRIGGER_PIN_RIGHT,OUTPUT);
pinMode(ECHO_PIN_RIGHT,INPUT);

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)

longmicrosec_left=ultrasonic_left.timing();
longmicrosec_right=ultrasonic_right.timing();

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.

floatleft_distance,right_distance;
left_distance=ultrasonic_left.convert(microsec_left,Ultrasonic::CM);
right_distance=ultrasonic_right.convert(microsec_right,Ultrasonic::CM);

*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

  • HC-SR04VERIFIED
  • HY-SRF05DOES NOT SUPPORT ONE WIRE

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