How to use an Infrared Proximity Sensor (Long-Range)
The sharp infrared distance proximity sensor is a sensor for distance measurements. This sensor is typically used for measuring larger ranges(approximately 20 cm to 150 cm (8″ to 60″)), although it is quite accurate.
How it works
The component uses a specific light sensor to detect a light wavelength in the Infrared (IR) wave spectrum. The intensity of the light is received when an object is close to the sensor. The light bounces off the object and into the light sensor, which results in a change to the intensity.
Wiring
- Red Wire: connected to 5V
- Black Wire: connected to ground
- Yellow Wire: connect to an analog pin
Parts
- Sharp Infrared Long-Range Proximity Sensor (Analog 20-150cm)
- Arduino Uno R3
- BreadBoard
- 10 kΩ resistor
- Jumper wires
- Arduino IDE
Wiring Guide
- Connect the Voltage and Ground Wires to the breadboard
- Connect the component wire (yellow wire) to an analog pin
- Follow the code provided
- Under the Tools folder select you board and pick the port you are using to upload the code onto the Arduino
- Watch the change in data from the Serial Monitor found under the Tools tab
Programming
The following code demonstrates how the short proximity sensor works and measures the distance when the sensor is moved towards or away from an object, within the range of 20-150cm. If you want to skip the steps, you can jump to the Full Code.
Step 1: Create variables for the sensors pin and a variable to store the data that is collected
int sensor = A0; //sensor pin connection
Step 2: Set up serial communication, set the sensor pin as an INPUT.
void setup() {
Serial.begin(9600); // start the serial port
pinMode(sensor, INPUT); //setting the pin to the Arduino
}
Step 3: Read the data from the sensor, and convert the data into a volts measurement
void loop() {
//5V
float volts = analogRead(sensor)*0.0048828125; // value from sensor * (5/1024)
Step 4: Now convert the volts measurement into distance, a measurement we understand. This calculation is found in the datasheet gragh of the component and can be found here!
int distance = 13*pow(volts, -1); // worked out from datasheet graph
delay(1000); // slow down serial port
Step 5: Finally use the distance we calculated and display ONLY when the distance is less than or equal to its max limit.
if (distance >= 20 && distance <= 150){
Serial.println(distance); // prints the distance
}
}
Full Code
int sensor = A0; //sensor pin connection
void setup() {
Serial.begin(9600); //start the serial port
pinMode(sensor, INPUT); //setting the pin to the Arduino
}
void loop() {
//5V
float volts = analogRead(sensor)*0.0048828125; // value from sensor * (5/1024)
int distance = 13*pow(volts, -1); // worked out from datasheet graph
delay(1000); // slow down serial port
if (distance >= 20 && distance <= 150){
Serial.println(distance); // prints the distance
}
}
Output
Serial Monitor will display distance that is measure from the proximity sensor, only if the distance measure is greater than or equal to 20 and less than or equal to the max limit which is 150cm for this exact component.