Search Code

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

Images

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.
Language: C++
/*
 * Sharp IR (infrared) distance measurement module for Arduino
 * Measures the distance in cm and displays it on LCD1602 (without the I2C module)

 * Original library: https://github.com/guillaume-rico/SharpIR
 
 * Watch video instructions for this code: https://youtu.be/NjUOEAoKY7A
 * 
 * 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/>.

*/



// original source https://www.arduino.cc/en/Tutorial/HelloWorld
// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

///////////////////***** start of Sharp IR
//Sharp IR library source: https://github.com/guillaume-rico/SharpIR
#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);
/////////////////////**** end of Sharp IR

void setup() {
  // Robojax LCD1602 Test 
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  // Robojax Sharp IR with LCD1602 Test 
}

void loop() {
   lcd.clear();
  // Robojax.com Sharp IR Test 
    lcd.setCursor(0, 0);
    lcd.print("Robojax IR Test");
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
 int dis=SharpIR.distance();// gets the distance in cm
 String distance = String(dis);
 distance ="Distance: "+distance+"cm";
 lcd.print(distance); 



  delay(500);
  // Robojax.com Sharp IR Test  
}

Resources & references

Files📁

Arduino Libraries (zip)

Fritzing File