Lesson 74: Using a Sharp Infrared Sensor Module with Arduino
Introduction
This guide demonstrates how to use two or more Sharp infrared (IR) distance sensors with an Arduino to measure distance and display the results on the Serial Monitor. While a single sensor is useful, many real-world applications, such as a smart car, a robotic arm, or any moving machine, require distance measurements from multiple directions simultaneously. For example, you could install one sensor at the front to measure distance ahead, and another on the right or left side to avoid collisions. This tutorial shows you how to configure your Arduino to handle multiple sensors efficiently, building on a separate, detailed video that explains the individual sensor's specifications and operation.
Here are a few practical project ideas to inspire you:
- Building a robot that avoids obstacles by measuring distances on its left, right, and front sides.
- Creating a smart parking assistant that uses multiple sensors to guide a vehicle into a tight spot.
- Developing a security system that monitors a doorway or corridor with sensors placed at different angles.
- Making an interactive art installation where the distance of a viewer's hand changes what is displayed.

In this video, the focus is on the wiring and code needed to run multiple Sharp IR sensors. The demonstration uses two GP2Y0A21YK0F sensors, which measure distances from 10 to 80 cm. The code is structured to be easily expandable, so you can add a third, fourth, or even more sensors with just a few lines of code.
Hardware and Components
To follow along with this project, you will need the following components:
- An Arduino board (e.g., Uno, Mega).
- Two or more Sharp IR distance sensors (e.g., GP2Y0A21YK0F).
- Jumper wires.
- A breadboard (optional, for easier connections).
Wiring Guide
The Sharp IR sensor has three wires: red (power), black (ground), and yellow (signal output). The wiring is straightforward. For the first sensor, connect its red wire to the Arduino's 5V pin, the black wire to a ground (GND) pin, and the yellow signal wire to an analog pin, for example, A1. For the second sensor, connect its red wire to the same or another 5V pin, the black wire to GND, and the yellow wire to a different analog pin, such as A2.
If you run out of 5V pins, the video provides a clever solution. You can use any digital pin as a power source. In the code, digital pin 2 is configured as an output and set to HIGH, which provides a constant 5V. You would then connect the red wires of your sensors to this digital pin instead of the dedicated 5V pin. The ground wires can be connected to any of the GND pins on the Arduino. The video shows a demonstration of this setup at [00:06:10].
For a visual reference of the connections, please refer to the wiring diagram below:



