---
title: Sensors
url: https://doc.liz6.com/en/hardware/09-embedded-systems/03-sensors
locale: en
area: hardware
tags:
- hardware
- embedded-systems
date: 2026-06-30
modified: 2026-07-16
description: The complete chain from physical quantities to I2C register values
---

# Sensors

> The complete chain from physical quantities to I2C register values

---

## Sensor Interface Selection

| Interface | Distance | Speed | Power Consumption | Multi-device | Typical Sensors |
|------|------|------|------|--------|------------|
| I2C | PCB | 100k~1M | Low | ✅ (Address) | Most digital sensors |
| SPI | PCB | MHz | Medium | Needs CS | High-speed ADC, IMU |
| UART | Long | 115k~1M | Medium | ❌ One-to-one | GPS, LiDAR |
| Analog | PCB | - | Very Low | ❌ | NTC, LDR, Microphone |
| 1-Wire | Long | Slow | Very Low | ✅ (ROM ID) | DS18B20 |
| Pulse/PWM | PCB | - | Low | ❌ | Hall effect, Tachometer |

---

## Temperature Sensors

### NTC Thermistor (Cheapest)
```
Circuit: Voltage Divider
  Vcc
   │
   R_fixed (10k, 1%)
   │
   ├── ADC Input
   │
   NTC
   │
  GND

Temperature Calculation:
  R_ntc = R_fixed × (Vcc - V_adc) / V_adc
  T = 1 / (1/T25 + ln(R_ntc/R25)/B) - 273.15
  T25 = 298.15K, R25 = Nominal resistance @25°C, B = B constant

Accuracy: ±0.5°C (with accurate B value + 1% resistor), sufficient for most scenarios
Pros: Cheap (a few cents), fast response, small size
Cons: Non-linear, requires MCU ADC, requires lookup table or B-parameter formula
```

### DS18B20 (Digital 1-Wire)
```
Protocol: 1-Wire (one data line, parasitic power optional)
Accuracy: ±0.5°C (-10~85°C)
Resolution: 9~12 bit
Each unit has a unique 64-bit ROM ID → Multiple devices on one bus

Pros: No ADC needed, factory calibrated, supports long distance, supports multi-point
Cons: Expensive (~¥3), slow (750ms @12bit), complex protocol
```

### Thermocouple (High Temperature)
```
Principle: Thermoelectric potential generated at the junction of two different metals (Seebeck effect)
Types: Type K (most common, -200~1250°C), J/T/E/N types
Requires: Cold junction compensation (measure cold junction temperature + lookup table)

Chips: MAX6675 (Type K, SPI), MAX31855/MAX31856
Applications: 3D printer hotends, ovens, industrial furnaces
```

### Quick Selection Guide
```
Scenario                      Recommendation
────────────────────────────────────
Room temp, ±2°C is enough     NTC (Cheapest)
Factory calibrated, no ADC    DS18B20
High temp (>150°C)            Thermocouple (Type K)
High precision (±0.1°C)       PT100/PT1000 (Platinum RTD)
Human body temp               Medical-grade IR
PCB board temp                Built-in sensor or NTC
```

---

## IMU (Inertial Measurement Unit)

### Typical Chips

| Chip | Accelerometer | Gyroscope | Magnetometer | Interface | Features |
|------|----------|--------|--------|------|------|
| MPU6050 | 3-axis | 3-axis | ❌ | I2C | Classic entry-level |
| MPU9250 | 3-axis | 3-axis | 3-axis | I2C/SPI | 9-axis integrated |
| ICM-20948 | 3-axis | 3-axis | 3-axis | I2C/SPI | Low-power 9-axis |
| BMI160 | 3-axis | 3-axis | ❌ | I2C/SPI | Ultra-low power |
| LSM6DS3 | 3-axis | 3-axis | ❌ | I2C/SPI | ST mainstream |
| BNO055 | 3-axis | 3-axis | 3-axis | I2C/UART | **Built-in attitude solution!** |

### IMU Outputs
```
Accelerometer: Acceleration (m/s² or g)
  Static: Z-axis = 1g (gravity)
  Can calculate pitch and roll (using gravity direction)

Gyroscope: Angular velocity (°/s or rad/s)
  Integrate to get angle → but drifts
  Static ideal value = 0 (actual has zero offset)

Magnetometer: Magnetic field strength (μT)
  Can calculate yaw (compass)

9-axis fusion = Accelerometer + Gyro + Magnetometer → Drift-free absolute attitude
  (Gyro is fast but drifts, Accel+Mag are drift-free but slow/noisy)
  → Complementary filter / Kalman filter / Madgwick algorithm
```

### Practical Pitfalls
```
- MPU6050 needs calibration after power-on (static sampling hundreds of times, take zero offset)
- I2C address conflict (MPU6050 default 0x68, AD0 pulled high becomes 0x69)
- Accelerometer unusable in vibrating environments (requires more complex filtering)
- Magnetometer is extremely susceptible to interference (keep away from motors/currents/ferromagnetic materials!)
- BNO055 saves fusion code but is expensive
```

---

## Environmental Sensors

### Temperature & Humidity
```
DHT11/DHT22: 1-Wire, cheap but slow (once every 2s), average accuracy
SHT30/31: I2C, ±2%RH, fast, reliable
BME280: I2C/SPI, Temp+Humidity+Pressure, one chip three uses ★Recommended
AHT20: I2C, cheap alternative to SHT30

Pitfalls: DHT11 is severely inaccurate at >80%RH or <10%RH
    SHT/BME series are much better
```

### Barometric Pressure
```
BMP280/BME280: I2C/SPI, accuracy ±1hPa (±8.5m altitude)
LPS22HB: I2C/SPI, ±0.5hPa
MS5611: I2C/SPI, ±0.15hPa (High precision)

Applications: Altimeter, weather station, drone altitude hold
```

### Light
```
BH1750: I2C, directly outputs lux value (no calibration needed!)
VEML7700: I2C, wide range, low power
OPT3001: I2C, spectral response matched to human eye

LDR (Light Dependent Resistor): Analog, cheap but non-linear + requires calibration
```

### Air Quality / Gas
```
CCS811: I2C, eCO2 + TVOC (requires 48h aging, high power consumption)
SGP30: I2C, eCO2 + TVOC (better than CCS811)
PMS5003: UART, Laser PM2.5/PM10 (physical measurement, accurate!)
MH-Z19: UART, NDIR CO2 (physical measurement)
MQ series (MQ-2/MQ-135...): Analog, requires warm-up, inaccurate
```

---

## Sensor Debugging Routine

```
1. Power on → Read WHO_AM_I / Device ID register
   Cannot read → Check power supply/soldering/I2C address/pull-up resistors
   Read successfully → Sensor is alive, continue

2. Read raw data → Check if reasonable
   All 0s or 0xFF → Configuration issue (not enabled/conversion not started)
   Data fluctuates wildly → Power noise / Timing issues

3. Compare with reference values
   NTC calculated temp vs room temp → Difference >5°C check B parameter or R_fixed
   IMU static accel ≈ 1g → If not, check range configuration

4. Add filtering
   Simple moving average → Eliminate high-frequency noise
   Median filter → Eliminate sporadic spikes

5. Calibration (if needed)
   Temperature: Ice water 0°C + Boiling water 100°C two-point calibration
   IMU: Six-face calibration (static sampling on each face)
```

---
*Keywords: NTC, DS18B20, Thermocouple, MPU6050, IMU, BME280, SHT30, I2C Sensor, Calibration*
