Interfacing SHARP 2Y0A21 Distance Sensor with Arduino

Introduction:

In this tutorial, I will explain how to interface SHARP 2Y0A21 infrared distance sensor with Arduino board and display the output in serial monitor.

SHARP 2Y0A21

SHARP 2Y0A21 sensor works on infrared technology, it consists of a transmitter and a receiver diode which calculates the distance between the sensor and the target by measuring time interval between transmitted and received wave.

SHARP infrared distance sensor
SHARP 2Y0A21 Sensor Specifications:
  1. Working voltage: 4.5 – 5.5 Vdc
  2. Distance measuring range: 10cm – 80cm (10cm dead band)
  3. Output type: Analog (voltage)
  4. Output: 0 to 3.1v (3.1v at 10cm to 0.3v at 80cm)
  5. Operating Temperature: -10 to +60 °C (Storage: -40 to +70 °C)

Circuit Diagram:

Code:

//SHARP 2Y0A21 IR Distance Sensor Test (10-80cm) 
// Author: Pranay Sawarkar
// www.eTechPath.com

#define sensor A2 

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

void loop() 
{
  float volts = analogRead(sensor)*0.0048828125;
  int distance = 30*pow(volts, -1.173);
  delay(1000); 
  
  if ((distance >=10)&&(distance <=80))
  {
    Serial.print("Distance:");
    Serial.println(distance);
    Serial.print("Volts:");
    Serial.println(volts);   
  }
  else
  Serial.println("Out of range");
}

Leave a Reply

Your email address will not be published. Required fields are marked *