IR range measurement!!!

Rudra

New Member
Hi,
I am trying to get a distance reading from Sharp GP2Y0A21YK0F Infrared range finder. I used the following code to get the distance reading;

main:
readadc10 0, w2
let w2=w2+1
let w2=6500/w2
let w2=w2-4
let w3=w2
debug w3
goto main

I am getting a fluctuating reading, the distance reading seems to vary +/- 7cm from the actual value. I tried to ground the unused ADC inputs but still the problem is the same. If anyone have any experience on this please help.
Thanks in advance
Rudra
 

gbrusseau

Senior Member
Byte variables cannot use negative numbers or fractions. Dividing a number by a variable, may result in a fraction. I think what happens then is the fractional results is dropped from the results. The division (/) command returns the quotient (whole number) word of a
word*word division. The modulus (// or %) command returns the remainder of
the calculation.
7/3 = 2.34, but the PICAXE drops the .34 and the results is reported as 2
 

Rudra

New Member
I am using two 6V Nicad battries with a 5V voltage regulator. This supplies power to both microcontroller and sharp range finder.

The problem is like this if the actual range is 35 centimeter the data on w3 fluctuates from 28-35. I am not worried about the decimals, I am more worried about the whole number.
 

Dippy

Moderator
1. Have you tried actually measuring the voltage returned by the sensor with a fast multimeter or scope? i.e. is it the sensor output wavering? If it is 'noise' then only a scope will show it. (You haven't got a 'scope? Oh dear :( )

2. In some of the Sharp Data Sheets they recommend a 10uF capacitor connected as close to the sensor power terminals as possible.

3. Have you checked there are no instability issues with power supply? Remember, the PICAXE ADC uses the PIC +V supply as the ADC reference. If this is wobbly then your ADC results will be wobbly. You've obviously read the Regulator DATA SHEET to get the recommended values for the capacitors?? (Don't tell me: you bought it from Ebay and there is no Data Sheet. Oh Dear :( )

4. Are your connections good?

5. Is it being interfered with by any external electrical items or fluorescent lighting?

I had a problem recently with unstable ADC problems on a PIC. It turned out to be a lack of suitable capacitance on the 5V line. In my case, the capacitance on the Vout from the regulator was too low. I changed it to a 10u tantalum and 47nF ceramic in parallel and now it is excellent.

A really good stable supply and decoupling on the PIC power pins is very important for stable ADC results.
Note: In my case the 47nF ceramic is right by the PIC for decoupling.

Maybe experiment with a small capacitor on the signal line too ....?

This is all assuming that your target is suitable and rock-steady of course!!
And assuming that the regulator you are using is man enough to power both devices??
 

slurp

Senior Member
And assuming that the regulator you are using is man enough to power both devices??
This is a very good point, lots of small circuits are built with 100mA regulators with numerous LEDs pushing 5-10mA this is soon eaten up. This Sharp sensor drags another 30mA which could be having an effect if the regulator isn't up to the job.

Decoupling capacitors may be of benefit if the regulator is up to the job (better than boarder line) otherwise they never get charged enough to really assist.

best regards,
Colin
 

hippy

Ex-Staff (retired)
@ Rudra : Have you used the enhanced download circuit ? This is recommended when requiring accurate ADC and using the PICAXE with the download cable connected.

I would also use additional variables in your program so you can see what the raw input values are as well as your processed result. As gbrusseau suggests, you may be losing accuracy in the processing. The input may actually be quite stable and consistent but the processing is causing wild swings.

You may also be able to use SERTXD and Programing Editor Terminal instead of DEBUG to get a better view of what is happening ...

Do
ReadAdc10 0, w2
w3 = w2+1
w3 = 6500/w3
w3 = w3-4
SerTxd( "Raw ADC=",#w2," Result=",#w3,CR,LF)
Loop
 

Rudra

New Member
Thanks for the answers this is what I did after reading all the answers.I connected the output of the sharp rangefinder to a digital voltmeter.I removed the voltage controller and connected a 6volt battery with a diode to get 5.3V.
The output voltage is stable upto 1 decimal place but at two decimal place the voltage fluctuates .03 volts.For example according to the datasheet the output voltage at 60cm is approximately .50volts but the reading fluctuates from .50 volts to .53 volts.
I connected a 10uF near the power supply of sharp rangefinder it did not help much.The only improvement that I got was that the fluctuation reduced to .01 vots.Adding a .33uF tantalum capacitor helped to stabilize the output a bit more.
So in the end this fluctuation of voltage after 1 decimal place remains.
The actual robot is supposed to be a wall following robot.With all the motors running the electrical noise would be higher.
 
Last edited:

Dippy

Moderator
How does that fluctuation compare to range based on the Data Sheet?
i.e. what does 0.02V respresent in terms of range-change (all other things being equal).

Sadly, I haven't got the time to do that for you.

And, using a drop of maths, how does that change compare to ADC changes?
Remember, when you are at the 'lower' end of the ADC an absolute number change has a bigger percentage change.

Have you checked your ADC by using a simply potential divider (2 resistors) to represent that voltage?
i.e. check your PICAXE/code stability.
You've got a bit of work to do.

Can you s'cope the output?

Have you scoped the signal line and the 5V power line with the motors running? (to check for noise)
Have you done anything to suppress electrical noise?
Have you put a nice decoupling capacitor right by the PICAXE power pins?
 
Last edited:

BeanieBots

Moderator
Dippy, you're thinking the same as me. Close to max range.
A fluctuation of 0.03 on a 5v signal is not bad but at long range when the output is around 0.5v it becomes much more significant.

That's probably as good as it's going to get but do check the datasheet.
If the datasheet confirms that, then feed the PICAXE ADC input via a 10k resistor with a 100nF capacitor between ADC input and 0v. It will slow the response but should give a more stable reading.
 
Top