← How-Tos
electronics May 7, 2026 ◑ 1 views ◯ 6 min read

How to Use Sensors with Arduino and ESP32: Temperature, Distance, Load, Current, and Hall Effect

sensorsarduinoesp32temperatureultrasonicload cellcurrenthall effectpressurehumidity

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

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

Wiring

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

Wiring

Working Principle

  1. Send 10μs HIGH pulse to Trig pin
  2. Sensor emits 8 ultrasonic pulses at 40kHz
  3. Echo pin goes HIGH when pulses are emitted
  4. Echo pin goes LOW when echo is received
  5. Duration of HIGH = round-trip time
  6. 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

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

Wiring

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

  1. Upload code with scale.set_scale() only (no calibration factor)
  2. Tare (zero) with no load: scale.tare()
  3. Place a known weight (e.g., 500g)
  4. Note the raw reading
  5. Calibration factor = raw_reading / known_weight
  6. Update code: scale.set_scale(calibration_factor)
  7. 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

Wiring

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

A3144 Digital Hall Sensor

Used for endstops, door sensors, RPM counting, and magnetic switches.

Wiring

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-3

Pro Tips

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.