What you really need is a 'proximity sensor'. As the name suggests, proximity sensors sense the proximity of an obstacle. (Du-uh ?!)
The most common proximity sensors used are IR LED-Photodiode based. In alternative implementations, Photodiodes can be replaced by Phototransistors, IR LED can be replaced by a LASER source. But, in all these implementations, the principle is the same:
There is a transmitter and a receiver( also called photodetector). The transmitter transmits EM rays all the time...if the receiver receives any of these rays, it can mean two things - either you have an obstacle in front of you reflecting these rays or you have another source of the same type of rays. Thus, this technique may not be the best obstacle-detecting technique around...but yeah..we still use it.
The following schematic made in TinyCAD illustrates the connections for a proximity sensor using IR LED and a Photodiode. Please note, this is a very basic circuit. Generally, the output voltage is first converted to a digital signal for use ( more on this later :) )
Basic Functioning of the Circuit
The IR LED is connected in series with a 330 ohm resistor to provide the correct voltage drop across the LED.
The Photodiode that is used must be sensitive to only that range of frequencies that the IR LED emits, else the purpose is lost - it is like a Chinese man receiving a message in Tamil. A Photodiode is used because it has the property that when reverse biased, the current it allows to pass across itself is a function of the intensity of optical illumination. Hence, it is very important to have the Photodiode reverse-biased i.e. the longer leg should have a lower potential than the shorter leg. Only then, your circuit shall work as desired.
As the robot goes nearer to the obstacle, more the number of reflected rays, more the optical illumination, more the current across the Photodiode and the resistor, more the output voltage..SIMPLE EH ?
Approximate cost break up
IR LED - Rs. 5-7
Photodiode - Rs. 5-10
Resistors - nominal
1) In the above circuit you can replace the IR LED by a laser source, so you will have to change your receiver accordingly, since the frequency of the rays has changed.
2) Also, if you want to increase the sensitivity of your sensor, you can replace the Photodiode by a Phototransistor as it provides a reasonable change in current even for a very small change in illumination at the cost of a teeny-weeny change in the circuit.
3) Yet another implementation which is much less complex is that using a Photoresistor or Light Dependent Resistor (LDR). But this type of proximity sensor is not generally used because it provides an appreciable output only after going very close to the obstacle, which is undesirable. Also, owing to memory effect, the response of LDR to changing illumination is very very slow compared to IR LED-Photodiode pair.
Hope this post was useful.