SHARP – Blog eTechPath https://blog.etechpath.com Sat, 08 Apr 2023 19:45:45 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://blog.etechpath.com/wp-content/uploads/2017/08/BrandLogo12-150x150.png SHARP – Blog eTechPath https://blog.etechpath.com 32 32 Interfacing SHARP 2Y0A21 Distance Sensor with Arduino https://blog.etechpath.com/interfacing-sharp-2y0a21-distance-sensor-with-arduino/ https://blog.etechpath.com/interfacing-sharp-2y0a21-distance-sensor-with-arduino/#respond Tue, 21 Feb 2023 08:44:53 +0000 https://blog.etechpath.com/?p=1201 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");
}
]]>
https://blog.etechpath.com/interfacing-sharp-2y0a21-distance-sensor-with-arduino/feed/ 0