Arduino Code and Video for Sharp IR Distance Module with LCD1602 Display

Arduino Code and Video for Sharp IR Distance Module with LCD1602 Display

In this tutorial, we will learn how to use the Sharp infrared distance module in conjunction with an LCD1602 display to measure distances accurately. The Sharp IR sensor will output the measured distance, which will then be displayed on the LCD, allowing for real-time feedback. By the end of this project, you will have a working setup that can measure distances from 4 to 30 centimeters (in video at 00:30).

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

Sharpt IR distance sensor

The Sharp IR distance sensor operates by emitting infrared light and measuring the time it takes for the light to reflect back. This allows the sensor to calculate distance based on the amount of light received. The LCD1602 display is used to present this information in a readable format. The setup is compact, making it suitable for various applications, including robotics and distance measurement systems.

Hardware Explained

For this project, we will use the Sharp IR distance sensor and the LCD1602 display. The Sharp IR sensor has three wires: the red wire connects to 5V power, the black wire connects to ground, and the yellow wire is the output signal that connects to an analog pin on the Arduino. The LCD1602 display requires several pins for communication, which will also be connected to the Arduino.

The LCD1602 display uses a LiquidCrystal library to manage the data sent to it, allowing for easy control of the display's output. This library handles the communication between the Arduino and the LCD, making it simple to print text and numbers.

Datasheet Details

ManufacturerSharp
Part numberGP2Y0A41SK0F
Logic/IO voltage5 V
Supply voltage4.5 – 5.5 V
Operating distance range4 – 30 cm
Output voltage (at 30 cm)0.4 V
Output voltage (at 4 cm)3 V
Response time30 ms
PackageCompact module
Notes / variantsDifferent models available for varying ranges

  • Ensure the sensor is powered correctly at 5 V.
  • Use a common ground for all components.
  • Be cautious with the output voltage, which varies with distance.
  • Consider using a filter for the output signal to stabilize readings.
  • Keep the sensor clean and free from obstructions for accurate measurements.

Wiring Instructions

Arduino Wiring Diagram of Sharp distance sensor with LCD1602-2
Arduino Wiring Diagram of Sharp distance sensor with LCD1602-2

To wire the Sharp IR distance module, connect the red wire to the 5V pin on the Arduino. Next, connect the black wire to one of the ground (GND) pins on the Arduino. The yellow wire, which outputs the distance measurement, should be connected to the analog pin A0 on the Arduino. For the LCD1602 display, connect the pins as follows: rs to pin 12, en to pin 11, d4 to pin 5, d5 to pin 4, d6 to pin 3, and d7 to pin 2. Ensure all connections are secure to avoid any loose wiring issues (in video at 02:15).

Code Examples & Walkthrough

The Arduino code for this project initializes the LCD and the Sharp IR sensor. The key identifiers include IR, which is defined as the analog pin used for the Sharp IR output, and model, which specifies the type of Sharp sensor being used. In the setup function, the LCD is initialized to display content.

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

#define IR A0 // define Sharp IR signal pin
#define model 430 // the model of the IR module

This excerpt shows how the LiquidCrystal library is initialized and how the IR sensor pin and model are defined. The model is critical to ensure the library functions correctly with the specific type of sensor being used.

In the loop function, the distance is continuously measured and displayed on the LCD. The function SharpIR.distance() retrieves the distance in centimeters, which is then formatted into a string and printed to the LCD.

int dis = SharpIR.distance(); // gets the distance in cm
String distance = String(dis);
distance = "Distance: " + distance + "cm";
lcd.print(distance);

This code snippet retrieves the distance reading from the sensor and formats it for display. The continuous update of the display gives real-time feedback on the distance measured by the Sharp IR sensor.

Demonstration / What to Expect

Upon completing the wiring and uploading the code, you should see the LCD display the distance measured by the Sharp IR sensor. You can test the setup by placing an obstacle at various distances. The readings should be accurate within the specified range of 4 to 30 centimeters. Be aware that readings may become less reliable at the extremes of this range (in video at 04:50).

Video Timestamps

  • 00:00 Introduction
  • 02:15 Wiring instructions
  • 04:50 Demonstration of the setup

Immagini

Arduino Wiring Diagram of Sharp distance sensor with LCD1602-2
Arduino Wiring Diagram of Sharp distance sensor with LCD1602-2
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
Sharpt IR distance sensor
Sharpt IR distance sensor GP2Y0A21YK0F
71-This is the Arduino code and video for a Sharp Infrared Sensor Module with LCD1602.
Lingua: C++
Copia!

Risorse e riferimenti

File📁

Librerie Arduino (zip)

File Fritzing