Infrared Distance Sensor
The GP2Y0A02YKOF is a distance measuring sensor module from Sharp. It consists of three parts: a PSD (Position Sensitive Detector), an IRED (Infrared Emitting Diode), and a signal processing circuit. Using a triangulation method, the measurement accuracy of the sensor is not affected by the material of the detected object, ambient temperature, or measurement time. The sensor's output voltage corresponds to the detected distance. By measuring the voltage value, the distance to the detected object can be determined. Therefore, this sensor is suitable for applications such as distance measurement and obstacle avoidance.
Module Source
Purchase Link:
https://item.taobao.com/item.htm?spm=a230r.1.14.16.7def2810sScrt7&id=580318855469&ns=1&abbucket=12
Baidu Netdisk Download Link:
https://pan.baidu.com/s/11dDQHyYJfi0nNyC28vkpoA
Password:qvpm
Specifications
Operating Voltage: 3.3-5V
Operating Current: 33mA
Module Dimensions: 37 x 21.6mm
Output Method: Analog output
Reading Method: ADC
Pin Count: 3 Pin
Hardware Connection
The output of an infrared range sensor is non-linear. Each model has a different output curve. Therefore, it is a good idea to do a little calibration of the sensor you are using before actual use. Create a graph for each model of sensor to get real and effective measurement data in actual use. The following figure shows the output curve graph for models with a range of 20-150CM.
As seen in the image above, when the distance to the detected object is less than approximately 15cm, the output voltage drops sharply. This means that, according to the voltage readings, the object’s distance seems to be increasing. However, this is not the case. Imagine your robot is slowly approaching an obstacle, but suddenly the obstacle disappears. Generally, your control program would make the robot move at full speed, resulting in a "thud" sound. Of course, there is a solution to this problem. Here’s a simple trick: just adjust the sensor’s installation position so that its distance from the robot’s perimeter is greater than the minimum detection distance.
The output data line of the infrared ranging sensor is to determine the distance by the change of voltage, we can use the ADC function to obtain the voltage change of the sensor, and convert it to the actual distance can be. Voltage distance conversion formula see the official code library link: https://github.com/zoubworldArduino/ZSharpIR
Find our 20-150CM model sensors with conversion formulas below.
- The Vcc terminal of the sensor is connected to the 5V supply of the development board;
- The GND terminal is connected to the GND of the development board;
- Out (signal output) is connected to the A0 analog input pin of the development board.
Usage Method
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 8, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
int sensorPin = A0; // 定义传感器连接到A0引脚
int sensorValue = 0; // 用于存储传感器值的变量
void setup() {
Serial.begin(9600); // 开始串行通讯,设置波特率为9600
}
void loop() {
sensorValue = analogRead(sensorPin); // 读取传感器的模拟值
// 将模拟值转换为距离
float voltage = sensorValue * (5.0 / 1023.0); // 将模拟值转换为电压值
float distance = 60.374 * pow(voltage, -1.16); // 基于电压转换的距离公式
Serial.print("Distance: ");
Serial.print(distance); // 打印距离值
Serial.println(" cm");
delay(1000); // 延时1秒
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Usage Testing
Place an object at a distance of 20cm from the distance sensor, measure the distance using the sensor, and output the result via the serial port.