How to use sharp IR sensor with arduino

How to use sharp IR sensor with arduino

Components you'll need:

  • Arduino uno
  • Sharp GP2D12 or any other analog model like GP2D120
  • Half sized breadboard

This is actually explained here but for ease of users i am posting it separately.


You can power the arduino through USB or 9V battery.


int sensorpin = 0;                 // analog pin used to connect the sharp sensor
int val = 0;                 // variable to store the values from sensor(initially zero)

void setup()
  Serial.begin(9600);               // starts the serial monitor
void loop()
  val = analogRead(sensorpin);       // reads the value of the sharp sensor
  Serial.println(val);            // prints the value of the sensor to the serial monitor
  delay(100);                    // wait for this much time before printing next value



mihai's picture

conversion (i know it's old)

According to this the calculation is Distance (cm) = 4800/(SensorValue - 20) and it's applicable only in between values 80-500. There is also a small graph there that you can translate in a mathematical formula

jgeidl's picture

conversion to inches or cm

Has anyone come up with a reliable conversion taking the voltages and converting to inches and/or cm?




Jim, K6JMG

Add a new comment

Login or register to post comments

New to community of robots ?