== Ultrasonic Rangefinder Robots need to sense the environment around them, particularly when driving autonomously. Ultrasonic sensors are like the sonar used by bats. [[Image(UltrasonicSensor.jpg,right,250px,margin=10)]] They emit a chirp of sound and measure the time it takes to hear an echo of that chirp. The longer the time, the further the sound traveled. Since sound travels at a particular speed (about 1125 ft/s) through air, we can use the echo delay to calculate distance to the object was that reflected the sound. You can read more about ultrasonic sensors on [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599715-ultrasonic-sensors-measuring-robot-distance-to-a-surface ScreenStepsLive] Ultrasonic sensors are connected to the RoboRIO using DIO ports. In the example below, a sensor's Ping and Echo wires are connected to DIO1 and DIO0. Create another program using the !TimedRobot java template and name it !UltrasonicTest. Modify the auto-generated template code as follows: * import the Ultrasonic class {{{ import edu.wpi.first.wpilibj.Ultrasonic; }}} * declare a ultrasonic variable in the Robot class {{{ private Ultrasonic f_ultrasonic; }}} * in robotInit() instantiate an ultrasonic object and set it to start automatically ranging {{{ // Sensor is connected to DIO1, DIO0 f_ultrasonic = new Ultrasonic(1,0); // Start sensor continuously pinging f_ultrasonic.setAutomaticMode(true); }}} * in robotPeriodic() read and display the range {{{ if (f_ultrasonic.isRangeValid()) { SmartDashboard.putNumber("Front range", f_ultrasonic.getRangeInches()); } }}} Build and Deploy the program to your robot as you did in the [wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/XboxController previous example] and observe the "Front range" value in the [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/599724-displaying-data-on-the-ds-dashboard-overview Smart Dashboard] as you move the robot towards and away from the wall. Notice that the measurements aren't always perfect; the sensor may receive echos from multiple surfaces. Extra Credit: explore more advanced ultrasonic sample program [wiki:ControlSystems/SampleCode/Ultrasonic here]