Code Explanation
This section focuses on the user-configurable parts of the code, which are all located at the top of the sketch. This is where you will define your pins and sensor models.
First, the code includes the necessary library for the Sharp IR sensors, #include <SharpIR.h>. This library handles the complex math required to convert the analog voltage from the sensor into a distance reading.
Next, you will define the analog pins for each of your sensors. The example code uses #define IR1 A1 and #define IR2 A2. If you are using more than two sensors, you would add more definitions here, like #define IR3 A3.
A crucial part of the configuration is setting the correct model for your sensor. The code uses #define model1 1080 and #define model2 1080. This number is not arbitrary; it tells the library which sensor you have, as each model has a different voltage-to-distance curve. The code comments provide a helpful list:
- 2 to 15 cm GP2Y0A51SK0F use 215
- 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
You must change this value to match your specific sensor. For instance, if you were using a GP2Y0A02YK0F, you would change the definition to #define model1 20150. The code then creates an object for each sensor, like SharpIR SharpIR1(IR1, model1);. This line ties the pin and model together for the first sensor. If you add more sensors, you would copy this line and change the object name, pin, and model accordingly.
In the setup() function, the Serial Monitor is initialized at 9600 baud. This is the speed at which the Arduino communicates with your computer. The code also includes a section to use a digital pin as a 5V source if needed, as explained in the wiring section. This is done with pinMode(2,OUTPUT); and digitalWrite(2, HIGH);.
Finally, the loop() function contains the main logic. There is a delay(500); at the start, which sets the time between each set of readings to 500 milliseconds. You can adjust this to make the readings more or less frequent. The core of the loop is where you read the distance from each sensor using the distance() function of the library, like int dis1=SharpIR1.distance();. The results are then printed to the Serial Monitor with descriptive text, such as Serial.print("Distance (1): ");, followed by the value and the unit "cm".
Live Project Demonstration
In the video, the presenter demonstrates the setup with two sensors. As you can see in the demonstration at [00:07:44], the Serial Monitor displays the distance readings from both sensors. When an object is placed in front of the first sensor, the reading for "Distance (1)" changes. Similarly, when an object is placed in front of the second sensor, the reading for "Distance (2)" updates. The video shows that by holding a sheet of paper at a distance, the sensors read approximately 23 and 24 cm. When one sensor is blocked, its reading drops to about 17 cm, confirming that each sensor operates independently and provides real-time distance data for its respective direction. This validates the code's ability to handle multiple sensors simultaneously.
Chapters
- [00:00] Introduction to Using Multiple Sharp IR Sensors
- [00:28] Applications for Multiple Distance Sensors
- [01:34] Code Explanation: Library and Pin Definitions
- [02:24] Configuring Sensor Models and Creating Objects
- [04:15] Setup and Loop Functions Explained
- [06:10] Wiring Guide for Multiple Sensors
- [07:44] Live Demonstration with Two Sensors
++
/*
* Using two or more Sharp IR distance sensors with Arduino
* Measure distance
* Download and resource page for this video https://robojax.com/RJT143
* Watch video instructions for this video: https://youtu.be/WQ3oFyF5qeY
*
* Watch main video explaining the Sharp IR distance sensor:
* https://robojax.com?vid=robojax-sharp_IR
*
* Written by Ahmad Shamshiri for Robojax.com on Saturday, December 1, 2018
* at 20:56 in Ajax, Ontario, Canada
*
* 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/>.
*/
#include <SharpIR.h>
#define IR1 A1 // define signal pin
#define IR2 A2 // define signal pin
#define model1 1080 // for GP2Y0A21YK0F used 1080
#define model2 1080 // for GP2Y0A21YK0F used 1080
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
/*
2 to 15 cm GP2Y0A51SK0F use 215
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 SharpIR1(IR1, model1);
SharpIR SharpIR2(IR2, model2);
void setup() {
// Multiple Sharp IR Distance meter code for Robojax.com
Serial.begin(9600);
Serial.println("Robojax Sharp IR ");
// extra pin for 5V if needed
pinMode(2,OUTPUT);// define pin 2 as output
digitalWrite(2, HIGH);// set pin 2 HIGH so it always have 5V
// Robojax.com 20181201
}
void loop() {
// Sharp IR code for Robojax.com 20181201
delay(500);
unsigned long startTime=millis(); // takes the time before the loop on the library begins
int dis1=SharpIR1.distance(); // this returns the distance for sensor 1
int dis2=SharpIR2.distance(); // this returns the distance for sensor 2
// Sharp IR code for Robojax.com 20181201
Serial.print("Distance (1): ");
Serial.print(dis1);
Serial.println("cm");
Serial.print("Distance (2): ");
Serial.print(dis2);
Serial.println("cm");
// Sharp IR code for Robojax.com
}
++
/*
* Lesson 74: Using two or more Sharp IR distance sensors
* Sharp IR (infrared) distance measurement module for Arduino
* Measures the distance in cm.
* Written by Ahmad Shamshiri for Robojax.com
* on February 25, 2019 at 21:38 in Ajax, Ontario, Canada
* Original library: https://github.com/guillaume-rico/SharpIR
* Watch video instructions for this code: https://youtu.be/WQ3oFyF5qeY
*
* 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 February 3, 2018 at 07:34
* in Ajax, Ontario, Canada. www.robojax.com
*
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/>.
*/
*/
// Sharp IR code for Robojax.com
#include
#define IR A0 // define signal pin
#define model 1080 // used 1080 because model GP2Y0A21YK0F is used
// Sharp IR code for Robojax.com
// 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
*/
#include <SharpIR.h>
#define IR1 A1 // define signal pin
#define IR2 A2 // define signal pin
#define model1 1080 // for GP2Y0A21YK0F used 1080
#define model2 1080 // for GP2Y0A21YK0F used 1080
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
/*
2 to 15 cm GP2Y0A51SK0F use 215
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 SharpIR1(IR1, model1);
SharpIR SharpIR2(IR2, model2);
void setup() {
// Multiple Sharp IR Distance meter code for Robojax.com
Serial.begin(9600);
Serial.println("Robojax Sharp IR ");
// extra pin for 5V if needed
pinMode(2,OUTPUT);// define pin 2 as output
digitalWrite(2, HIGH);// set pin 2 HIGH so it always have 5V
// Robojax.com 20181201
}
void loop() {
// Sharp IR code for Robojax.com 20181201
delay(500);
unsigned long startTime=millis(); // takes the time before the loop on the library begins
int dis1=SharpIR1.distance(); // this returns the distance for sensor 1
int dis2=SharpIR2.distance(); // this returns the distance for sensor 2
// Sharp IR code for Robojax.com 20181201
Serial.print("Distance (1): ");
Serial.print(dis1);
Serial.println("cm");
Serial.print("Distance (2): ");
Serial.print(dis2);
Serial.println("cm");
// Sharp IR code for Robojax.com
}
Resources & references
-
ExternalDistance-measuring sensors (website)sharp-world.com
-
ExternalSharp IR Library (from GitHub)github.com
-
ExternalSharp Library from RoboJob.comrobojax.com
Files📁
Fritzing File
-
sharp ir sensor GP2Y0A02YK0F
sharp ir sensor GP2Y0A02YK0F .fzz
Other files
-
Sharp GP2Y0A41SK0F Infrared distance sensor datasheet
robojax_sharp_IR_GP2Y0A41SK0F_datasheet.pdf0.84 MB -
Sharp GP2Y0A710K0F Infrared distance sensor datashee
robojax_sharp_IR_GP2Y0A710K0F_datasheet.pdf0.33 MB -
Arduino library for Sharp Infrard distance sensor
robojax-sharp_IR-master.zip