Varying intensity of LED using sharp IR

I am using sharp IR GP2YOA21YKOF analog sensor for my project.
i need to vary the intensity of the LED according to the distance measured by the Sharp sensor.
That is, the intensity of LED has to decrease as an object approaches the sensor and vice versa.
since i am a newbie, kindly help me with the code.
Thank you.


The analog output of the sensor does not provide enough current to drive an LED directly, however, you could probably create a simple circuit utilizing a bipolar junction transistor to control the current through the LED (which is proportional to it’s brightness) using that output. The components you would need would depend on the power of the LED you are using. You might ask around on some general electronics forums (where you are more likely to find someone familiar with this kind of thing) to help you create such a circuit.


Hello again,
thanks for the reply.
But, I am using aurdino board here.I am connecting LED to the board.
The brightness of LED will be controlled by the PWM signals sent through code.
and to the same board, sharp sensors are connected depending upon which the brightness has to be varied.
do we need an extra circuit to drive the LED!!!??
correct me if i’am wrong.
Thank you.


Driving an LED from an Arduino generally requires a little bit of additional circuitry. It you are driving the LED with low current (less than 20mA), you should be able to just add an appropriate current limiting resistor. If you are using a higher-power LED, you will need something like a MOSFET to switch the higher current (along with the aforementioned current limiting resistor).

If you have questions about how to code something like this, you might look at the AnalogInput example sketch in the Arduino IDE for an example of how to read an analog input, like the Sharp sensor you mentioned, using the analogRead() function. Also, the Fading sketch shows an example of how to use the analogWrite() function to send a PWM signal to something like an LED.


That was really a helpful suggestion!
Thank you:)