Ultrasonic Distance Sensor
Specs (Click to Open)
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)
1//Include the Ultrasonic Library
2#include "frc/Ultrasonic.h"
3
4//Constructors
5frc::Ultrasonic sonar{Trigger, Echo};
6
7//Create an accessor function
8double getDistance(void)
9{
10 return sonar.GetRangeInches();
11 // or can use
12 return sonar.GetRangeMM();
13}
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)