Small low-cost ultrasonic distance measurement modules like HC-SR04 are a cost effective way to sense the distance of nearby objects. Frequently robots use ultrasonic measurement modules to adapt to the environment and sense objects or collisions. These modules they have two transducers; one acts as a speaker which emits ultrasonic waves, the other acts as a microphone to receive the ultrasonic waves that have returned from an object.
The HC-SR04 requires 5V to run so can be powered from an Arduino or Raspberry Pi but since it uses 5V logic, its important that you take precautions when using it with your Raspberry Pi. It's explained very well in the video below:
For the Arduino, its pretty easy to wire up for use:
There is also an improved library and example sketch for the Ultrasonic sensor: