Project 006: Arduino Infrared Proximity Sensor Project

Project 006: Arduino Infrared Proximity Sensor Project © GPL3+

A very basic project composing of a Sharp infrared proximity sensor hooked up to an Arduino with a few lines of code; for a perfect newbie.

  • 4 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
You could use any other Arduino board as well. The Keyestudio Uno is used in this example.
SparkFun Sharp Infrared Proximity Sensor (2Y0A21)
11026 02
Jumper wires (generic)
3 Male to Male Jumper Wires.
Adafruit industries ada62 image 75px
USB-A to B Cable
Depends on the Arduino.
Pcbway qeheggiidg
PCBWay Custom PCB

Apps and online services

About this project

Before starting the project...

Credits go to Lucky Larryon

This project's hardware and software side is by sure, the most simplest and straight-forward. There are only three connection wires to the Arduino and 13 lines of code. The Sharp Infrared Range Sensor (2Y0A21) works for long distances, up to 80cm and the sensor has a minimum measurement of 10cm as the sensor won't be accurate because of its suited environment conditions its made for. Applications for this project would be for: reverse sensors, robots, touch-less switches, etc.. Four components will be needed including:

  • 1 Arduino (the Keyestudio Uno is used in this example)​
  • 1 Sharp IR Range Sensor (2Y0A21)​
  • 3 Jumper Wires (Male to Male)​
  • 1 USB Data Cable (depends on the Arduino)

Mounting the circuit

This circuit consists of three connections. Before starting, turn of all power from your Arduino board to prevent any electrical hazards. Now let's get onto the wiring of this proximity sensor. First, connect the positive pin (red wire) of the sensor to 5v (+5 volts)​ ​on the Arduino. Second, connect the ground pin (black wire) of your sensor toGND (-)​ on your Arduino. Lastly, connect the data pin of your sensor to digital pin 1 on your Arduino. The hardware is now complete.

About the code

This code may seem short to write, but has a complicated explanation. It starts with defining the pin number (digital pin 1) of the variable; IRpin. After that, the next line immediately is the void setup​ of the code where we begin serial communication in the baud rate of 9600. After we end the void setup​, we move onto the starting of the ​void loop. ​Then, in the loop section, we use a data type (float) in the first and second line to calculate measurements we need. First, we calculate the volts, which is a variable under float, where we read the data from the IRpin (which we declared) and multiply the readings given from the sensor by 0.0048828125 to produce the volts​. The second line declares another float variable named distance which combined the volts ​with some mathematical terms. It also uses the ​pow function which calculates the value of a number raised to a power. The pow ​function uses the rule of pow(base, exponent) where the variable ​volts is the base and -1.10 is the exponent. On top of that we multiply the result with 65 to create the float variable, distance.​​ Next, we print the distance we calculated to the serial monitor and add "cm" to the end to identify the unit of measurement. Lastly, we wait 100 milliseconds and start again giving us data every 100 milliseconds.

Amazing opportunities

Also, be sure to check out PCBWay, a leading manufacturer and distributor in PCB design and manufacturing. They have amazing prices and excellent quality in their services, so don't miss out on them! Plus, PCBWay has an amazing website, online Gerber viewer function and a gift shop so make sure to check out their links below:

PCBWay Free Online Gerber Viewer Function:

PCBWay Gift Shop:

​Enjoy! Contact us for any inquiries!


Arduino Sharp Infrared Proximity Sensor Project CodeC/C++
int IRpin = 1;                                    
void setup() {
void loop() {
  float volts = analogRead(IRpin)*0.0048828125;   
  float distance = 65*pow(volts, -1.10);   
  Serial.println(" cm");


Circuit Diagram
Diagram 2preyy7e72
Schematics 1 7gnyuv7cmm


Similar projects you might like

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 20 respects

Touch Sensor and Sound Sensor Controlling AC/DC Lights

Project showcase by Anbumachi

  • 7 respects

Standalone Arduino Applied in Projects

Project tutorial by PCBWay and Silícios Lab

  • 43 respects

MQ-4 Natural Gas Sensor Experiment Based on Arduino

Project tutorial by Andrew85

  • 1 comment
  • 7 respects
Add projectSign up / Login