Arduino Proximity Sensors
Introduction
Proximity sensors are essential components in many Arduino projects, allowing your creations to detect nearby objects, measure distances, and interact with the environment. These sensors enable behaviors like obstacle avoidance in robots, automatic door systems, and interactive art installations.
In this guide, we'll explore various proximity sensors compatible with Arduino, how they work, and how to implement them in your projects. By the end, you'll be able to select the right proximity sensor for your needs and integrate it into your Arduino sketches.
Types of Proximity Sensors
Arduino projects commonly use several types of proximity sensors:
- Ultrasonic sensors - Use sound waves to measure distance
- Infrared (IR) proximity sensors - Detect objects using infrared light
- Time-of-Flight (ToF) sensors - Measure distance using light pulses
- Capacitive proximity sensors - Detect objects without physical contact
Let's explore each type in detail.
Ultrasonic Sensors
Ultrasonic sensors like the HC-SR04 are among the most popular proximity sensors for Arduino. They work by sending out ultrasonic pulses and measuring the time it takes for the echo to return.
How Ultrasonic Sensors Work
- The Arduino sends a pulse to the sensor's trigger pin
- The sensor emits an ultrasonic sound wave
- The sound wave bounces off objects and returns to the sensor
- The sensor detects the echo and sends a signal to the Arduino
- The Arduino calculates the distance based on the time between sending and receiving
HC-SR04 Ultrasonic Sensor Example
Wiring Diagram
- Connect VCC to Arduino 5V
- Connect GND to Arduino GND
- Connect Trig to Arduino digital pin 9
- Connect Echo to Arduino digital pin 10
Code Example
const int trigPin = 9;
const int echoPin = 10;
// Variables for duration and distance
long duration;
int distance;
void setup() {
// Initialize Serial communication
Serial.begin(9600);
// Set pin modes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Clear the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set trigPin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, return sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate distance
// Speed of sound is approximately 343 meters per second or 0.0343 cm/microsecond
// The sound wave travels to the object and back, so we divide by 2
distance = duration * 0.0343 / 2;
// Print distance to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Expected Output
Distance: 15 cm
Distance: 16 cm
Distance: 14 cm
Distance: 50 cm
Distance: 120 cm
Limitations
- Ultrasonic sensors may not detect soft, fabric-covered, or angled objects well
- They typically have a range of 2cm to 400cm
- They're less accurate in extreme temperatures or humid conditions
Infrared (IR) Proximity Sensors
IR proximity sensors use infrared light to detect objects. They're generally less expensive than ultrasonic sensors but have a shorter range.
Sharp GP2Y0A21YK Example
This popular IR distance sensor can measure distances between 10cm and 80cm.
Wiring Diagram
- Connect VCC to Arduino 5V
- Connect GND to Arduino GND
- Connect OUT to Arduino analog pin A0
Code Example
const int irSensorPin = A0;
int distance;
void setup() {
Serial.begin(9600);
}
void loop() {
// Read the analog value from the sensor
int sensorValue = analogRead(irSensorPin);
// Convert the analog reading to distance in cm
// This formula is specific to the GP2Y0A21YK sensor
// For other sensors, consult the datasheet
distance = 4800 / (sensorValue - 20);
// Print the distance
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Digital IR Proximity Sensor
Some IR sensors simply detect if an object is within a certain range rather than measuring the exact distance.
Wiring Diagram
- Connect VCC to Arduino 5V
- Connect GND to Arduino GND
- Connect OUT to Arduino digital pin 7
Code Example
const int irSensorPin = 7;
void setup() {
Serial.begin(9600);
pinMode(irSensorPin, INPUT);
}
void loop() {
// Read the digital value
int objectDetected = digitalRead(irSensorPin);
if (objectDetected == LOW) {
Serial.println("Object detected!");
} else {
Serial.println("No object detected");
}
delay(500);
}
Time-of-Flight (ToF) Sensors
ToF sensors like the VL53L0X use light pulses to measure distance with high precision. They're more accurate than IR sensors and work well in varying lighting conditions.