Fade led with Infrared input

I am making an interactive LED circuit that takes a calibration reading at the beginning, then checks against that during the loop. I am using an infrared emitter, infrared photodiode, and an led… ( with obvious other components like resistors, capacitors, etc.). What I want my program to do better is led fading. When my hand is as far away as the sensor will allow as a change, the led should be fairly dim, growing brighter and brighter as my hand gets closer to the receiver. My code can sort of do this, but instead of being smooth, the led pulses when my hand is anywhere but as close as possible to the receiver. I need it to be a very smooth transition as my hand gets closer and closer… brighter and brighter. Here is the code to test this…

int threshold;

 void setup()

   threshold = analogRead(A5);

void loop()

   int Receiver = analogRead(A5);
    if( Receiver < ( threshold - 3 ))
    if( Receiver < ( threshold - 3)) && ( Receiver < ( threshold - 4)) // checks for when the Receiver is less than ( receiver - 4) 
       // ... and so on... all the way to about (threshold - 45) .. so I skip Receiver val every 1, but the analogWrite val every 4, because of scaling issues

any ideas on how to make it smoooooooth?

Google the term running average

ok new question...

as I said my setup is working fairly well, but a few things could be better, for instance, the sensitivity. I can get the led to turn on when my hand is about 4 inches away from the sensor, but what can I say, I'm American and I want MORE. Id like to be able to have the led turn on at about twice the distance. I think using a transistor, which I have in my possession, would help achieve this. Not sure how to plug it in from A5 and ground and + 5 volts and all that though. Also, do I need a resistor or capacitor for any reason with this?


If you need more distance you will either have to improve sensitivity on the detector side or light intensity on the emitter side.

I assume you were referring to a phototransistor although this will only help if it is more sensitive than your photodiode.

Easiest and most direct way would be to increase light output of your emitter. Get one with more power or a smaller beam angle, this will immediately improve maximum distance.

For more detailed recommendations you will have to provide more information on your current setup and parts...

Using these products from mouser..


EverLight infrared led 940 nm .... 638-IR333C/H0/L10

94k ohm resistor with the photodiode...

200 ohms with the led...


How about an IR emitter like this from mouser

782-TSAL5100 = 1000 mW/sr ( the one I use now is 450 mW/sr )

... too much or no??