Author
Topic: Home made IR range finder (Read 6318 times)

I was looking for a cheap way (not necessarily as accurate) to replace the Sharp GP2D12 or alike sensor that I can't find in my country. Here's what I have found:

The simplest sensor can be made using an IR LED powered by a PWM signal and a maching frequency IR decoder like the PNA4602 (38kHz).

The ATmega8 has 3 PWM signal generators, 2 of them using Timer1 and one of them using Timer2. If you use the first 2 for motor controll (to accelerate, decelerate or PID) there is only Timer2 that remains. Now this timer is only 8 bit and the highest frequency that can generate is given by the formula:

PWM_frequency = Clock_frequency / 256

That means to get a 38kHz signal you will need a crystal of 9.8MHz for your mc. OR, for 36kHz, a crystal of 9.216MHz, or for 40kHz, a crystal of 10.240MHz.

To generate the signal, you have to configure Timer2 to work as PWM generator with prescale = 1, compare pwm = clear up, pwm = on. Then you have to specify the value for the OCR2 register, which sets the duty cycle (128 for 50% duty cycle).

Here is the tricky part. The "on" time of the duty cycle will make the IR LED light up more or less, depending of the value you set. If the LED has more power, the light will reach further, so objects will be detected at a greater distance. All you have to do is set a high value for the duty cycle, take a measurement, if object found, set a lower value, take another measurement and so on, until you have no reflection. Then you can determine approximately the distance an object can be. However this is dependand on the reflecting caracteristics of the object, but in a certain environment (maze for instance) it can be enough.

I am not a programmer, so I don't know to code all this in C. I work mostly in Bascom for my needs and I didn't find the need to change it yet. But I'm sure someone will help you get the code done if needed.

The Parallax BOEbot uses a simple IR LED and detector. By using the "Freqout" PBASIC command, you can determine the proximity of objects. Read all about it in Chapter 8 of the Robotics with the Boe-Bot Student Guide which can be downloaded for free by clicking here --> http://www.parallax.com/dl/docs/books/edu/RoboticsV2_2.pdf

Yes, indeed. In my research I have read their student guide. The difference is that they are playing with the frequency while I play with the duty cycle, for a much finer and longer range. Their method can be used on AVRs by using Timer1 (16 bit) PWM signal generator that doesn't need a special crystal.I think having a fixed frequency and a variable duty cycle works much easier and with better results.

Thanks for the idea, it is really interesting. With enough I/O pins to spare, this can simplify things.

While the freqout is working perfectly with BasicStamp, it will not work with AVR. Each microcontroller has it's own flavor of basic language. You can port most of the code from one to the other. But not all. I am working with Bascom-AVR and this one does not have the freqout command. It has sound command, but they state: "Sound command should be used to generate sounds not precise frequencies. To generate frequencies use a Timer." That is what I did... I used Timer1 and easily replicate the BoeBot method for range finding on a ATmega8 (8MHz internal clock). But then I wanted to use Timer1 for motor controll (I don't use servos but DC geared motors) so I have turned to Timer2. But this one is a little frequency limited and that's why I came up with the solution to modify the duty cycle to adjust the power to the IR LED (same method is used to accelerate or decelerate a motor...). The frequency can also be generated using code. But Timers have the nice feature that they run in parallel with the code (once started, they run until stopped and duty cycle can be changed on the fly), that is why I did not choose to use code to generate the frequency.

All in all, there are many ways to get the same result. I just thought this might be helpfull.