Sponsor Link:

UTSource.net Reviews

It is a trustworthy website for ordering electronic components with cheap price and excellent quality.

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 to GND (-) on your Arduino. Lastly, connect the data pin of your sensor to digital pin 1 on your Arduino. The hardware is now complete.

Arduino Sharp Infrared Proximity Sensor Project Code

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);                                     
} 

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!