Ultrasonic Distance Sensor

The Ultrasonic Distance Sensor has been updated from a 3-pin to a 4-pin sensor. This was done to create better compatiblity between multiple different control systems.

../../_images/ultrasonic-1.jpg
Electrical Characteristics

Function

Min

Nom

Max

Input Voltage

5VDC

Current

15mA

Range

2cm

400cm

Measure Angle

15°

Frequency

40Hz

Trigger Pulse

10μS TTL

Programming the Ultrasonic Distance Sensor

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
//import the Ultrasonic Library
import edu.wpi.first.wpilibj.Ultrasonic;

//Create the Ultrasonic Object
private Ultrasonic sonar;

//Constuct a new instance
sonar = new Ultrasonic(Trigger, Echo);

//Create an accessor method
public double getDistance()
{
    return sonar.getRangeInches();
    // or can use
    return sonar.getRangeMM();
}

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)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//Include the Ultrasonic Library
#include "frc/Ultrasonic.h"

//Constructors
frc::Ultrasonic sonar{Trigger, Echo};

//Create an accessor function
double getDistance(void)
{
    return sonar.GetRangeInches();
    // or can use
    return sonar.GetRangeMM();
}

The accessor functions 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)