How to Use Sensors with Arduino and ESP32: Temperature, Distance, Load, Current, and Hall Effect
Introduction
Sensors are how microcontrollers perceive the physical world. From measuring room temperature with a $2 chip to weighing objects with a load cell, sensors turn physical quantities into electrical signals that Arduino and ESP32 can read, process, and act upon. This guide covers the five most useful sensor categories for maker projects: temperature and humidity, distance (ultrasonic), force and weight (load cells), current sensing, and magnetic field detection (Hall effect). For each sensor, you will learn the working principle, wiring, code, calibration, and practical applications.
What You Need
- Arduino Uno/Nano or ESP32
- Sensors: DHT22, HC-SR04, HX711 + load cell, ACS712, A3144/A1324 Hall sensor
- Jumper wires and breadboard
- Resistors, capacitors for signal conditioning as needed
Part 1: Temperature and Humidity (DHT22 / DHT11 / SHT30)
The DHT22 (AM2302) is the most popular combined temperature and humidity sensor for hobby projects. It provides digital output over a single wire, eliminating the need for analog calibration.
DHT22 Specifications
- Temperature range: -40 to 80°C, ±0.5°C accuracy
- Humidity range: 0-100% RH, ±2-5% accuracy
- Sample rate: 0.5Hz (one reading every 2 seconds max)
- Voltage: 3.3V to 6V
Wiring
- VCC → 3.3V or 5V
- GND → GND
- DATA → Digital pin (with 10kΩ pull-up resistor between DATA and VCC)
Arduino Code
#include "DHT.h" #define DHTPIN 2 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); } void loop() { delay(2000); // DHT22 needs 2 seconds between readings float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } Serial.print("Humidity: "); Serial.print(h); Serial.print("% "); Serial.print("Temperature: "); Serial.print(t); Serial.println("°C"); }SHT30 (I2C Alternative)
The SHT30 is more accurate (±0.3°C, ±2% RH) and uses I2C, making it easier to wire and faster to read.
#include #include "SHTSensor.h" SHTSensor sht; void setup() { Wire.begin(); sht.init(); } void loop() { sht.readSample(); Serial.print(sht.getTemperature()); Serial.print("°C "); Serial.print(sht.getHumidity()); Serial.println("%"); delay(1000); }Part 2: Distance Sensing (HC-SR04 Ultrasonic)
The HC-SR04 measures distance using ultrasonic pulses. It emits a 40kHz sound burst and measures the time until the echo returns.
Specifications
- Range: 2cm to 400cm (claimed), realistically 2-300cm
- Accuracy: ±3mm
- Angle: 15° cone
- Voltage: 5V (3.3V versions available as RCW-0001)
Wiring
- VCC → 5V
- GND → GND
- Trig → Digital pin (output — triggers measurement)
- Echo → Digital pin (input — receives echo timing)
Working Principle
- Send 10μs HIGH pulse to Trig pin
- Sensor emits 8 ultrasonic pulses at 40kHz
- Echo pin goes HIGH when pulses are emitted
- Echo pin goes LOW when echo is received
- Duration of HIGH = round-trip time
- Distance (cm) = (duration × 0.034) / 2
Arduino Code
const int trigPin = 9; const int echoPin = 10; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); long duration = pulseIn(echoPin, HIGH); float distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(500); }Pro Tips
- Soft materials (fabric, foam) absorb sound — unreliable readings
- Angles >15° cause missed echoes
- Multiple sensors need staggered triggering to avoid cross-talk
- Temperature compensation improves accuracy: speed of sound varies with temperature
Part 3: Force and Weight (HX711 + Load Cell)
Load cells measure force (and thus weight) by detecting tiny deformations in a metal beam using strain gauges. The HX711 is a 24-bit ADC amplifier designed specifically for load cells.
Load Cell Types
- Single-point (platform scale): 1kg, 5kg, 10kg, 50kg — one cell supports the entire platform
- S-type (hanging scale): Tension and compression, 50kg-5 ton
- Button/pancake: Compression only, compact
Wiring
- Load cell red → HX711 E+
- Load cell black → HX711 E-
- Load cell white → HX711 A+
- Load cell green → HX711 A-
- HX711 VCC → 5V (or 3.3V for 3.3V HX711 module)
- HX711 GND → GND
- HX711 DT → Digital pin
- HX711 SCK → Digital pin
Arduino Code
#include "HX711.h" #define DT 3 #define SCK 2 HX711 scale; void setup() { Serial.begin(9600); scale.begin(DT, SCK); scale.set_scale(); // Calibrate later scale.tare(); // Zero with no load } void loop() { Serial.print("Reading: "); Serial.print(scale.get_units(), 1); // One decimal place Serial.println(" g"); delay(500); }Calibration
- Upload code with scale.set_scale() only (no calibration factor)
- Tare (zero) with no load: scale.tare()
- Place a known weight (e.g., 500g)
- Note the raw reading
- Calibration factor = raw_reading / known_weight
- Update code: scale.set_scale(calibration_factor)
- Verify with multiple known weights
Part 4: Current Sensing (ACS712 / INA219)
Current sensors let you monitor power consumption, detect motor stall, or implement overcurrent protection.
ACS712 Hall-Effect Current Sensor
- Non-invasive: sensor sits in series with the load
- Outputs analog voltage proportional to current
- Versions: 5A (185mV/A), 20A (100mV/A), 30A (66mV/A)
- Isolation: 2.1kV dielectric strength
Wiring
- IP+ and IP- → Inline with the wire carrying current to measure
- VCC → 5V
- GND → GND
- VOUT → Analog pin
Arduino Code (ACS712-5A)
const int sensorPin = A0; const float sensitivity = 0.185; // 185mV per A for 5A version void setup() { Serial.begin(9600); } void loop() { int raw = analogRead(sensorPin); float voltage = raw * (5.0 / 1023.0); float current = (voltage - 2.5) / sensitivity; // 2.5V = zero point Serial.print("Current: "); Serial.print(current, 2); Serial.println(" A"); delay(500); }INA219 (I2C Current/Voltage/Power)
The INA219 is a digital current monitor with I2C output. It measures both bus voltage and shunt voltage, calculating current and power internally.
#include #include Adafruit_INA219 ina219; void setup() { ina219.begin(); } void loop() { float current_mA = ina219.getCurrent_mA(); float power_mW = ina219.getPower_mW(); Serial.print("Current: "); Serial.print(current_mA); Serial.print(" mA "); Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW"); delay(1000); }Part 5: Hall Effect Sensors (A3144 / A1324 / DRV5053)
Hall effect sensors detect magnetic fields. They are used for position sensing, speed detection, current sensing, and brushless motor commutation.
Types
- Digital (switch): A3144, 3144E — output HIGH/LOW when field exceeds threshold
- Linear (analog): A1324, DRV5053 — output voltage proportional to field strength
- Current transducer: Integrated with current path (ACS712 is a Hall current sensor)
A3144 Digital Hall Sensor
Used for endstops, door sensors, RPM counting, and magnetic switches.
Wiring
- VCC → 5V
- GND → GND
- OUT → Digital pin (with 10kΩ pull-up to VCC)
Arduino Code
const int hallPin = 2; void setup() { pinMode(hallPin, INPUT_PULLUP); Serial.begin(9600); } void loop() { int state = digitalRead(hallPin); Serial.println(state == LOW ? "Magnet detected" : "No magnet"); delay(100); }A1324 Linear Hall Sensor
Measures magnetic field strength for position sensing and joystick-like control.
const int hallPin = A0; void setup() { Serial.begin(9600); } void loop() { int raw = analogRead(hallPin); float voltage = raw * 5.0 / 1023.0; // Quiescent output is ~2.5V, sensitivity ~5mV/Gauss float field = (voltage - 2.5) / 0.005; Serial.print("Field: "); Serial.print(field); Serial.println(" Gauss"); delay(100); }Sensor Selection Guide
ApplicationRecommended SensorInterfaceCost Room temp/humidityDHT22 or SHT301-Wire / I2C$3-5 Distance / levelHC-SR04Digital pulse$1-2 Weight / forceHX711 + 5kg load cellDigital$3-5 DC currentACS712-5A or INA219Analog / I2C$2-4 Magnetic switchA3144Digital$0.50 Magnetic field strengthA1324 or DRV5053Analog$1-3Pro Tips
- Averaging: Take 10-50 samples and average for noisy analog sensors
- Shielding: Current sensors and Hall sensors are affected by nearby magnetic fields — keep away from motors and transformers
- Decoupling: Add 100nF ceramic capacitor across sensor VCC/GND near the sensor for stable readings
- Cable length: Keep sensor cables short; for long runs use shielded cable or digital sensors (I2C)
- Calibration: Always calibrate load cells and current sensors with known references
- Timing: DHT22 needs 2 seconds between readings; querying faster gives garbage data
- Temperature compensation: Ultrasonic sensors and load cells drift with temperature — add a temperature sensor and compensate in software
Conclusion
Sensors transform physical phenomena into data your microcontroller can act upon. Temperature sensors monitor environments, ultrasonic sensors measure distance without contact, load cells weigh objects with precision, current sensors protect circuits and measure power, and Hall sensors detect magnets for position and speed. The common thread is proper wiring, appropriate libraries, calibration against known standards, and software filtering to handle real-world noise. With these five sensor types, you can build weather stations, automated scales, collision-avoiding robots, battery monitors, and magnetic position encoders.
Related Guides
- I2C vs SPI vs UART: How to Choose and Use Serial Communication Protocols
- Current Sensing for Makers: Shunt Resistors, INA219/INA226, and ACS712 Hall-Effect Sensors
- ESP32 ADC Explained: Non-Linearity, Attenuation, and Calibrating Analog Readings for Real Accuracy
- How to Program Addressable LED Strips: WS2812B Patterns, Effects, and Power Design
- How to Control Motors with Arduino and ESP32: Stepper, DC, and Servo Drivers
- ESP32: Setting Up for Arduino IDE
- Arduino vs ESP32: Which Should You Use? A Practical Comparison
- Getting Started with ESP32: GPIO, WiFi, and Your First Project