0
0
AutocadHow-ToBeginner · 4 min read

How to Use Sharp IR Distance Sensor with Arduino: Simple Guide

To use a Sharp IR distance sensor with Arduino, connect its analog output pin to an Arduino analog input pin and power it with 5V and GND. Then, read the sensor value using analogRead() and convert it to distance using the sensor's characteristic formula or lookup table.
📐

Syntax

The basic syntax to read the Sharp IR sensor value in Arduino is:

  • analogRead(pin): Reads the analog voltage from the sensor connected to the specified pin.
  • pinMode(pin, INPUT): Sets the sensor pin as input (optional for analog pins).
  • Use the raw analog value to calculate distance based on the sensor's datasheet.
arduino
const int sensorPin = A0;  // Analog pin connected to sensor output

void setup() {
  Serial.begin(9600);  // Start serial communication
}

void loop() {
  int sensorValue = analogRead(sensorPin);  // Read analog value
  Serial.println(sensorValue);  // Print raw sensor value
  delay(500);  // Wait half a second
}
💻

Example

This example reads the Sharp IR sensor analog value and converts it to approximate distance in centimeters using a simple formula. It prints both raw and distance values to the Serial Monitor.

arduino
const int sensorPin = A0;  // Sensor output connected to analog pin A0

void setup() {
  Serial.begin(9600);  // Initialize serial communication
}

void loop() {
  int sensorValue = analogRead(sensorPin);  // Read raw analog value

  // Convert raw value to voltage (assuming 5V Arduino)
  float voltage = sensorValue * (5.0 / 1023.0);

  // Approximate distance calculation (example for Sharp GP2Y0A21YK)
  // Distance in cm = 27.86 / (voltage - 0.42)
  float distance = 27.86 / (voltage - 0.42);

  Serial.print("Raw Value: ");
  Serial.print(sensorValue);
  Serial.print(" | Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);  // Wait 500ms before next reading
}
Output
Raw Value: 300 | Distance: 15.2 cm Raw Value: 310 | Distance: 14.6 cm Raw Value: 295 | Distance: 15.7 cm ...
⚠️

Common Pitfalls

  • Incorrect wiring: Make sure the sensor's power (Vcc) is connected to 5V, GND to ground, and output to an analog pin.
  • Ignoring sensor voltage range: The sensor output voltage is nonlinear; use the correct formula or calibration for accurate distance.
  • Using digital pins: The sensor outputs analog voltage, so connect to analog input pins only.
  • Not allowing sensor warm-up: The sensor may need a short warm-up time for stable readings.
arduino
/* Wrong way: Using digitalRead on analog output pin */
const int sensorPin = 2;  // Digital pin (wrong)

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  int val = digitalRead(sensorPin);  // Incorrect for analog sensor
  Serial.println(val);
  delay(500);
}

/* Right way: Use analogRead on analog pin */
const int correctSensorPin = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int val = analogRead(correctSensorPin);
  Serial.println(val);
  delay(500);
}
📊

Quick Reference

StepDescription
1Connect sensor Vcc to 5V and GND to ground on Arduino.
2Connect sensor output to an analog input pin (e.g., A0).
3Use analogRead(pin) to get sensor voltage value.
4Convert analog value to voltage: voltage = value * (5.0 / 1023.0).
5Calculate distance using sensor-specific formula or lookup.
6Print or use distance value in your program.

Key Takeaways

Connect the Sharp IR sensor output to an Arduino analog pin and power it with 5V and GND.
Use analogRead() to get the sensor's voltage reading, not digitalRead().
Convert the analog reading to voltage, then apply the sensor's formula to find distance.
Check the sensor datasheet for the correct distance conversion formula.
Allow the sensor to warm up for stable and accurate readings.