Arduino Code and Video for Sharp IR Distance Module
In this tutorial, we will explore how to use the Sharp IR distance module to measure distances and trigger actions based on the detected distance. This project will demonstrate how to set up the hardware and write the Arduino code to read distance measurements and activate a buzzer when an object is detected within a certain range. You'll learn how to wire the components together and understand the key identifiers in the code.

For this project, we will be utilizing a Sharp IR sensor that emits infrared light and measures the distance to an object based on the reflected light. When an object is detected within the range, the buzzer will be triggered, providing a simple yet effective way to demonstrate the capabilities of the Sharp IR module. Be sure to check the video for a visual guide on the setup and code (in video at 00:00).
Hardware Explained
The main components of this project include a Sharp IR distance sensor, an Arduino board, and a buzzer. The Sharp IR sensor consists of a transmitter and a receiver that work together to measure distances. When the infrared light from the transmitter hits an object, it reflects back to the receiver, allowing it to calculate the distance based on the time taken for the light to return.
The Arduino acts as the controller, reading the distance measurements from the IR sensor and controlling the buzzer based on the readings. The buzzer will sound when an object is detected within a specified range, making this setup useful for applications such as obstacle detection in robotics.
Datasheet Details
| Manufacturer | Sharp |
|---|---|
| Part number | GP2Y0A21YK0F |
| Logic/IO voltage | 5 V |
| Supply voltage | 4.5 – 5.5 V |
| Output voltage | 0.4 – 2.5 V |
| Measuring range | 10 – 80 cm |
| Response time | 50 ms |
| Output current | 20 mA |
| Package | Module |
| Notes / variants | Available in multiple distance ranges |
- Ensure proper power supply (4.5 – 5.5 V) to avoid damage.
- Use a resistor to limit current if necessary.
- Keep the sensor clean for accurate readings.
- Be mindful of ambient light interference.
- Wiring must be secure to prevent intermittent connections.
Wiring Instructions

To wire the Sharp IR distance module, connect the ground pin of the module to the GND pin on the Arduino. Then, connect the VCC pin of the module to the 5V pin on the Arduino. The signal output pin from the Sharp IR module should be connected to pin 2 on the Arduino, which will read the distance data.
For the buzzer, connect the longer pin (positive) to pin 8 on the Arduino and the shorter pin (negative) to the GND. Make sure all connections are secure and that the components are powered correctly to ensure proper functionality.
Code Examples & Walkthrough
Below is a brief excerpt from the Arduino code where we define the pins and set up the sensor. The identifier IR is defined as the analog pin used for the sensor signal.
#define IR A0 // define signal pin
SharpIR SharpIR(IR, model); // Initialize SharpIR with the defined pin
This code initializes the Sharp IR sensor on the specified analog pin. The identifier model specifies the type of sensor being used, which in this case is set to 1080 for the GP2Y0A21YK0F model.
In the setup function, we start the serial communication to monitor the distance readings:
void setup() {
Serial.begin(9600); // Initialize serial communication
Serial.println("Robojax Sharp IR "); // Print message to serial monitor
}
The setup function initializes the serial communication at 9600 baud, allowing the Arduino to send distance data to the Serial Monitor for observation.
Within the loop function, we read the distance from the sensor and print it:
void loop() {
int dis = SharpIR.distance(); // Read the distance
Serial.print("Mean distance: "); // Print distance to serial monitor
Serial.println(dis); // Output the distance measurement
}
This excerpt shows how to continuously read the distance and print it to the Serial Monitor. The variable dis holds the distance value measured by the Sharp IR sensor.
Demonstration / What to Expect
When the setup is complete, you can expect the buzzer to sound whenever an object is detected within the specified range of the Sharp IR sensor. The Serial Monitor will display "Mean distance" along with the measured distance in centimeters. If there are no objects within range, the monitor will indicate that as well (in video at 02:30).
Common pitfalls include ensuring that the sensor is aligned properly and that there are no obstructions blocking the infrared signal. Also, check that the power supply is stable to avoid erratic readings.
Video Timestamps
- 00:00 - Introduction to the project
- 02:30 - Demonstration of the sensor functionality
- 03:45 - Wiring explanation
- 05:00 - Code walkthrough
/*
* Sharp IR (infrared) distance measurement module for Arduino
* Measures the distance in cm.
* Original library: https://github.com/guillaume-rico/SharpIR
* Watch video instruction for this code: https://youtu.be/GL8dkw1NbMc
*
* Full explanation of this code and wiring diagram is available at
* my Arduino Course at Udemy.com here: http://robojax.com/L/?id=62
* Written by Ahmad Shamshiri on Feb 03, 2018 at 07:34
* in Ajax, Ontario, Canada. www.robojax.com
*
* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place.
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
*/
// Sharp IR code for Robojax.com
#include <SharpIR.h>
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// 430 for GP2Y0A41SK
/*
2 to 15 cm GP2Y0A51SK0F use 1080
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series use 430
10 to 80 cm GP2Y0A21YK0F use 1080
10 to 150 cm GP2Y0A60SZLF use 10150
20 to 150 cm GP2Y0A02YK0F use 20150
100 to 550 cm GP2Y0A710K0F use 100550
*/
SharpIR SharpIR(IR, model);
void setup() {
// Sharp IR code for Robojax.com
Serial.begin(9600);
Serial.println("Robojax Sharp IR ");
}
void loop() {
// Sharp IR code for Robojax.com
delay(500);
unsigned long startTime=millis(); // takes the time before the loop on the library begins
int dis=SharpIR.distance(); // this returns the distance to the object you're measuring
// Sharp IR code for Robojax.com
Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);
//Serial.println(analogRead(A0));
unsigned long endTime=millis()-startTime; // the following gives you the time taken to get the measurement
Serial.print("Time taken (ms): ");
Serial.println(endTime);
// Sharp IR code for Robojax.com
}
|||您可能需要的东西
-
亚马逊从亚马逊购买Sharp IR距离传感器amzn.to
资源与参考
-
外部RoboJob.com 的 Sharp Libraryrobojax.com
-
外部Sharp IR GP2Y0A51SK0F datasheet (PDF)global.sharp
文件📁
Arduino 库(zip 格式)
-
Arduino library for sharp IR distance sensor
robojax-sharp_IR-master.zip
Fritzing 文件
-
sharp ir sensor GP2Y0A02YK0F
sharp ir sensor GP2Y0A02YK0F .fzz