wiki:ControlSystems/Sensors/Ultrasonic

Inexpensive HC-SR04 ultrasonic sensors https://www.robotshop.com/media/catalog/product/cache/image/625x625/9df78eab33525d08d6e5fb8d27136e95/h/c/hc-sr04-ultrasonic-range-finder-2.png are helpful for learning about sensors and making basic robots more interactive. Every team should have a box of sensors and cables ready to connect to a roboRIO. The sensors are cheap and plentiful; the main obstacle is usually cabling and although students can crimp nice new cables with locking connectors, for many purposes, it is quicker and easier to make cables using off-the-shelf materials; it takes only a few minutes to wire them as needed and this is something rookie students can do without special training and minimal supervision to get comfortable with wiring. It also avoids most of the hassle associated with poor crimps.

Materials Needed:

Everything is available from Amazon, AliExpress, Gearbest, Banggood, etc. depending on how fast you need it. Instructions for students:

Using sharp tweezers or the tip of a pointy knife or small screwdriver, remove the two black plastic shell/housings from the 4-wire cable: slightly lift the black tabs that retain each wire in the connector and slide the wires out so you're left with a 4-wire bundle and two 4-pin shells. Watch this video to see how it's done.

  1. Reinsert the wires from one end of the 4-wire bundle into one of the 4-pin shells in this order: Red(Vcc), White(Trig), Yellow(Echo), Black(Gnd).
  2. Reinsert 3 of the 4 wires on the other end of the bundle into a 3-pin shell in this order: Black (Gnd), Red (5v), Yellow (S) and label it A
  3. Reinsert the last wire into one end of another 3-pin shell: White (S), Empty, Empty and label it B
  4. You can discard or save the left-over 4-pin shell housing.

Use the new cable to connect the ultrasonic sensor to the RoboRIO as follows:

  1. Plug the 4-pin connector into the HC-SR04 with the Red wire on the Vcc pin and the Black wire on the Gnd pin - this is important because the sensor will be destroyed if you connect it backwards.
  2. Plug connector A into one of the DIO ports (let's say DIO 0) with the black wire on the OUTSIDE edge of the roboRIO
  3. Plug connector B into another DIO port (let's say DIO 1) with the white wire towards the INSIDE edge of the roboRIO

You can use the ultrasonic sensor in your programs by creating an Ultrasonic object. WPILib already provides an Ultrasonic class that makes things easy. A sample snippet is below:

import edu.wpi.first.wpilibj.Ultrasonic;

public class Robot extends IterativeRobot {
  private Ultrasonic ultrasonic;
  @Override
  public void robotInit() {
    ultrasonic = new Ultrasonic(1,0); // trig, echo
    ultrasonic.setAutomaticMode(true);
    ultrasonic.setEnabled(true);
  } 

  @Override
  public void teleopPeriodic() {
    double range = ultrasonic.getRangeInches();
    // do something with the range
  }
}

Students should first get comfortable using the sensor and displaying the range readings on the console, a good next exercise is to add collision avoidance to a robot.

Last modified 3 years ago Last modified on Oct 11, 2018, 3:14:01 PM