Convert analogue voltage to distance value(GP2D12 sensor)?

Thread Starter


Joined Jul 8, 2007
I am new beginner in PIC microcontroller. I use PIC16F877A and Hi-Tech PICC LITE Compiler (C compiler). I already wrote C codes to get analogue voltage from GP2D12 distance sensor. But, i tried many times to convert it into distance value based on datasheet. I still can't get it. I NEED some help. I hope everyone can share experience and idea with me. The C codes as shown below. Thanks.



Joined Apr 20, 2004
You might spend some time learning about A to D converters.

Your PIC has a 10 bit A to D converter with a 5 volt full scale reading. That will give you 1024 (3FF) as a full scale reading. The lsb resolution is then 5/1024, or 4.88 mv.

If your distance sensor is measuring 50 metres for a 5 volt input, then the smallest distance the PIC can distinguish is 4.88 mm. To convert a reading to a distance, multiply the number from the A to D times 4.88 mm.