Ir proximity sensor

While surfing the net , I came across many tutorials for proximity detection using ir led and receiver. Most of them have connected the ir led to an analog output pin in the arduino and have connected the receiver in an analog input pin. Does the setup work in this way? Doesn't the analog pin output a variable voltage? And for transmitting an ir signal don't we need to use an oscillating carrier wave? Please clarify my doubts.

analogWrite does NOT output a variable voltage. It outputs a variable duty cycle (PWM), with fixed voltage (VCC) and fixed frequency.

For short distances, constant IR light and a photodiode can be used. For longer distances and ambient light, modulated IR (38khz) and a demodulating 3-pin receiver can be used. There are many more ways of proximity detection, like PIR and ultrasound. Leo..