Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
45 min
Share

Measure distance with sharp ir sensor with arduino

If you want to measure the distance between two points accurately with Arduino, you should use the sharp IR sensor, because of its high accuracy and small error rate in the measurement.

Project Video

Overview

In this tutorial, the Arduino will help you get accurate measurement of the distance, so that you can use it in your project.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
Sharp Infrared Proximity Distance Sensor
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item

Steps

Wiring it Up

GP2Y0A21YK0F sharp ir sensor must be connected to the Arduino board as shown below.

Connections from the GP2Y0A21 sharp ir sensor to arduino :

      • red wire  → Arduino 5V pin

      • black wire →  Arduino GND pin

      • yellow wire →  Arduino pin A0

Coding



 
/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial: put the link of the tutorial here

Tutorial: Measure distance with sharp ir sensor with arduino!

The function of this sketch is to obtain the distance from the sharp ir sensor and display it on your computer


Connections from the GP2Y0A21 sharp ir sensor to arduino :

• red wire  → Arduino 5V pin

• black wire →  Arduino GND pin

• yellow wire →  Arduino pin A0

*/

// Include the library:
#include "SharpIR.h"

// Define model and input pin:
#define IRPin A0
#define model 1080

// Create variable to store the distance:
int distance_cm;

/* Model :
 GP2Y0A02YK0F --> 20150
 GP2Y0A21YK0F --> 1080
 GP2Y0A710K0F --> 100500
 GP2YA41SK0F --> 430
*/

// Create a new instance of the SharpIR class:
SharpIR mySensor = SharpIR(IRPin, model);

void setup() {
 // Begin serial communication at a baudrate of 9600:
 Serial.begin(9600);
}

void loop() {
 // Get a distance measurement and store it as distance_cm:
 distance_cm = mySensor.distance();

 // Print the measured distance to the serial monitor:
 Serial.print("Mean distance: ");
 Serial.print(distance_cm);
 Serial.println(" cm");

 delay(1000);
}

   

Testing it Out

You should also make sure you have chosen the right baud rate (9600) as specified in the code. You can do this by clicking on the drop-down menu at the bottom right corner of the output window.
know when your plant needs watering

Now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.

know when your plant needs watering

Place an object at different distances in front of the sensor and you will see that it displays the distance accurately on the serial monitor.

Now, as shown in the image below, the serial monitor displays the distance.

know when your plant needs watering

Resources

No items found.