keepcalculator

06 Numbers and Powers

First 4 ESP32 Sensors: Temperature, Motion, Distance

Card No. 06·6602 · Updated · Standard formula, shown below

If you are just starting with the ESP32, the first four sensors you should try are a temperature/humidity sensor (like the DHT22), a passive infrared motion sensor (HC-SR501), an ultrasonic distance sensor (HC-SR04), and an ambient light sensor (like the LDR or BH1750). These four cover the most common environmental and interaction needs for IoT projects, and each teaches a different fundamental skill: reading analog values, digital timing, and I2C communication.

Why These Four Sensors First

These sensors are the “hello world� of physical computing because they are cheap, widely documented, and use different communication methods. The DHT22 uses a single-wire protocol, the HC-SR501 just outputs a digital HIGH/LOW signal, the HC-SR04 uses timing pulses, and the BH1750 uses I2C. By wiring and coding each one, you learn the three main ways microcontrollers talk to sensors: digital input, pulse timing, and serial protocols. That knowledge transfers directly to almost every other sensor you will ever use.

Another reason to start with these four is that they are forgiving. If you miswire a temperature sensor, you usually just get a wrong reading, not a fried board. The HC-SR04 is also tolerant of voltage mismatches, and the HC-SR501 works with a simple 3.3V supply. They are also all available as breakout boards that plug directly into a breadboard, so you do not need to solder anything to get started.

Temperature and Humidity: DHT22 vs. DHT11

The DHT22 (also sold as the AM2302) is the better choice for most projects. It measures temperature from -40°C to 80°C with ±0.5°C accuracy, and humidity from 0 to 100% with ±2% accuracy. The cheaper DHT11 is less accurate (±2°C, ±5% humidity) and only measures up to 50°C. The DHT22 costs a bit more, but the extra accuracy matters for anything like a greenhouse or a weather station.

Wiring is simple: VCC to 3.3V, GND to GND, and the data pin to any GPIO. You need a 10kΩ pull-up resistor between the data line and VCC. Many breakout boards already include this resistor, so check the board you buy. In code, you use a library like DHT sensor library by Adafruit, which handles the timing-critical single-wire protocol for you.

A common mistake is using the onboard 3.3V regulator but then powering the sensor from the 5V pin. The DHT22 can work with 5V, but the data line then outputs 5V, which can damage the ESP32’s GPIO pins. Always power the sensor from 3.3V or use a level shifter.

Motion Detection: HC-SR501 PIR Sensor

The HC-SR501 is a passive infrared sensor that detects changes in infrared radiation in its field of view. When a warm body (like a person or animal) moves across the sensor, it outputs a HIGH signal on its OUT pin for a set amount of time. It is called “passive� because it does not emit anything; it just senses heat.

You can adjust two potentiometers on the board: one sets the delay (how long the output stays HIGH, from about 3 seconds to 5 minutes), and the other sets sensitivity (detection range, roughly 3 to 7 meters). There is also a jumper to select between “H� (retriggering) and “L� (non-retriggering) modes. In H mode, every new movement restarts the delay timer; in L mode, the output goes HIGH for the set delay and then goes LOW even if there is still movement.

Wire VCC to 3.3V, GND to GND, and OUT to a GPIO. The output is 3.3V so it is safe for the ESP32. In code, you simply read digitalRead() on that pin. A common issue is the sensor triggering falsely for the first 30–60 seconds after power-up while it calibrates. Add a delay in setup() or ignore readings for the first minute.

Distance: HC-SR04 Ultrasonic Sensor

The HC-SR04 measures distance by sending an ultrasonic pulse and timing how long it takes for the echo to return. It has four pins: VCC, Trig, Echo, and GND. You send a 10µs HIGH pulse to Trig, and the sensor responds with a HIGH pulse on Echo whose duration is proportional to the distance. The distance in centimeters is duration (in microseconds) divided by 58.

The critical issue is that the Echo pin outputs 5V, which is too high for the ESP32’s GPIO pins. You must use a voltage divider (two resistors, e.g., 1kΩ and 2kΩ) or a level shifter to bring it down to 3.3V. Many tutorials skip this, but it is essential to avoid damaging the board. Alternatively, you can use a 3.3V-compatible ultrasonic sensor like the HC-SR04P or the JSN-SR04T.

In code, you use pulseIn() to measure the echo pulse. The sensor has a range of about 2 cm to 400 cm, with an accuracy of roughly ±3 mm. It works best with a flat, solid surface perpendicular to the sensor. Soft surfaces like fabric or carpet absorb the sound and give unreliable readings. Also, the sensor has a beam angle of about 15 degrees, so it can pick up objects slightly off to the side.

