GP2Y0A41SK0F Arduino Tutorial: Measure Distance with Serial Monitor and LCD1602 I2C
Interfacing a distance sensor with Arduino is always a fun and useful project. In this tutorial, we will learn how to use the GP2Y0A41SK0F infrared distance sensor with Arduino, step by step. This sensor is popular because it gives fast and stable distance readings. It works on an analog output, so the Arduino can read the values easily.
We will first read the raw analog values on the Serial Monitor. After that, we will calculate the real distance in centimetres. Once the readings are correct, we will display the distance on the LCD1602 I2C display. If you are new to I2C LCDs, you can also check my detailed guide: I2C LCD1602 Arduino Tutorial.

What is GP2Y0A41SK0F and How It Works
The GP2Y0A41SK0F is a popular short-range IR distance sensor. It is easy to use, accurate, and works smoothly with Arduino. Before wiring and coding, let’s understand its basics and how it measures distance.

Overview of the GP2Y0A41SK0F Sensor Module
The GP2Y0A41SK0F is an infrared proximity sensor made by Sharp. It detects objects between 4 cm and 30 cm. This makes it ideal for short-range distance sensing.
Key points about the sensor:
- It uses infrared light to measure distance.
- Output is analog voltage, so Arduino can read it with an analog pin.
- It is more stable than simple IR obstacle sensors.
- It works well even with objects of different colors.
The module has three pins: VCC, GND, and OUT. You can power it using 5V from the Arduino.
Working Principle (Infrared Triangulation Method)
The sensor uses infrared triangulation, which is more accurate than basic reflection-based IR sensors.
Here’s how it works:
- The IR LED inside the sensor emits a beam of infrared light.
- The light hits the object and reflects back.
- A position-sensitive detector (PSD) inside the sensor receives the reflected beam.
- The angle of reflection changes with distance.
- The sensor converts this angle into a voltage value.
This method makes the sensor very stable. It is less affected by ambient light and object color. That is why it is widely used in robotics and automation.
Output Voltage vs Distance Characteristics
The key thing to understand about this sensor is that the output voltage is not linear.
- When an object is closer, the output voltage is higher.
- When the object moves farther, the voltage drops.
But the change is not uniform. The sensor has a curved response graph, meaning distance and voltage do not increase or decrease in a straight line.
Typical readings:
| Distance | Approx. Output Voltage |
|---|---|
| 4 cm | ~3.0V |
| 10 cm | ~1.5V |
| 20 cm | ~0.9V |
| 30 cm | ~0.4V |
Because of this non-linear behavior, we must apply a conversion formula later in the tutorial to convert ADC values into accurate distance values.
Why Choose GP2Y0A41SK0F for Arduino Projects
This sensor is a favorite among beginners and makers. Here are the reasons:
- Stable readings compared to ultrasonic sensors in short range
- Fast response time, useful in moving robots
- Simple to wire using just one analog pin
- No calibration required for basic use
- Works well in low light and daylight
Wiring GP2Y0A41SK0F with Arduino
Before coding, we must connect the sensor to the Arduino correctly. The GP2Y0A41SK0F is simple to wire because it uses only three pins. In this section, we also add the LCD1602 I2C module, which will display the distance values later in the tutorial.
Pinout of GP2Y0A41SK0F Sensor
The sensor module comes with three pins, and each pin has a clear function:
- VCC: Power input (4.5V–5.5V)
- GND: Ground
- OUT: Analog output voltage
The OUT pin gives a changing voltage based on how far the object is. We will read this voltage using one of the Arduino’s analog pins.
Connection Diagram with Arduino UNO
The wiring to Arduino UNO is very simple:
| Sensor Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT | A0 |
This is all you need to start reading distance values. Make sure the ground connections are common for stable output.
Reading Raw Analog Values from GP2Y0A41SK0F
We start with basic readings from the analog pin and print them on the Serial Monitor. This helps us understand how the sensor responds before converting the values into distance. The raw ADC value will change as the object moves closer or farther from the sensor.
Arduino Code to Read Raw ADC Data
int sensorPin = A0; // GP2Y0A41SK0F output connected to A0
int rawValue = 0; // variable to store ADC value
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
rawValue = analogRead(sensorPin); // Read the raw analog value
Serial.print("Raw ADC Value: ");
Serial.println(rawValue); // Print the value on Serial Monitor
delay(200); // Small delay for readability
}Explanation:
- We first set A0 as the input pin where the sensor output is connected.
- In
setup(), we start the Serial Monitor at 9600 baud rate. - Inside the
loop(), we useanalogRead()to read the sensor’s output voltage. - The raw ADC value will be between 0 and 1023 because Arduino uses a 10-bit ADC.
- We print this raw value on the Serial Monitor.
- A short delay is added so the readings are easy to view.
This simple test ensures that our wiring is correct and the sensor is responding properly.
Output
The video below shows the output of the code above. The raw values are printed on the serial monitor of the Arduino IDE.
Converting Raw Values to Distance (cm)
After reading the raw values, we convert them into real distance in centimetres. The GP2Y0A41SK0F sensor has a non-linear output curve, so we use an approximate formula to map the raw ADC values to distance.
Arduino Code for Distance Calculation
int sensorPin = A0;
int rawValue = 0;
float distanceCM = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
rawValue = analogRead(sensorPin); // Read raw ADC value
// Convert ADC reading to voltage
float voltage = rawValue * (5.0 / 1023.0);
// Convert voltage to distance (approx formula)
distanceCM = 12.08 * pow(voltage, -1.058);
Serial.print("Distance: ");
Serial.print(distanceCM, 1); // Print distance with 1 decimal
Serial.println(" cm");
delay(200);
}Explanation:
- First, we read the raw ADC value using
analogRead(). - The value is converted to voltage because formulas for this sensor work in volts, not raw numbers.
- The approximate equation is applied to compute the distance in cm.
- We print the distance on the Serial Monitor.
- A short delay allows stable reading.
Formula Used for Distance Conversion
The sensor output follows a non-linear voltage curve, so we use an approximate equation widely used for this sensor:
distance (cm) = 12.08 * (voltage ^ -1.058)What this means:
- When voltage is high, the distance becomes small (object is near).
- When voltage is low, the distance becomes large (object is far).
- The negative power creates the non-linear behaviour needed for this sensor.
This formula gives good accuracy for 4 cm to 30 cm, which is the sensor’s range.
Output
The video below shows the output of the code above. The distance values are printed on the serial monitor of the Arduino IDE.
Displaying Distance on LCD1602 I2C
Once the serial output works, we show the same distance reading on the I2C LCD1602. This makes the project complete and visually clear.
Wiring GP2Y0A41SK0F Sensor + I2C LCD1602 with Arduino
The image below shows the wiring connection between GP2Y0A41SK0F Sensor, LCD1602 and Arduino Uno.
Here is the complete wiring table for both, the sensor and the LCD:
| GP2Y0A41SK0F Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT | D0 |
| LCD Pin | Arduino Pin |
|---|---|
| GND | GND |
| VCC | 5V |
| SDA | A4 (for Arduino UNO) |
| SCL | A5 (for Arduino UNO) |
Both modules work on 5V, so wiring is very simple.
Installing the LiquidCrystal_I2C Library
Before writing the code, install the LiquidCrystal_I2C library:
- Open Arduino IDE.
- Go to Sketch → Include Library → Manage Libraries.
- Search for “LiquidCrystal_I2C”.
- Install the version by Frank de Brabander or Marco Schwartz.
Arduino Code to Display Distance on LCD1602
#include <LiquidCrystal_I2C.h>
int sensorPin = A0;
float distanceCM = 0;
// Create LCD object: address 0x27, 16 columns, 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on backlight
}
void loop() {
int rawValue = analogRead(sensorPin);
float voltage = rawValue * (5.0 / 1023.0);
distanceCM = 12.08 * pow(voltage, -1.058);
// Print on Serial (optional)
Serial.print("Distance: ");
Serial.print(distanceCM, 1);
Serial.println(" cm");
// Show on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(0, 1);
lcd.print(distanceCM, 1);
lcd.print(" cm");
delay(400);
}Explanation of the LCD Code
- We include the LiquidCrystal_I2C library to control the LCD easily.
- An LCD object is created with address 0x27.
- In
setup(), the LCD is initialized and the backlight is turned on. - Inside the loop:
- We read the sensor value.
- Convert it to voltage and then to distance.
- Print the distance on the Serial Monitor (optional).
- Clear the LCD and print the updated distance on both rows.
- The delay gives enough time for the screen to refresh smoothly.
Output
The video below shows the output of the code above. The distance values are printed on the serial monitor of the Arduino IDE.
Smoothing Sensor Output (Optional but Important)
The GP2Y0A41SK0F can produce noisy or jumpy readings, especially when objects move fast. To get stable results, we can smooth the sensor output using a simple moving average filter. This makes the distance readings steady and easier to read on the LCD.
Simple Moving Average Filter Code
#include <LiquidCrystal_I2C.h>
int sensorPin = A0;
float distanceCM = 0;
const int sampleCount = 10; // Number of samples for smoothing
int samples[sampleCount]; // Array to store sensor values
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
// Initialize samples to zero
for (int i = 0; i < sampleCount; i++) {
samples[i] = 0;
}
}
int getSmoothedValue() {
// Shift samples to left
for (int i = 0; i < sampleCount - 1; i++) {
samples[i] = samples[i + 1];
}
// Add new reading
samples[sampleCount - 1] = analogRead(sensorPin);
// Calculate average
long sum = 0;
for (int i = 0; i < sampleCount; i++) {
sum += samples[i];
}
return sum / sampleCount;
}
void loop() {
int rawValue = getSmoothedValue(); // Get filtered reading
float voltage = rawValue * (5.0 / 1023.0);
distanceCM = 12.08 * pow(voltage, -1.058);
// Serial output
Serial.print("Smooth Distance: ");
Serial.print(distanceCM, 1);
Serial.println(" cm");
// LCD output
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smooth Dist:");
lcd.setCursor(0, 1);
lcd.print(distanceCM, 1);
lcd.print(" cm");
delay(300);
}Explanation of the Filter
- We store multiple sensor readings inside an array.
- Every time the loop runs:
- Old readings move left.
- The newest reading is added at the end.
- Then, we take the average of all stored values.
- This smooths out sudden spikes or dips.
- The smoothed reading is converted to distance just like before.
Applications of GP2Y0A41SK0F with Arduino
This section lists real-world uses of the GP2Y0A41SK0F sensor. Because it gives fast and reliable short-range distance readings, it fits perfectly in many practical Arduino projects.
Robot Obstacle Detection
Robots often need to detect objects in front of them to avoid collisions. The GP2Y0A41SK0F offers quick distance feedback, which helps a robot stop, slow down, or turn before hitting an obstacle. It works well even in low-light conditions, making it more reliable than simple light-based sensors.
Automatic Hand Sanitizer / Soap Dispenser
Touchless dispensers use a short-range distance sensor to detect a hand. The GP2Y0A41SK0F detects objects within a few centimeters, which is ideal for dispensing sanitizer or soap automatically. This creates a clean and hygienic user experience.
Smart Trash Bins
Smart bins open their lids automatically when a hand approaches. The sensor provides fast detection at close range, so the bin responds immediately. It is also stable under different lighting conditions, making it suitable for home and office automation.
Industrial Distance Monitoring
Industries use short-range distance sensors to measure part placement, machine alignment, and object detection on conveyor belts. The GP2Y0A41SK0F provides accurate readings for these controlled environments, making it a good choice for automation and quality-checking systems.
Conclusion
The GP2Y0A41SK0F distance sensor is a compact and highly reliable component for short-range distance measurement. When paired with Arduino, it becomes an excellent choice for projects that need quick, stable, and consistent readings. In this tutorial, we explored everything from basic wiring and serial testing to LCD output, filtering, and real-world applications.
By combining these features, you now have a solid foundation to build automation systems, smart gadgets, and robotics projects that depend on accurate distance detection. This sensor is easy to integrate, beginner-friendly, and flexible enough to scale into more advanced designs. Whether you’re experimenting, prototyping, or creating a final product, this sensor-Arduino setup offers dependable performance and plenty of creative possibilities.
Browse More Arduino Sensors Tutorials
Interface AHT20 Sensor with Arduino | Measure Temperature and Humidity with OLED Display
Arduino DS18B20 Temperature Sensor Tutorial: Single and Multiple Sensors with SSD1306 OLED
Interface SHT21 Temperature and Humidity Sensor with Arduino | Display on Serial Monitor and I2C LCD
DS1307 Arduino Tutorial: Interfacing RTC Module with LCD1602 I2C Display
ADXL345 Arduino I2C Tutorial: Pinout, Wiring, Calibration, and Accelerometer Data Reading Explained
IR Sensor Arduino Tutorial: Interfacing, Calibration, Detection Modes, Codes & LCD1602 Display
Arduino BME280 Tutorial: Wiring, Pinout, Code and LCD1602 Display Output
Arduino GP2Y0A41SK0F Project Download
Info
You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.
Arduino GP2Y0A41SK0F Sensor FAQs
Because the sensor is analog, small fluctuations are normal. Using a simple moving average filter can stabilize the readings.
It works best with surfaces that reflect IR well. Very dark or thin objects may reduce accuracy but usually still get detected.
Although rated for 4–30 cm, the most stable readings are usually between 5–25 cm.
Yes, as long as each sensor uses a different analog input pin.
It can work outdoors but direct sunlight may interfere with IR readings. Using a small shade or indoor placement improves performance.





