Проверка
Проверка
Проверка
Проверка
Проверка
Проверка
The ESP32, a powerhouse in the IoT world found in billions of devices, excels at connecting things. But how does it sense its immediate environment? Proximity detection – the ability to detect the presence or absence of an object nearby – is a fundamental capability for creating interactive and responsive ESP32 projects. Choosing and implementing the right ESP32 proximity sensor opens doors to countless applications.
Understanding Proximity Sensors: The ESP32’s “Digital Sense”
Think of proximity sensors as the ESP32’s eyes or feelers. While humans use sight or touch, these sensors utilize various physical principles to detect objects without physical contact. The ESP32, with its versatile GPIO pins, powerful ADC (Analog-to-Digital Converter), and precise timing capabilities, is exceptionally well-suited to interface with and process data from a wide range of proximity sensors. For makers and engineers, this means unlocking capabilities like automatic activation, collision avoidance, object counting, and presence detection.

Common ESP32 Proximity Sensor Types & How They Work
Distance = (Time of Flight * Speed of Sound) / 2).pulseIn() or hardware timers. Calculating distance involves knowing the speed of sound (affected by temperature/humidity; some sensors include compensation).Implementing an ESP32 Proximity Sensor: A Practical Glimpse (Ultrasonic Example)
Let’s consider a basic setup with the ubiquitous HC-SR04 ultrasonic sensor:
VCC to ESP32 5V (or 3.3V if compatible), GND to ESP32 GND, Trig to a chosen GPIO (e.g., GPIO 5), Echo to another GPIO (e.g., GPIO 18). Note: The HC-SR04 typically requires 5V logic levels; use a logic level converter if connecting its Echo pin (5V) to the ESP32 (3.3V tolerant IOs usually work, but caution advised).const int trigPin = 5;
const int echoPin = 18;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW); // Clear trigger
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); // Send 10µs pulse
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH); // Measure echo pulse duration (µs)
float distanceCM = duration * 0.034 / 2; // Speed of sound ~ 0.034 cm/µs. Divide by 2 (round trip)
Serial.print("Distance: ");
Serial.print(distanceCM);
Serial.println(" cm");
delay(100); // Adjust loop rate
}
This code triggers the sensor, measures the echo time, calculates distance using the speed of sound, and prints it. Real-world code needs robust error handling (timeouts).
Compelling Project Ideas Using ESP32 Proximity Sensors