Ian St. Louis
Created November 25, 2015

Distance Measuring Sensor

Taking simple readings from a 2Y0A21 Sharp Infrared Proximity Sensor!

BeginnerShowcase (no instructions)36
Distance Measuring Sensor


Read more


Distance measuring sensor code

Using Arduino and a 2Y0A21 Sharp Infrared Proximity Sensor to gather sensor readings.
int sensorPin = 0; // intialize sensor on analog pin 0

void setup(){
  Serial.begin(9600); // open up serial port to display distance values

void loop(){
  // the sensor will change the voltage according to the distance the user is from it, so we want to read this voltage
  int val = analogRead(sensorPin); 
  // scaled down the value of the distance, to decrease the resolution of the data so it is easier to follow
  val = round(val/3.5); 

  // print this new value to the serial monitor

  // delay so the serial monitor displays values in a readable manner. Any faster than this makes the data much harder to read in sequence


Ian St. Louis

Ian St. Louis

12 projects • 4 followers