Ultrasonic Rangefinders

The WPI Ultrasonic class provides a simple interface for programming an Ultrasonic Rangefinder. VMX-pi supports Ultrasonic Rangefinders, as described in this guide.

To access WPI Library LED Array support, ensure that the VMX-specific Gradle Rio plugin version is updated to version 2020.3.2.3 or higher.

Supported Ultrasonic Sensor

The WPI Ultrasonic class supports Ultrasonic Rangefinder sensors which provide two separate signal connections:

  • a “Trigger” pin which transmits a pulse to the sensor requesting it to initiate ranging, and
  • an “Echo” pin which returns a pulse with different lengths depending upon the distance detected by the sensor.

The “Two signal wire” Ultrasonic Rangefinders currently supported on VMX-pi include:

  • Daventech SR04
  • HC-SR04

NOTE 1: Several other completely different types of Ultrasonic Rangefinder exist currently (1-signal-wire sensors such as the Parallax Ping) which use a single wire protocol in which the “trigger” request pulse and the “echo” response pulse are communicated on the same pin.  These sensor types are not currently supported by the WPI Ultrasonic class.

NOTE 2: Other types of Ultarsonic Rangefinder do not require a “Trigger”, but rather simply provide a single wire upon which an analog voltage – which varies based upon the range detected by the sensor – it output. In this case, the sensor analog output may be connected to one of the VMX-pi Analog Input channels. This type of device is also simple to use, but is not currently supported by the WPI Ultrasonic class; rather, the WPI AnalogInput class can be used to retrieve the current voltage and convert the voltage to a distance.

Supported VMX-pi Channels

Trigger (Output) Channel

The Trigger Channel may be any VMX-pi Channel that can be configured as a Digital Output.

Echo (Input) Channel

The Echo Channel must be any VMX-pi Channel that can be configured as a WPI Counter in “kSemiPeriod” mode. As further discussed in the “Counter” section of the VMX-pi IO Map for FRC, all FlexDIO channels (WPI DIO Channels 0-11) are kSemiPeriod-capable, as shown below:

kSemiPeriod-capable FlexDIOs 0-7
kSemiPeriod-capable FlexDIOs 8-11

Basic Electrical Connectivity between the Ultrasonic Rangefinder and VMX-pi

This section summarizes the basic requirements and strategy for electrical connections between the Ultrasonic Rangefinder and VMX-pi.

HC-SR04 Ultrasonic Rangefinder with VCC, Trigger, Echo and GND pins

Supplying Power to the Ultrasonic Rangefinder

Ultrasonic Rangefinders like the HC-SR04 shown above require a 5VDC power supply. To output sufficient voltage from the VMX-pi to a 5VDC Ultrasonic Rangefinder, be sure to set the FlexDIO Voltage Select jumper to 5V. It’s always best to check the device datasheet if you have any questions about specifications such as these.

Interfacing with the Ultrasonic Rangefinder signals

Simply connect your chosen “Trigger Channel” and “Echo Channel” on VMX-pi to the corresponding sensor pins.

Programming the Ultrasonic Rangefinder

Using the WPI Ultrasonic class, there are two steps:

  • Initializing the Ultrasonic class, providing it the appropriate channel numbers
  • Periodically “pinging” the Ultrasonic Rangefinder, waiting several milliseconds until the result is available, and then reading the “Distance” value from the sensors

The simple Java example below demonstrates both of the above steps in a WPI Library-based Robot application:

public class Robot extends TimedRobot {  

private static final int kUltrasonicPingPort = 8;

private static final int kUltrasonicEchoPort = 9;
  private final Ultrasonic m_ultrasonic = 

new Ultrasonic(kUltrasonicPingPort, kUltrasonicEchoPort);
  

@Override  public void robotInit() {   }


  /**   * Teleop:  acquire distance from sensor and print to console.   */  

@Override  public void teleopPeriodic() {

     m_ultrasonic.ping();

     Timer.delay(0.005);  // Wait awhile for the rangefinder result

     System.out.println(“Ultrasonic Distance: ” + m_ultrasonic.getRangeInches());  

}

}