Distance sensors are widely used in applications that require distance measurement. It is also used in robotics application to detect and measure distance with wall or obstacles. So we are going to look on Analog Distance Sensor SN-GP2Y0A21. As it’s name sounds, it gives out analog readings with changes in distance.

Image
SN-GP2Y0A21

Some features of SN-GP2Y0A21:

– Analog output

– Detecting distance: 10cm to 80cm

– Operating supply voltage: +4.5V to +5.5V

Image
Pin connections for SN-GP2Y0A21
dist7
Schematic Diagram

To use the sensor, we need to look in details of the output. The output voltage is not linear to the distance. There is an analog output voltage vs distance plot in the datasheet.

Image
Analog Output Voltage vs Distance

An example code of using this sensor on Arduino:


#include <LiquidCrystal.h>
#define sensor A1
float volt;

LiquidCrystal lcd(5,6,7,8,9,10);

void setup()
{
 lcd.begin(16,2);
 pinMode(sensor,INPUT);
}

void loop()
{
 float reading=analogRead(sensor);
 volt=(reading/1023)*5;
 lcd.setCursor(0,0);
 lcd.print("ADC :");
 lcd.setCursor(9,0);
 lcd.print((int)reading);
 lcd.setCursor(0,1);
 lcd.print("Voltage:");
 lcd.setCursor(9,1);
 lcd.print(volt);
 lcd.setCursor(13,1);
 lcd.print("V");
 delay(500);
}

A view of the circuit:

Image

Image

Video on Analog Distance Sensor SN-GP2Y0A21:

 

If you have any questions, feel free to comment.

Advertisements