Introduction
This experiment demonstrates how to use an infrared distance sensor to measure the distance of objects using an Arduino.
Components Needed
- Infrared Distance Sensor
- Arduino
- Jumper Wires
Circuit Setup
- Connect the VCC and GND pins of the infrared sensor to the 5V and GND pins on the Arduino.
- Connect the output of the infrared sensor to an analog input pin (e.g., A0) on the Arduino.
The infrared sensor detects the distance to objects based on the reflection of infrared light.
Code for Infrared Distance Measurement
Upload the following code to your Arduino to measure distance:
const int sensorPin = A0;
int distance;
void setup() {
Serial.begin(9600);
}
void loop() {
distance = analogRead(sensorPin);
Serial.print("Distance: ");
Serial.println(distance);
delay(1000);
}
Explanation
The infrared sensor sends out infrared light and measures the amount of reflection that comes back, calculating the distance to the object based on this feedback.
Troubleshooting
- If the sensor reads a constant value, check for obstructions or dirt on the sensor lens.
- Ensure proper connections between the sensor and Arduino.