Introduction: Homemade Infrared Rangefinder (Similar to Sharp GP2D120

About: I'm a student from Cheshire, currently studying at University of Nottingham!

Here is my instructable on how to construct a pretty simple (for some!) short range infrared rangefinder/range sensor. Infrared rangefinders are very useful in a number of projects. The majority of these come from obstacle detection (in robots) or generally detecting distances! The one shown here is only a simple rangefinder and will only really be able to measure about 6 or 7cm infront of the range finder. Luckily, most objects reflect infrared well enough to produce a reading (including a hand, paper and tin foil). I will be showing you how to use the infrared range finder with an Arduino and ways of linearizing the result.

Step 1: Theory

The theory behind an infrared rangefinder is that pulsed infrared is emitted from an IR led and then reflected back off an object into an IR receiver. As light adheres to the inverse square law which states that as distance from a source is increased, the intensity decreases by the square (Source: Essentially, the light is emitted by the infrared LED, which then bounces off the object. In the first instance, the LED is the emitter and the reflective object is the observer. Once the light hits the object, it then bounces off and is reflected back to the IR receiver. The object is then acting as the source of light so the inverse square law takes effect twice. This has the problem that the maximum range of the rangefinder is quite short and to increase the range, higher power LED’s would be required.

Another problem that takes affect with light based rangefinders is how it can be affected by ambient light. I fix this in my rangefinder by modulating the emitting LED. Without this modulation, a simple light bulb connected to the mains can affect the result by superimposing 50Hz onto the actual signal.

My rangefinder works through having a  modulated IR source at an ultrasonic frequency, being detected by a IR receiver (IR photodiode) which is then fed into a high pass filter, amplified and peak detected.

Step 2: Step 1: Making the IR Transmitter

The first step is to create the IR transmitter. As a simple transmitter, I’m using a 555 timer as an inverting Schmitt trigger to create an astable oscillator. Using the 555 timer in this fashion reduces the amount of components required to one resistor and one capacitor. Using a 555 timer isn’t particularly stable as the tolerance of both capacitor and resistor affects the frequency, along with temperature changes. Since the infrared receiver is then only high pass filtered, as long as the filter cut off frequency is below the operating frequency of the transmitter, the filtering properties will work. As can be seen in the schematic diagram, the red section is the transmitter.

With the resistor and capacitor values chosen, the operating frequency will be ultrasonic at a frequency around 46kHz. As stated, this will be both temperature and supply voltage dependent.

It may seem to some that the LED is operating at a current higher than normally specified but since the duty cycle isn't 100% and is being pulsed, the average current will be below the specified 20mA. When building this section, a 100R Resistor was used.

A 555 timer is set up as an inverting schmitt trigger by connecting the trigger to the threshold and connecting the reset to the positive supply. A timing resistor is then placed from the output to the trigger (or threshold, since they're both connected) and a capacitor is placed from the trigger to ground.

Since the 555 timer is more effective at sinking current then sourcing current (take a look at the schematic diagram, the output stage consists of a darlington pair as the current source and a single transistor as the current sink), the LED is connected from the positive source through a resistor to pin 3 (or the output) of the 555 timer.

Step 3: Step 2: Making the IR Receiver

In making the IR receiver, I combined the phototransistor with a transistor to create a quite high gain buffered IR receiver. This works through having the phototransistor in the feedback loop of a common emitter transistor amplifier. Since only a few electrons flows per photon in a phototransistor, gain is required. When creating this section, you have to ensure that the phototransistor is correctly biased.

The phototransistor I used was the L-53P3C bought off eBay for a very cheap price. Any suitable photodiode or phototransistor can be used, just ensure that the spectral response matches that of your IR transmitters. Using a IR filtered photodiode/phototransistor will be even better.

The data sheet states that the maximum on current is 1mA meaning that the worst case scenario will be with current flowing directly through the 18k resistor, into the photodiode and directly from the transistors base to ground, equivalent to a current of 4.3/18000 (4.3 coming from the 5v power supply, minus the 0.7v base to emitter drop, this is as if the transistor was not conducting current through the collector). This gives us a maximum worst case current of 0.238mA, well within the specified current.

The IR receiver is the blue section of the schematic diagram. On the schematic, I have showed how to connect a photodiode. If you're using a photo transistor, just connect the emitter of the phototransistor to the base of the normal transistor and the collector of the phototransistor to the collector of the normal transistor. This creates a kind of darlington pair.

The phototransistor I used was bought from:

Step 4: Step 3: Making the High Pass Filter

One main problem with homemade IR rangefinders is the problem with ambient light affecting results. Sharp fixed this problem with their range of IR sensors by using a CCD sensor (kind of like a little 25x25 pixel camera) to detect the angle at which light is reflected and through simple trigonometry, figuring out the distance to the object. Doing this on the cheap isn’t too easy and so an intensity based sensor can be used.

To overcome the effect of ambient light, the IR transmitter modulates the transmitting LED at a certain frequency. If a precision oscillator (one that doesn’t vary its frequency much, like a crystal oscillator) is used, a band pass filter could be sufficiently used to only pass a certain band of frequencies, for example, if a 32.768kHz oscillator was used for the transmitter ( a common crystal value used in cheap clocks and watches), a band pass filter tuned to this frequency could be used to only pass that frequency and gradually reject surrounding frequencies.

Since I’ve decided (to keep part count down) to use a cheap oscillator, based on the 555 timer, frequency will vary with both supply voltage, temperature and component tolerance, therefore I have opted for a high pass filter only. A high pass filter allows all frequencies above a certain point (known as the cut off frequency) to pass and gradually rejects frequencies below this point. The “slope” of the filter determines how fast frequencies below the cut of point are rejected. I could’ve used a simple RC low pass filter to reject frequencies below this point but this would only have a cut off slope of 6dB per octave (as the frequency below the cut off point is halved in frequency, the amplitude is decreased by 6dB). Instead, I opted for an active high pass filter based around a common collector transistor amplifier.

Since this amplifier now contains a form of gain (current gain in this instance) the slope can be increased to 12dB per octave. Without paying major attention to the maths (which can be found at:, the cut off frequency for the filter in the schematic is around 6kHz, I only chose this value as I had suitable components.

The only other major source of IR above this frequency is TV remotes. These may interfere with IR sensor (I have not tried these yet.)

The high pass filter is also needed to filter out 50/60Hz IR radiated from incandescent lightbulbs.

The high pass filter works through a capacitor being equivalent to a frequency dependant resistor. In a normal RC filter, this works through the capacitor and resistor acting as a frequency dependant potential divider and at any frequency, having an equivalent theoretical resistance value (called capacitive reactance). In an active high pass filter, both the reactive effect of a capacitor and the phase difference on both sides of the capacitor affect the frequency response. Explaining this requires both knowledge of complex numbers and a fair bit of maths so this will be avoided for this instructable. All you need to know is that an active high pass filter has a steeper cut off slope than a passive equivalent!

The high pass section is green on the schematic.

Firstly, a standard common collector amplifier is created by connecting the collector of a transistor directly to the positive rail. A 33k resistor is then connected from the positive rail to the base and then another from the base to ground. The final resistor for the amplifier is an emitter resistor of 4.7k to ground.

Second, the frequency dependant components are added. Firstly, a 1nF capacitor is added from the base to an empty track and an 18k resistor from that empty track to the emitter of the transistor (this is the feedback that increases the slope from 6db per oct. to 12db per oct.)

Another 1nF capacitor is then added from this empty track to the collector of the preceding stage.

If successful, you will now have built the hardest parts of the project!

Step 5: Step 4: Adding the Common Emitter Amplifier

The next stage is to create a common emitter amplifier. A common emitter amplifier provides both voltage and current gain. Since at the moment, after being high passed, the signal amplitude isn’t particularly high, adding a common emitter amplifier is a useful subsystem.

The design I’ve used uses an emitter stabilising resistor, along with collector bias. Using collector bias vs voltage divider bias allows the circuit to be used over a wider range of voltages.

In this version of a common emitter amplifier, a very dirty and quick way of calculating the gain can be found by dividing the collector resistor by the emitter resistor. In this case, 4700/330 which is equal to a gain of around 14 or 23dB.(Source:  If you have an oscilloscope, you can completely cover the two photodiodes (with them next to each other, equivalent to total reflection) and adjust the 330ohm resistor until the amplifier is on the point of clipping. If you really need a massive boost of gain, you can bypass the 330ohm resistor with a 1uF capacitor though I don’t recommend this as this will lead to a temperature dependant gain.

The input impedance of this stage can also be roughly worked out as Re*B||1meg (The emitter resistor multiplied by the gain of the transistor, in parallel with the 1meg bias resistor). In this case, the transistors I’m using are the BC337-25 series. The 25 states that the gain will be at a minimum of 250 (I think!), therefore the input impedance can be calculated as:

1/(((330*250)^-1)+ 1,000,000^-1) = 76212 ohm

With the input capacitor being 1nF, this section also acts as a high pass filter. The cut off frequency of this section can also be calculated using the standard RC filter equation.

This equation is: 1/2*pi*RC = F (Source:

After I calculated this frequency, I got a cut off frequency of just over 2kHz, well below the operating frequency yet still adds to the high pass function of the whole circuit.

The common emitter amplifier is the purple section of the schematic.

Creating a common emitter amplifier is quite easy compared to the past stages. Its quite similar to the IR receiver stage. 

Firstly, another transistor is added with a 4.7k resistor from the positive rail to the collector.

Next, an 330R resistor is added from the emitter to ground. 

A 1meg resistor is then added from the collector to the base.

Finally, a 1nF capacitor is added from the base to the preceding stage emitter to couple the stages.

If you have made it to this point, you're doing brilliant!

Step 6: Step 5: the Final Building Stage! Adding the Peak Detector

Now that you have built up to this stage, you can be happy to know that this is the final section really!

The output from the common emitter amplifier will be a varying signal with its amplitude equal to the exponent of the distance of your hand to the sensor (confusing I know!) . This will be oscillating at the same frequency as the transmitter and will cause all sorts of havoc if you were to try and read this by a microcontroller!

The way that I solved this was to use a passive peak detector. Those of you with some electronic knowledge will have noticed that I haven’t used any Op Amps in this instructable. The reason being that at a power supply of 5v, finding suitable Op Amps can be a bit of a ballache. The standard op amps that one will probably know, the 741, TL082, LF353 all will not work on supplies lower than 8v, causing problems at 5v! For a 5v op amp, something like the TLC272 would work but transistors do the job fine so I’ll stick with it!

A peak detector is a small circuit which detects the peaks of a wave. This is generally done using a rectification circuit and a capacitor. A simple full wave rectifier is a form of peak detector!

In the schematic, the 100nF capacitor couples the emitter stage to the peak detector circuit. The two diodes rectify the voltage so it lies between -0.6v and up to 4.3v. This rectified current then charges up a capacitor (680nF) which is constantly draining into the 18k resistor.

The capacitor and resistor both act as a form of low pass filter and will ensure that the capacitor remains charged on negative parts of the waveform.

On my version of this circuit, the maximum voltage at this point was around 1.1v. Since an Arduino has a resolution of 4.9mV at 8bit, the Arduino will essentially be able to measure around 225 different values. An Arduino also has a high impedance input to the ADC's so the loading effect on the peak detector circuit will be minimum.

The peak detector is the final yellow section of the schematic.

The first part of this is to connect a diode from ground (anode) to the output capacitor (cathode). The cathode is denoted by a black band generally.

Secondly, a diode needs to be added from the cathode of the last diode to an empty track with the cathode of THIS diode going to the empty track.

Thirdly, a 680nF capacitor is added from the empty track to ground with an 18k or 33k resistor in parallel.

Once you have done this, the building is complete, woo hoo!

As with most of the component values in this instructable, most of them can be changed to whatever values you have (for example, I used 18k resistors because I have an abundance of them! Most of the 18k resistors can be replaced by 10k.)

Step 7: Step 6: Testing

Now that you’ve successfully (hopefully!) built the range finder, it’s time for a test!

To test it, you can use either an oscilloscope or a volt meter, I used an oscilloscope for easy of photographing.

To ensure that it is working properly, you must see if the voltage increases as the distance between an object and the sensor decreases. As you will probably see, the voltage will slowly increase and then more rapidly, and more rapidly! If you plot the distance vs voltage, you will see that the it will look a bit like a reciprocal graph (Example: ) where you’re just looking at the first quadrant (top right section). By knowing this form of relationship between voltage and distance will help in linearizing the values which can be done on an Arduino.

As you can see, I used a piece of plastic with a piece of white paper attached as an equivalent object to measure the distance. This suited fine for my purposes and gave sufficient results. You can see the voltage on the oscilloscope as I vary the distance of the plastic and paper.

The maximum voltage output will be when the object is directly infront of the sensor (essentially resting on the two LED's). This voltage will be dependent on the IR emitter drive current and the level of gain in the common emitter stage.

Step 8: Step 7: Connecting Up to an Arduino!

Now that the analog section of the circuit is complete, its time to connect it up to the Arduino. You can now swap the power supply for 5v and ground from the Arduino. The maximum current draw will be around 30mA at peak times. This will not exceed the 500mA USB specification. Luckily, even if you do exceed this current, the Arduino has a resettable 500mA polyfuse, though I still don’t recommend exceeding the maximum current!

Next, connect the output of the peak detector directly to one of the analog inputs (I used A0 for my input). This is all you need to connect to the Arduino!

Step 9: Step 8: Arduino Programming

For the programming section, that’s really for you to decide what to use it for. I will just show a simple Analogread situation with the math required for linearizing. Linearizing will be much easier with the aid of excel.

The code is as follows for just reading the input, this is dependent on the input being A0. Change to your spec!
int Readvalue; //The initially read value
int Printvalue; //The value printed to the screen
float Mathvalue; //The variable used for any form of maths

void setup(){
  Serial.begin(9600); //Begin serial communication with computer at 9600bps

void loop(){
  Readvalue = analogRead(A0)+1; //Read the infrared range finder input, add 1 to ensure that the reading never equals zero or you will get strange maths results!
  Serial.println(Readvalue); //Print the read value to the serial monitor
  delay(50); //Delay for 50ms as to not fill the serial buffer

Once programmed, you should access the serial monitor and see if the value changed with distance from the sensor. If so, your Arduino is correctly reading the sensor!

One useful thing to do would be to use the map function and find the maximum and minimum values from the sensor and map them to 0 and 1023. This will be equivalent to a normal Analogread!

Step 10: Step 9: Linearizing the Results

The final part of this instructable will be on linearizing the results as currently, they are not as you would expect (you can see this if you plot a graph of voltage against distance!).

I placed the results into a table and plotted a graph from these results. I also included an exponential trend line. If you remember the graph I showed in a past step, you can see it’s quite similar, excel doesn’t allow the plotting of 1/x graphs!

Now, by looking at the math functions that the Arduino can do limits us a bit to how we can linearize these results. By playing around with the functions in excel will help us find suitable one but as the exponential trend line fits, I found that it would only be appropriate to do the inverse of an exponent (an exponent is equal to e^x), the inverse is equal to the natural logarithm (known as ln) of x. By doing the inverse exponent, the results will be much more linear and the Arduino allows this function. If you plot this graph, you will be able to apply a linear trend line and have a quite acceptable R^2 value.

Now that you have linearized the results, you need to invert the gradient. Currently, as distance is decreased, voltage increases. This is not a viable form of measurement as one would expect a value to decrease as the voltage increased!

The easiest way to do this would be to invert the results of the log by multiplying by minus 1. This will then invert the gradient and the resultant value will increase as distance increases!

The next part is applying a relationship between these values and distance. Firstly, you want the value at 0 distance to be zero. You do this by taking the log of your largest experimentally found voltage and adding this to all of the values.

You will then get a result that the smallest voltage gives the largest value and the largest voltage then gives a value of 0.

Now you have a linear function that converts the non linear voltage. All you now need to do is map this to the equivalent distance, e.g. the largest value that you calculated should be equal to the distance that you measured (for that value, its confusing I know!). For this final step, all you need to do is multiply by the largest distance measured divided by the normalized result and you will have a function that will give you a successful distance reading! Note that if you change any of the circuit parameters, or adjust the temperature, this will change! Hence why its only simple.

This will make much more sense once you read the code, honest!

The new code is:
int Readvalue; //The initially read value
int Printvalue; //The value printed to the screen
float Mathvalue; //The variable used for any form of maths
float Normalize_constant = 0.47; //The variables that I calculated from my results
float Scale_constant = 3.34; //Same as above!

void setup(){
  Serial.begin(9600); //Begin serial communication with computer at 9600bps

void loop(){
  Readvalue = analogRead(A0)+1; //Read the infrared range finder input, add 1 to ensure that the reading never equals zero or you will get strange maths results!
  Mathvalue = log(Readvalue) //Apply the log of the read value
  Mathvalue = Mathvalue*-1 //Invert the log values
  Mathvalue = Mathvalue+Normalize_constant //Normalize the results, my normalize constant was equal to LN(1.6) as calculated from my results. Yours will vary!
  Mathvalue = Mathvalue*Scale_constant //Multiply by the scale constant to ensure that the distance measured is the same as the values. 
  Serial.println(Mathvalue); //Print the final math value to the serial monitor
  delay(50); //Delay for 50ms as to not fill the serial buffer

Step 11: Step 10: Finished!

Finally! You’re finished, you should hopefully have constructed an infrared rangefinder using cheap components found from any electronics store! Screw you sharp and your expensive infrared sensors, robots will be using these from now!

Thanks for reading! :)

Instructables Design Competition

Participated in the
Instructables Design Competition

SciStarter Citizen Science Contest

Participated in the
SciStarter Citizen Science Contest