Ambient Light: LDR vs. BH1750

An LDR (light-dependent resistor) is the simplest light sensor. It is just a resistor whose resistance changes with light. You wire it in series with a fixed resistor (e.g., 10kΩ) to form a voltage divider, then read the analog voltage on an ESP32 ADC pin. The code is a simple analogRead(). The downside is that the LDR’s response is non-linear and it is not calibrated to any standard unit, so you only get relative brightness.

The BH1750 is a digital ambient light sensor that outputs a value in lux (a standard unit of illuminance). It uses I2C, so you connect VCC to 3.3V, GND to GND, SDA to GPIO21, and SCL to GPIO22 (the default I2C pins on most ESP32 boards). The Adafruit BH1750 library makes it easy to read lux values from 0 to 65535. This sensor is more accurate and consistent than an LDR, and it is still very cheap.

For most projects, the BH1750 is worth the extra dollar. If you just need to detect day/night, an LDR is fine. If you want to log light levels or control a display’s brightness, use the BH1750.

How to Choose and Common Pitfalls

When picking sensors, check the voltage requirements. The ESP32 runs at 3.3V logic, and many sensors designed for Arduino (which runs at 5V) may output 5V signals. Always check the datasheet or look for “3.3V compatible� in the description. For the HC-SR04, you must use a level shifter or a 3.3V variant.

Another common pitfall is using the ESP32’s ADC without knowing its quirks. The ESP32 ADC is non-linear, especially in the upper range, and its input impedance is relatively low. For accurate analog readings, you may need to add a capacitor in parallel with the sensor and use the analogReadMilliVolts() function or a calibration table. For the LDR, this is usually not critical, but for a temperature sensor like the TMP36, it matters.

Also, be aware of the ESP32’s pin limitations. Some GPIO pins (e.g., GPIO0, GPIO2, GPIO12) have pull-up or pull-down resistors at boot, which can interfere with sensors that use those pins. Stick to safe pins like GPIO4, GPIO16, GPIO17, GPIO21, GPIO22, GPIO23, GPIO25, GPIO26, GPIO27, and GPIO32–GPIO36 (input-only for 34–36).

Comparing Sensor Options and Costs

For temperature, the DHT22 is a good all-rounder, but if you need higher accuracy, the DS18B20 is waterproof and uses a one-wire protocol. The BME280 measures temperature, humidity, and barometric pressure over I2C or SPI, and is excellent for weather stations. For motion, the HC-SR501 is the standard, but the RCWL-0516 microwave sensor can detect motion through walls and is not affected by heat. For distance, the HC-SR04 is cheap and works well indoors, but the VL53L0X time-of-flight laser sensor is much more accurate and works in the dark, though it has a shorter range (about 2 meters). For light, the BH1750 is the best balance of cost and accuracy.

All of these sensors are inexpensive, usually under $5 each. The total cost for the four recommended sensors is typically under $15. You will also need a breadboard and jumper wires, which are a few dollars. The ESP32 board itself is the most expensive part, but it is still under $10 for a common development board.

Frequently Asked Questions

Do I need a level shifter for the HC-SR04 with ESP32?

Yes, you should use a level shifter or a voltage divider on the Echo pin because it outputs 5V, which exceeds the ESP32’s 3.3V maximum. A simple two-resistor divider (1kΩ and 2kΩ) works fine. Alternatively, buy a 3.3V version of the sensor.

Can I power the DHT22 with 5V?

The DHT22 can be powered with 5V, but the data line will then output 5V, which can damage the ESP32. If you power it with 5V, you must also add a level shifter on the data line. The simplest solution is to power it with 3.3V.

Why does my PIR sensor trigger randomly?

The HC-SR501 needs a warm-up period of about 30–60 seconds after power-up. It may also be picking up heat from a nearby appliance, direct sunlight, or a rapid change in ambient temperature. Adjust the sensitivity potentiometer down and ensure the sensor is not facing a heat source.

How do I get stable analog readings from an LDR?

The ESP32’s ADC is noisy. Add a 0.1µF capacitor between the analog pin and ground, and take multiple readings (e.g., 10) and average them. You can also use analogReadMilliVolts() to get a more stable voltage value.

What is the maximum distance the HC-SR04 can measure?

The HC-SR04 is rated for 2 cm to 400 cm (4 meters). In practice, reliable readings are usually up to about 3 meters. For longer distances, consider the JSN-SR04T (waterproof, up to 6 meters) or a laser-based sensor.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →