Ultrasonic Distance Sensor

../../_images/ultrasonic-1.svg

Specs (Click to Open)
Ultrasonic Specs

Function

Min

Nom

Max

Input Voltage

5VDC

Current

15mA

Range

2cm

400cm

Measure Angle

15˚

Frequency

40Hz

Trigger Pulse

10µs TTL

Programming

 1//import the Ultrasonic Library
 2import edu.wpi.first.wpilibj.Ultrasonic;
 3
 4//Create the Ultrasonic Object
 5private Ultrasonic sonar;
 6
 7//Constuct a new instance
 8sonar = new Ultrasonic(Trigger, Echo);
 9
10//Create an accessor method
11public double getDistance()
12{
13    return sonar.getRangeInches();
14    // or can use
15    return sonar.getRangeMM();
16}

The accessor methods will then output the range in either inches or mm.

Note

The valid digital pairs for Trigger and Echo pins are (Trigger, Echo) (0,1), (2,3), (4,5), (6,7), (8, 9), (10,11)