Explain how sensors bridge the physical and digital worlds by converting physical phenomena into electrical signals
Interpret sensor specifications including accuracy, precision, resolution, range, and response time from datasheets
Apply signal processing techniques such as filtering and calibration to obtain reliable sensor measurements
Classify sensors by measurement type, output signal, and power requirements for systematic selection
Select appropriate sensors for IoT projects by evaluating cost, performance, environmental constraints, and interface compatibility
In 60 Seconds
Sensors bridge the physical and digital worlds by converting physical phenomena — temperature, pressure, motion, light — into electrical signals a microcontroller can read. Key specifications to compare are: accuracy (closeness to true value), precision (repeatability), resolution (smallest detectable change), range (measurement limits), and response time (how quickly the output tracks a change). Always read the datasheet before selecting a sensor.
2.2 Sensors: The Eyes and Ears of IoT
For Beginners: What This Chapter Covers
Welcome to the world of sensors! This is your roadmap for learning everything about how IoT devices “sense” the physical world.
What you’ll learn:
What sensors are and why every IoT device needs them
How to choose the right sensor for temperature, motion, distance, and more
How to read datasheets so you can understand any sensor’s capabilities
Common mistakes to avoid that trip up even experienced engineers
Hands-on labs where you’ll build real sensor projects
Time commitment: The full series takes about 5-6 hours. But you can jump to any chapter that interests you.
Prerequisites: Basic understanding of electricity (voltage, current) and electronics (circuits, components). See the Prerequisites section below for links.
What Sensor Selection Really Depends On
When two sensors appear to measure the same thing, the real decision usually comes down to four constraints:
Measurement fit: Does the range, accuracy, resolution, and response time match the problem?
Electrical fit: Does the output type match your hardware: analog, I2C, SPI, UART, or 1-Wire?
Deployment fit: Will it still perform under the actual temperature, humidity, vibration, dust, or washdown conditions?
System fit: What does it cost in BOM budget, calibration effort, firmware complexity, and battery life?
This chapter introduces the main sensor families first. Later chapters show how datasheets, calibration, signal conditioning, and power management turn that initial choice into a working IoT design.
Sensors are the fundamental building blocks of IoT systems, serving as the bridge between the physical and digital worlds. They convert physical phenomena (temperature, pressure, motion, light, etc.) into electrical signals that can be processed by microcontrollers and computers.
This comprehensive guide has been organized into focused chapters for easier learning. Start with the introduction and work through each chapter, or jump directly to topics of interest.
2.3 Learning Path
Figure 2.1: Beginner-friendly overview of sensor types
Key insight: 12-bit resolution (0.0805°C steps) does not mean 0.0805°C accuracy. Real accuracy is limited by sensor tolerance (±1-2°C for thermistors) and ADC linearity (±0.16°C). Resolution determines how smoothly readings change, not how correct they are.
Sensor Sampling Power Budget:
Show code
viewof iActive = Inputs.range([0.1,50], {value:1.5,step:0.1,label:"Active Current (mA)"})viewof tActive = Inputs.range([0.01,10], {value:0.75,step:0.01,label:"Active Time (s)"})viewof iSleep = Inputs.range([0.0001,1], {value:0.001,step:0.0001,label:"Sleep Current (mA)"})viewof sampleInterval = Inputs.range([1,3600], {value:600,step:1,label:"Sample Interval (s)"})viewof batteryCapacity = Inputs.range([100,10000], {value:2500,step:100,label:"Battery Capacity (mAh)"})// Ensure sample interval is at least as long as active timeeffectiveSampleInterval =Math.max(sampleInterval, tActive)tSleep = effectiveSampleInterval - tActiveavgCurrent = ((iActive * tActive) + (iSleep * tSleep)) / effectiveSampleIntervalbatteryLifeHours = batteryCapacity / (avgCurrent >0? avgCurrent :0.001)batteryLifeYears = batteryLifeHours /8760html`<div style="background: var(--bs-light, #f8f9fa); padding: 15px; border-radius: 8px; border-left: 4px solid #E67E22; margin-top: 10px;"> <h4 style="margin-top: 0; color: #2C3E50;">Power Budget Calculator</h4> <p style="font-size: 14px; line-height: 1.6; margin: 8px 0;"> <strong>Average Current:</strong> ${avgCurrent.toFixed(4)} mA <br><strong>Battery Life:</strong> ${batteryLifeHours.toFixed(0)} hours (${batteryLifeYears.toFixed(1)} years)${sampleInterval < tActive ?'<br><span style="color: #E74C3C; font-weight: bold;">Warning: Sample interval increased to '+ tActive +'s (must be >= active time)</span>':''} </p> <p style="font-size: 13px; color: #7F8C8D; margin: 8px 0; font-style: italic;"> Note: Theoretical battery life. Real-world performance limited by self-discharge (~2-3% per year for alkaline primary cells, ~2-5% per month for NiMH rechargeable). For multi-year deployments, use lithium primary cells (e.g., LiSOCl2). </p></div>`
Example Calculation (DS18B20 reading every 10 minutes):
Use this interactive tool to evaluate whether a candidate temperature sensor meets your application requirements. Adjust the sliders to match your sensor’s specifications and your application needs. For other sensor types (humidity, distance, acceleration), apply the same pass/fail methodology with appropriate units.
Or jump to a specific topic using the learning path table above.
2.9 Key Takeaways
Sensors are the interface between physical phenomena and digital processing
Match specifications to requirements – a sensor’s accuracy, resolution, and response time must each meet or exceed your application’s needs, with margin for real-world degradation
High resolution does not guarantee reliability – always verify that accuracy is sufficient for your control tolerance before considering resolution
Calibration is essential for most sensing applications to correct factory tolerances and environmental drift
Read datasheets carefully – distinguish between typical and worst-case specs, and note the conditions under which specifications were measured
2.10 Knowledge Check
Quiz: Sensor Fundamentals
Match each sensor specification to its correct definition:
Place these sensor selection steps in the correct order:
Worked Example: Thermostat Sensor Selection with Real Numbers
Scenario: You’re designing a smart thermostat for residential HVAC control. The system must maintain room temperature within ±1.0°C of the setpoint (e.g., if setpoint is 22°C, acceptable range is 21-23°C). You’re evaluating three temperature sensor options:
Candidate Sensors:
Sensor
Accuracy
Resolution
Conversion / Min Sample Time
Cost
Interface
A: DHT22
±0.5°C
0.1°C
2s min sampling period
$4
Digital (proprietary)
B: TMP36
±1°C at 25°C (±2°C full range)
~0.01°C (via ADC)
~1s analog settle
$1.50
Analog voltage
C: DS18B20
±0.5°C
0.0625°C
750ms conversion (12-bit)
$3
Digital (1-Wire)
Analysis:
Step 1: Accuracy Requirement Check
System must detect ±1.0°C deviations from setpoint
Example: If setpoint = 22°C, must reliably distinguish 21°C from 23°C
Accuracy requirement: Better than ±0.5°C (half the threshold margin)
Sensor A (DHT22): ±0.5°C accuracy ✓ - At true 21°C, could read 20.5°C to 21.5°C - At true 23°C, could read 22.5°C to 23.5°C - Worst case: True 21°C reads as 21.5°C, just below 22°C threshold → PASS
Sensor B (TMP36): ±1°C accuracy at 25°C ⚠️ (marginal) - At true 21°C, could read 20°C to 22°C - At true 23°C, could read 22°C to 24°C - Worst case: True 21°C reads as 22°C (setpoint) → System fails to detect 1°C undershoot - Verdict: Marginal – the sensor’s accuracy exactly equals the control tolerance, leaving zero margin for drift, aging, or self-heating. In practice, this is risky for reliable ±1°C control
Sensor C (DS18B20): ±0.5°C accuracy ✓ - Same analysis as Sensor A → PASS
Step 2: Resolution Requirement Check
Need to distinguish 1°C differences
Resolution must be at least 10× finer than accuracy requirement for smooth control
Requirement: ≤0.1°C resolution
Sensor A (DHT22): 0.1°C resolution ✓ (exactly meets requirement)
Sensor B (TMP36):
Analog output: 10 mV/°C
ADC: 12-bit (4096 steps) on 3.3V reference
Voltage resolution: 3.3V / 4096 = 0.805mV per ADC step
Temperature resolution: 0.805mV / (10mV/°C) = 0.0805°C ✓ (exceeds requirement)
Note: High resolution doesn’t compensate for marginal ±1°C accuracy!
Sensor C (DS18B20): 0.0625°C resolution ✓ (far exceeds requirement)
Step 3: Conversion Time and Thermal Response
Conversion/sampling time (time to produce a reading): All three sensors are well under 5 seconds
Thermal response time (time for sensor package to equilibrate with environment): Typically 5-10s in still air for TO-92 packages (DS18B20, TMP36), longer in protective housings
Residential HVAC: thermal mass of room is slow (10-30 minutes to change 1°C), so both conversion time and thermal response are negligible
Control loop update rate: 10 seconds sufficient
All sensors meet timing requirements: DHT22 (2s sampling period), TMP36 (~1s analog settle), DS18B20 (750ms conversion) ✓
Step 4: Interface Complexity
Digital sensors (DHT22, DS18B20): Software library handles communication
Analog sensor (TMP36): Requires ADC pin, voltage divider if 5V sensor on 3.3V MCU
Winner: DS18B20 or DHT22 (both meet all requirements)
Final Selection: DS18B20
Reasoning:
Accuracy: ±0.5°C meets ±1°C control requirement with margin
Resolution: 0.0625°C provides smooth control (16× finer than requirement)
Multi-sensor capability: 1-Wire bus allows multiple DS18B20s on one GPIO pin for zone control or averaging
Cost: $3 is acceptable for residential thermostat BOM
Reliability: Digital interface immune to ADC noise and voltage reference drift
Implementation:
#include <OneWire.h>#include <DallasTemperature.h>#define ONE_WIRE_BUS 4OneWire oneWire(ONE_WIRE_BUS);DallasTemperature sensors(&oneWire);void setup(){ Serial.begin(9600); sensors.begin();}void loop(){ sensors.requestTemperatures();float temp = sensors.getTempCByIndex(0); Serial.print("Temperature: "); Serial.print(temp); Serial.println(" C");// Control logic with hysteresis (±0.5°C band around 22°C setpoint)// Note: In production, widen hysteresis to >=2x sensor accuracy// to prevent oscillation from measurement noise at switching pointsif(temp <21.5){// Turn on heating}elseif(temp >22.5){// Turn on cooling}// Else: within comfort range, no action delay(10000);// Read every 10 seconds}
Real-World Validation: After deployment, measure actual performance: - Record 1000 temperature readings over 24 hours - Compare to calibrated reference thermometer - Calculate mean absolute error (MAE) and standard deviation - Expected: MAE < 0.3°C, σ < 0.2°C - If MAE > 0.5°C: perform two-point calibration to correct offset
Cost-Benefit Analysis:
DS18B20 ($3) vs TMP36 ($1.50) = $1.50 premium
Value: Reliable ±1°C control can prevent up to 10-20% energy waste in scenarios with frequent overshooting (actual savings depend on insulation, climate, and HVAC efficiency)
Key Lesson: For control applications, accuracy matters far more than resolution. The TMP36’s excellent 0.0805°C resolution is undermined by its ±1°C accuracy at room temperature, which leaves zero margin for a ±1°C control requirement. Always match sensor specifications to your application requirements with adequate margin, not just “more is better.”
Common Pitfalls
1. Confusing Accuracy and Precision
A sensor can be precise (repeatable) but inaccurate (consistently wrong by a fixed offset). Calibration corrects systematic offset errors. A sensor with large random variation is imprecise — filtering reduces the effect but calibration cannot fix random noise.
2. Ignoring Operating Environment in Selection
A sensor with excellent specs at 25 C may perform much worse at the actual deployment temperature. Always check the datasheet for accuracy vs. temperature curves and verify the sensor is rated for the full deployment temperature range.
3. Selecting Sensors Without Checking Interface Compatibility
Choosing a sensor before verifying the microcontroller has enough GPIO pins and the required peripheral hardware wastes design time. Confirm the interface protocol, logic levels, and library availability before purchasing.
4. Underestimating Power in Battery Applications
The datasheet active current often reflects peak consumption. Calculate average current using the duty cycle formula: Iavg = Iactive x (ton/tperiod) + Isleep x (1 - ton/tperiod) to accurately estimate battery life.
2.11 What’s Next
After completing this series, continue your IoT learning journey with: