{"id":44,"date":"2005-10-25T15:16:29","date_gmt":"2005-10-25T20:16:29","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/44"},"modified":"2007-08-07T09:39:37","modified_gmt":"2007-08-07T14:39:37","slug":"linearizing-code-for-sharp-gp2d120-infrared-ranger","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/44\/","title":{"rendered":"Linearizing Code for Sharp GP2D120 Infrared Ranger"},"content":{"rendered":"<p>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 <a href=\"http:\/\/www.acroname.com\">Acroname&#8217;s site<\/a>, tweaked a bit to work with my circuit.<\/p>\n<p><!--more--><\/p>\n<p>Written in Wiring syntax, tested on an Arduino board:<\/p>\n<pre>\n\/*\n GP2D120 sensor example\n by Tom Igoe \n \n  Reads a changing voltage from a GP2D120 IR ranging sensor\n  on analog input 0 and sends the result out in ASCII-encoded\n  decimal numbers.\n   \n Arduino\/ATMega8 hardware connections:\n A0\/PC5: potentiometer on analog in 1\n D0\/PD0\/RX0: Serial input from the PC via MAX232 or hex inverter\n D1\/PD1\/TX0: Serial output to PC via MAX232 or hex inverter\n \n Distance ranging formula comes from Acroname, http:\/\/www.acroname.com:\n http:\/\/www.acroname.com\/robotics\/info\/articles\/irlinear\/irlinear.html\n \n Created 6 Oct. 2005 \n Updated 25 Oct. 2005\n *\/\n\nint val;             \/\/ outgoing ADC value\nint distance = 0;\n\n\/\/function prototype:\nvoid blink(int howManyTimes);\n\nvoid setup()\n{\n  \/\/ start serial port at 9600 bps:\n  beginSerial(9600);\n  blink(3);\n}\n\nvoid loop()\n{\n  \/\/ read analog input:\n  val = analogRead(0); \n  \/\/ send analog value out:\n  printString(\"Analog Value =\\t\");\n  \/\/ Calculate linear slope of reading (thanks, Acroname!):\n  distance = (2914 \/ (val + 5)) - 1;\n  printInteger(distance);\n  printString(\"\\n\\r\");\n  \/\/ wait 10ms for ADC to reset before next reading:\n  delay(10);                 \n}\n\n\n\/\/ Blink the reset LED:\nvoid blink(int howManyTimes) {\n  int i;\n  for (i=0; i&lt; howManyTimes; i++) {\n    digitalWrite(13, HIGH);\n    delay(200);\n    digitalWrite(13, LOW);\n    delay(200);  \n  }\n}\n\n<\/pre>\n<p>\nWritten for the BX-24:<\/P><\/p>\n<pre>\n\n 'GP2D120 sensor example\n 'by Tom Igoe \n \n ' Reads a changing voltage from a GP2D120 IR ranging sensor\n ' on analog input 0 and sends the result out in ASCII-encoded\n ' decimal numbers.\n\n ' Analog in is on pin 15 of the BX-24\n   \n' Distance ranging formula comes from Acroname, http:\/\/www.acroname.com:\n' http:\/\/www.acroname.com\/robotics\/info\/articles\/irlinear\/irlinear.html\n\n \n' created 6 Oct 2005\n' Updated 25 Oct. 2005\n \n\ndim val as integer             '  outgoing ADC value\ndim distance as integer\n\nsub main ()\n    call blink(3)\ndo\n'  read analog input:\n  val = getADC(15)\n   '  Calculate linear slope of reading (thanks, Acroname!):\n  distance = (2914 \/ (val + 5)) - 1\n '  send analog value out:\n  debug.print \"Distance = \"; cStr(distance)\n  '  wait 10ms for ADC to reset before next reading:\n  call delay(0.01)                 \nloop\nend sub\n\n\n'  Blink the reset LED:\nsub blink(byVal howManyTimes as integer) \n  dim i as integer\n  for i=0 to howManyTimes \n    call putPin(26, 1)\n    call delay(0.2)\n    call putPin(26,0)\n    call delay(0.2)  \n  next\nend sub\n\n<\/pre>\n<p>Written in PicBasic Pro, tested on a PIC18F252:<\/p>\n<pre>\n\n' GP2D120 sensor example\n' by Tom Igoe \n \n'  Reads a changing voltage from a GP2D120 IR ranging sensor\n'  on analog input 0 and sends the result out in ASCII-encoded\n'  decimal numbers.\n   \n' Distance ranging formula comes from Acroname, http:\/\/www.acroname.com:\n' http:\/\/www.acroname.com\/robotics\/info\/articles\/irlinear\/irlinear.html\n' I had to change the final constant to match my sensor's readings.\n \n' Created 6 Oct. 2005 \n' Updated 25 Oct. 2005\n \n ' Define ADCIN parameters\nDEFINE  ADC_BITS        10     ' Set number of bits in result\nDEFINE  ADC_CLOCK       3         ' Set clock source (3=rc)\nDEFINE  ADC_SAMPLEUS    50        ' Set sampling time in uS\n\n' pins: \ntx var portc.6          ' serial TX\nrx var portc.7          ' serial RX\nresetLED var portb.7    ' blinking LED\n' analog in is on porta.0\n\n' constant to set the baud rate:\ninv9600 con 16468\n\n' define variables:\nadcVar var word\ndistance var word\n\n' subroutine variables:\ni var byte\n\nTRISA = %11111111       ' Set PORTA to all input\nADCON1 = %10000010      ' Set PORTA analog and right justify result\n\nsetup:\n  gosub blink\n\nmain:\n  '  read analog input:\n  adcin 0, adcVar\n  '  Calculate linear slope of reading (thanks, Acroname!):\n  distance = (2914 \/ (adcVar + 5)) - 1\n  ' send the distance out:\n  serout2 tx, inv9600, [\"Distance = \", 9, distance, 10, 13]\n  '  wait 10ms for ADC to reset before next reading:\n  pause 10                 \ngoto main\n\n\n'  Blink the reset LED:\nblink:\n  for i=0 to 3 \n   high resetLED\n   pause 200\n   low resetLED\n   pause 200\n  next\nreturn\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Arduino\/ATMega8 hardware connections: A0\/PC5: potentiometer on analog in 1 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 valueint distance = 0;\/\/function prototype:void blink(int howManyTimes);void setup(){ \/\/ start serial port at 9600 bps: beginSerial(9600); blink(3);}void loop(){ \/\/ read analog input: val = analogRead(0); \/\/ send analog value out: printString(&#8220;Analog Value =\\t&#8221;); \/\/ Calculate linear slope of reading (thanks, Acroname!): distance = (2914 \/ (val + 5)) &#8211; 1; printInteger(distance); printString(&#8220;\\n\\r&#8221;); \/\/ wait 10ms for ADC to reset before next reading: delay(10); }\/\/ Blink the reset LED:void blink(int howManyTimes) { int i; for (i=0; i&lt; howManyTimes; i++) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); }}Written for the BX-24: &#8216;GP2D120 sensor example &#8216;by Tom Igoe &#8216; Reads a changing voltage from a GP2D120 IR ranging sensor &#8216; on analog input 0 and sends the result out in ASCII-encoded &#8216; decimal numbers.  &#8216; Analog in is on pin 15 of the BX-24 &#8216; Distance ranging formula comes from Acroname, http:\/\/www.acroname.com:&#8217; http:\/\/www.acroname.com\/robotics\/info\/articles\/irlinear\/irlinear.html &#8216; created 6 Oct 2005&#8217; Updated 25 Oct. 2005 dim val as integer &#8216; outgoing ADC valuedim distance as integersub main () call blink(3)do&#8217; read analog input: val = getADC(15) &#8216; Calculate linear slope of reading (thanks, Acroname!): distance = (2914 \/ (val + 5)) &#8211; 1 &#8216; send analog value out: debug.print &#8220;Distance = &#8220;; cStr(distance) &#8216; wait 10ms for ADC to reset before next reading: call delay(0.01) loopend sub&#8217; Blink the reset LED:sub blink(byVal howManyTimes as integer) 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) nextend subWritten in PicBasic Pro, tested on a PIC18F252:&#8217; GP2D120 sensor example&#8217; by Tom Igoe &#8216; Reads a changing voltage from a GP2D120 IR ranging sensor&#8217; on analog input 0 and sends the result out in ASCII-encoded&#8217; decimal numbers.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11,10],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-BX-24","category-picbasic-pro"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}