Back to Arduino Step By Step Course

Lesson 73: Distance measurement using Sharp Infrared Sensor with Arduino

If you don't like to see ads while video is being played you can purchase this course for $200 from Udemy or purchase YouTube premium Here

  • Sharp Library from
  • Sharp Library from GitHub
  • Part 7: Obstacle Avoidance with Arduino

    This video shows you how to measure the distance between 4cm to 180cm depending on the module, using Arduino. Sharp Analog output modules:

    Timign in the video
      * Lesson 73: Sharp IR distance measurement
     * Sharp IR (infrared) distance measurement module for Arduino
     * Measures the distance in cm. 
     * written by Ahmad Shamshiri for 
     * on Feb 25, 2019 at 21:38 in Ajax, Ontario, Canada
     * Original library:
     * Watch Video instrution for this code:
     * Full explanation of this code and wiring diagram is available at
     * my Arduino Course at here:
     * Written by Ahmad Shamshiri on Feb 03, 2018 at 07:34
     * in Ajax, Ontario, Canada.
    If you found this tutorial helpful, please support me so I can continue creating 
    content like this. You can support me on Patreon
    or make donation using PayPal
     *  * 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 download from
        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
        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 <>.
      // Sharp IR code for
    #define IR A0 // define signal pin
    #define model 1080 // used 1080 because model GP2Y0A21YK0F is used
    // Sharp IR code for
    // ir: the pin where your sensor is attached
    // model: an int that determines your sensor:  1080 for GP2Y0A21Y
    //                                            20150 for GP2Y0A02Y
    //                                            430 for GP2Y0A41SK   
    2 to 15 cm GP2Y0A51SK0F	use 1080
    4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series	use 430
    10 to 80 cm GP2Y0A21YK0F	use 1080
    10 to 150 cm GP2Y0A60SZLF	use 10150
    20 to 150 cm GP2Y0A02YK0F	use 20150
    100 to 550 cm GP2Y0A710K0F	use 100550
    SharpIR SharpIR(IR, model);
    void setup() {
        // Sharp IR code for
     Serial.println("Robojax Sharp IR  ");
    void loop() {
        // Sharp IR code for
      unsigned long startTime=millis();  // takes the time before the loop on the library begins
      int dis=SharpIR.distance();  // this returns the distance to the object you're measuring
      // Sharp IR code for
      Serial.print("Mean distance: ");  // returns it to the serial monitor
      unsigned long endTime=millis()-startTime;  // the following gives you the time taken to get the measurement
     Serial.print("Time taken (ms): ");
         // Sharp IR code for

    The least I expect from you is to thumb up the video and subscribe to my channel. I appriciate that. .I have spent months making these lectures and writing code. You don't lose anything by subscribging to my channel. Your subscription is stamp of approval to my videos and more people can find them and in it turn it helps me. Thank you

    If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

    **** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.