# Linearizing Code for Sharp GP2D120 Infrared Ranger

This program takes input from a Sharp GP2D120 infrared ranging sensor and outputs the result in ASCII. The ranging formula comes from an excellent article on Acroname’s site, tweaked a bit to work with my circuit.

Written in Wiring syntax, tested on an Arduino board:

```/*
GP2D120 sensor example
by Tom Igoe

Reads a changing voltage from a GP2D120 IR ranging sensor
decimal numbers.

Arduino/ATMega8 hardware connections:
D0/PD0/RX0: Serial input from the PC via MAX232 or hex inverter
D1/PD1/TX0: Serial output to PC via MAX232 or hex inverter

Distance ranging formula comes from Acroname, http://www.acroname.com:
http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html

Created 6 Oct. 2005
Updated 25 Oct. 2005
*/

int val;             // outgoing ADC value
int distance = 0;

//function prototype:

void setup()
{
// start serial port at 9600 bps:
beginSerial(9600);
}

void loop()
{
// send analog value out:
printString("Analog Value =\t");
// Calculate linear slope of reading (thanks, Acroname!):
distance = (2914 / (val + 5)) - 1;
printInteger(distance);
printString("\n\r");
delay(10);
}

int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
}

```

Written for the BX-24:

```
'GP2D120 sensor example
'by Tom Igoe

' Reads a changing voltage from a GP2D120 IR ranging sensor
' on analog input 0 and sends the result out in ASCII-encoded
' decimal numbers.

' Distance ranging formula comes from Acroname, http://www.acroname.com:
' http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html

' created 6 Oct 2005
' Updated 25 Oct. 2005

dim val as integer             '  outgoing ADC value
dim distance as integer

sub main ()
do
'  Calculate linear slope of reading (thanks, Acroname!):
distance = (2914 / (val + 5)) - 1
'  send analog value out:
debug.print "Distance = "; cStr(distance)
call delay(0.01)
loop
end sub

dim i as integer
for i=0 to howManyTimes
call putPin(26, 1)
call delay(0.2)
call putPin(26,0)
call delay(0.2)
next
end sub

```

Written in PicBasic Pro, tested on a PIC18F252:

```
' GP2D120 sensor example
' by Tom Igoe

'  Reads a changing voltage from a GP2D120 IR ranging sensor
'  on analog input 0 and sends the result out in ASCII-encoded
'  decimal numbers.

' Distance ranging formula comes from Acroname, http://www.acroname.com:
' http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html
' I had to change the final constant to match my sensor's readings.

' Created 6 Oct. 2005
' Updated 25 Oct. 2005

DEFINE  ADC_BITS        10     ' Set number of bits in result
DEFINE  ADC_CLOCK       3         ' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    50        ' Set sampling time in uS

' pins:
tx var portc.6          ' serial TX
rx var portc.7          ' serial RX
resetLED var portb.7    ' blinking LED

' constant to set the baud rate:
inv9600 con 16468

' define variables:
distance var word

' subroutine variables:
i var byte

TRISA = %11111111       ' Set PORTA to all input
ADCON1 = %10000010      ' Set PORTA analog and right justify result

setup:

main:
'  Calculate linear slope of reading (thanks, Acroname!):
distance = (2914 / (adcVar + 5)) - 1
' send the distance out:
serout2 tx, inv9600, ["Distance = ", 9, distance, 10, 13]
pause 10
goto main