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.

  • 46 views
  • 0 comments
  • 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.
×1
SparkFun Sharp Infrared Proximity Sensor (2Y0A21)
×1
11026 02
Jumper wires (generic)
3 Male to Male Jumper Wires.
×3
Adafruit industries ada62 image 75px
USB-A to B Cable
Depends on the Arduino.
×1
Pcbway qeheggiidg
PCBWay Custom PCB
×1

Apps and online services

About this project

Before starting the project...

Credits go to Lucky Larryon

http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/

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: https://www.pcbway.com/project/OnlineGerberViewer.html

PCBWay Gift Shop: https://www.pcbway.com/projects/gifts.html

​Enjoy! Contact us for any inquiries!

Code

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

Schematics

Circuit Diagram
Diagram 2preyy7e72
Schematics
Schematics 1 7gnyuv7cmm

Comments

Similar projects you might like

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 5,726 views
  • 7 comments
  • 20 respects

Touch Sensor and Sound Sensor Controlling AC/DC Lights

Project showcase by Anbumachi

  • 2,054 views
  • 2 comments
  • 7 respects

Standalone Arduino Applied in Projects

Project tutorial by PCBWay and Silícios Lab

  • 6,021 views
  • 10 comments
  • 43 respects

MQ-4 Natural Gas Sensor Experiment Based on Arduino

Project tutorial by Andrew85

  • 1,251 views
  • 1 comment
  • 7 respects
Add projectSign up / Login