Arduino Code and Video for Sharp IR Distance Module with LCD1602 and I2C Module
This project guide will walk you through building a simple yet effective distance measurement system using a Sharp Infrared (IR) distance sensor and an LCD1602 display. This setup allows you to accurately measure distances within a specific range and display them directly on a screen, making it ideal for various DIY electronics applications.
Sharp Analog output modules:
2 to 15 cm GP2Y0A51SK0F
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series
10 to 80 cm GP2Y0A21YK0F
10 to 150 cm GP2Y0A60SZLF
20 to 150 cm GP2Y0A02YK0F
100 to 550 cm GP2Y0A710K0F

Practical usage examples and project ideas for this system include:
- Building an automated parking assist system to prevent bumper damage.
- Creating a simple water level sensor for tanks or wells.
- Developing a non-contact liquid dispenser.
- Implementing a basic security alarm by detecting proximity.
- Creating a fill-level indicator for hoppers or bins.
- Designing a collision avoidance system for small robots or RC vehicles.
Hardware/Components
To complete this project, you will need the following components:
- Arduino Uno or compatible board
- Sharp Infrared Distance Sensor (model GP2Y0A41SK0F, measuring 4 to 30 cm)
- LCD1602 with I2C module (for easy wiring)
- Breadboard (optional, for prototyping)
- Jumper wires
- USB cable for Arduino power and programming
Wiring Guide

Connecting the Sharp IR sensor and the LCD1602 with I2C module to your Arduino is straightforward, thanks to the I2C module reducing the number of wires needed for the LCD.
The Sharp IR sensor has three wires: Red (5V), Black (Ground), and Yellow (Output). Connect the Red wire to the Arduino's 5V pin, the Black wire to any GND pin on the Arduino, and the Yellow output wire to the Arduino's Analog Pin A0 (in video at 03:55).
The LCD1602 with I2C module has four wires: VCC, GND, SDA, and SCL. Connect the VCC pin to the Arduino's 5V, and the GND pin to the Arduino's GND. The SDA (Serial Data Line) wire should be connected to Arduino's Analog Pin A4, and the SCL (Serial Clock Line) wire should be connected to Arduino's Analog Pin A5 (in video at 02:42).
Code Explanation
The provided Arduino code utilizes two main libraries: Wire.h for I2C communication and LiquidCrystal_I2C.h for controlling the LCD, and SharpIR.h for the Sharp IR sensor. The user-configurable parts of the code are primarily related to sensor definition and LCD initialization.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
The line LiquidCrystal_I2C lcd(0x27, 16, 2); initializes the LCD object. The first parameter, 0x27 (in video at 03:26), is the default I2C address for most PCF8574-based I2C LCD modules. If your LCD doesn't work, you might need to find its correct address (common alternatives are 0x3F). The second parameter, 16, specifies that your LCD has 16 characters per line, and the third parameter, 2, indicates it has 2 lines.
#include <SharpIR.h>
#define IR A0 // define Sharp IR signal pin
#define model 430 // the model of the IR module
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:
/*
* GP2Y0A02YK0F --> "20150"
GP2Y0A21YK --> "1080"
GP2Y0A710K0F --> "100500"
GP2YA41SK0F --> "430"
*/
SharpIR SharpIR(IR, model);
These lines set up the Sharp IR sensor. #define IR A0 defines the Arduino analog pin where the Sharp IR sensor's output wire is connected. #define model 430 (in video at 04:27) is crucial as it specifies the exact model of your Sharp IR sensor. The library uses this model number to apply the correct mathematical conversion from the sensor's analog voltage output to a distance in centimeters. The comment block lists various Sharp IR models and their corresponding integer values that should be used here. For example, if you were using a GP2Y0A21YK, you would change 430 to 1080.
void setup()
{
lcd.begin();
lcd.backlight();
}
In the setup() function, lcd.begin() initializes the LCD, and lcd.backlight() turns on its backlight, making the display visible.
void loop()
{
lcd.clear();
lcd.print("Robojax IR Test");
lcd.setCursor (0,1); // go to start of 2nd line
int dis=SharpIR.distance();// gets the distance in cm
String distance = String(dis);
distance ="Distance: "+distance+"cm";
lcd.print(distance);
delay(500);
}
Inside the loop() function:
lcd.clear();clears any previous text on the display.lcd.print("Robojax IR Test");prints a static message on the first line.lcd.setCursor (0,1);moves the cursor to the start of the second line (0 is the first column, 1 is the second line, as lines are 0-indexed).int dis=SharpIR.distance();calls thedistance()function from the SharpIR library to read the sensor's value and convert it into an integer representing the distance in centimeters (in video at 05:06). This is the core function for getting the measurement.String distance = String(dis);converts the integer distance into a string, as you cannot directly concatenate integers with strings for printing.distance ="Distance: "+distance+"cm";formats the string to include "Distance: " before the value and "cm" after it.lcd.print(distance);displays the formatted distance string on the LCD.delay(500);introduces a 500-millisecond pause (in video at 06:00). This delay is important for the Sharp sensor's accuracy and stability. You can adjust this value: increasing it might improve accuracy but slow down updates, while decreasing it will speed up updates but might reduce accuracy.
Live Project/Demonstration
Once the components are wired and the code is uploaded to your Arduino, the system will start measuring and displaying distances. The project demonstrates accurate measurements at closer ranges, for example, showing 12 cm when the object is at 12 cm (in video at 00:31) and 10 cm for 10 cm (in video at 00:37).
However, as the object moves further away, especially beyond 15-20 cm, the accuracy may decrease. For instance, at 20 cm, the display might show 21 cm (in video at 07:06), and at 25 cm, it might read 28 or 29 cm (in video at 07:32). This behavior is characteristic of these infrared sensors. The Sharp IR sensor outputs an analog voltage that varies with distance: as distance increases, the output voltage decreases (in video at 07:56).
The relationship between distance and voltage output is non-linear, especially at the extremes of the sensor's range. This non-linearity is why the `SharpIR` library is useful, as it contains calibration data to convert these varying voltages into more accurate distance readings. For applications requiring higher precision at longer ranges, it might be necessary to choose a different Sharp IR model designed for those distances or implement custom calibration curves.
Chapters
- [00:06] Project Introduction and Overview
- [01:10] Sharp IR Sensor Details and Pinout
- [02:09] LCD I2C Module Wiring
- [02:58] Arduino Code Explanation
- [06:36] Distance Measurement Demonstration
- [07:47] Sensor Characteristics and Accuracy Limitations
- [08:48] Concluding Remarks and Resources
Things you might need
-
AmazonPurchase LCD1602-I2C from Amazonamzn.to
-
Amazon
Resources & references
No resources yet.
Files📁
Arduino Libraries (zip)
-
Arduino library for sharp IR distance sensor
application/zip
Fritzing File
-
LCD LCD1602-I2C module with 4 wires
application/zip0.01 MB -
sharp ir sensor GP2Y0A02YK0F
application/zip