F FisherHub Docs

传感器入门

传感器在智能硬件中的角色

传感器是将物理世界和数字世界之间建立桥梁的关键元件。它们把温度、光照、加速度、气体浓度等物理量转换为电信号,供微控制器读取和处理。

选择传感器时,需要考虑精度、量程、接口类型、功耗、体积和成本六个维度。

温湿度传感器

最常见的是 DHT11/DHT22 和 SHT3x 系列。

# DHT22 读取示例 (MicroPython)
from machine import Pin
import dht

sensor = dht.DHT22(Pin(4))

def read_environment():
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()
    print(f"温度: {temp:.1f}°C, 湿度: {hum:.1f}%")
    return temp, hum
型号精度(温度)精度(湿度)接口价格
DHT11±2°C±5%单总线极低
DHT22±0.5°C±2%单总线
SHT30±0.3°C±2%I2C
BME280±1°C±3%I2C/SPI

选型建议:原型验证用 DHT22,量产产品用 SHT30 或 BME280(后者还集成气压计)。

距离传感器

技术型号量程精度适用场景
超声波HC-SR042cm-400cm±3mm液位、避障
红外VL53L0X30cm-200cm±1mm接近检测、手势
毫米波LD24100.75m-5m可调人体存在检测
// HC-SR04 超声波测距 (Arduino)
const int trigPin = 9;
const int echoPin = 10;

void setup() {
    Serial.begin(115200);
    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, 30000);
    if (duration == 0) {
        Serial.println("超出范围");
        return;
    }
    
    float distance = duration * 0.034 / 2;
    Serial.print("距离: ");
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
}

注意:超声波传感器受温度影响,mmWave 传感器能穿透非金属障碍物,红外传感器不适合阳光直射环境。

IMU 惯性测量单元

IMU 由加速度计、陀螺仪(有时加磁力计)组成,用于姿态检测和运动追踪。

# MPU6050 读取示例 (MicroPython)
from machine import Pin, I2C
import math

i2c = I2C(0, scl=Pin(22), sda=Pin(21))
mpu = MPU6050(i2c)

def calculate_pitch_roll():
    ax, ay, az = mpu.acceleration
    pitch = math.atan2(-ax, math.sqrt(ay*ay + az*az)) * 180 / math.pi
    roll = math.atan2(ay, az) * 180 / math.pi
    return pitch, roll

常见型号:

  • MPU6050 — 6 轴(accel+gyro),价格低,适合入门
  • ICM-20948 — 9 轴,功耗更低,适合可穿戴
  • BMI270 — 12 轴,Bosch 出品,常用于手机

使用 IMU 时需要注意零点漂移,建议用卡尔曼滤波或 Mahony 互补滤波融合数据。

光敏传感器

型号输出特点
LDR (光敏电阻)模拟电压最廉价,响应慢
BH1750数字(I2C)输出 lux 值,精度高
TSL2591数字(I2C)超高灵敏度,适合穿戴
// BH1750 读取 (Arduino)
#include <Wire.h>
#include <BH1750.h>

BH1750 lightMeter;

void setup() {
    Serial.begin(115200);
    Wire.begin();
    lightMeter.begin();
}

void loop() {
    float lux = lightMeter.readLightLevel();
    Serial.print("光照: ");
    Serial.print(lux);
    Serial.println(" lux");
    delay(1000);
}

气体传感器

常见气体传感器包括:

  • MQ-2/MQ-135 — 可燃气体、空气质量,模拟输出,需要预热
  • CCS811 — TVOC + eCO2,I2C 接口,数字输出
  • SGP30 — TVOC + CO2,超低功耗,长寿命
// MQ-2 气体传感器 (Arduino)
int gasPin = A0;

void setup() {
    Serial.begin(115200);
    // MQ系列传感器需要预热30秒以上
    delay(30000);
}

void loop() {
    int value = analogRead(gasPin);
    float voltage = value * 5.0 / 1024;
    
    // 电压越高,气体浓度越大
    Serial.print("气体浓度: ");
    Serial.println(voltage, 3);
    delay(1000);
}

MQ 系列传感器功耗较高(加热丝约 150mW),电池供电产品建议选用电化学或半导体类低功耗气体传感器。

传感器选型决策树

  1. 需要测量什么物理量?→ 缩小到该类型的传感器
  2. 精度要求如何?→ 确定数字输出还是模拟输出
  3. 接口兼容性?→ 检查 MCU 是否有足够 I2C/SPI/ADC 资源
  4. 功耗限制?→ 确认工作电流和待机电流
  5. 成本预算?→ 批量价格需向代理商询价

小结

传感器选型没有绝对最优,只有最适合。建议先用手头开发板验证关键性能,再根据实测数据做最终选择。数字接口传感器(I2C/SPI)比模拟传感器更容易使用、更可靠,是优先选项。