Sensors in IoT

Light Dependent Resistor

  • 光敏电阻是一种电阻值随光照强度变化的传感器,key parameter为varying resistance

Light Sensor Based on LDR and Op-Amp

  • 利用 LDR 将光照强度变化转换为电阻变化,再通过分压电路转换为电压变化:
  • 光越强, 越小,因此输出电压越高。
  • 使用 Gain = 1 的 Op-Amp Buffer 隔离 LDR 分压电路与 Arduino,减轻后级的loading effect(更正:Buffer的高输入阻抗减少从分压电路取流,低输出阻抗用于驱动后级)
  • 最后由 Arduino 的 10-bit ADC 将模拟电压转换为 的数字值。
  • 课件 p.24 按 10-bit ADC 最大码值写作:。若以 5.0 V 为参考电压,代码可用 analogRead(A0) * (5.0 / 1023.0) 估算输入电压。
  • ADC 量化步长常用 描述;它与按最大码值进行的端点换算 5.0/1023 用途不同,不能混为一个分母。

课件 p.25 的程序从 A0 读取光敏电阻模块的模拟值,并将反向亮度映射到 D9 的 PWM 输出。下面是加入 0–255 边界处理和串口输出的独立 sketch;850 是课件给出的实验映射参数,受模块、电路和环境影响,不是通用校准值。

const int sensorPin = A0;
const int ledPin = 9;
 
void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  int sensorValue = analogRead(sensorPin);
  int brightness = (850 - sensorValue) / 4;
 
  if (brightness < 0) {
    brightness = 0;
  } else if (brightness > 255) {
    brightness = 255;
  }
 
  analogWrite(ledPin, brightness);
  Serial.print("ADC: ");
  Serial.print(sensorValue);
  Serial.print("  PWM: ");
  Serial.println(brightness);
  delay(10);
}

此 sketch 使用 Arduino Uno 的 A0 模拟输入和支持 PWM 的 D9;应将 LED 串联限流电阻。

Photodiode

  • 光电二极管也是光传感器,key parameter为varying current
  • 原理是当具有足够能量的光子照射PN junction时会产生electron-hole pair从而光生电流,也就是说光越强光电流越大

Thermistor

  • 热敏电阻的电阻会随温度变化

Temperature Sensor Circuit

  • 此处Thermistor与其他电阻组成检测电路,再送入Comparator与参考电压进行比较
  • 当温度达到设定条件时,比较器输出就会翻转,从而控制LED或是输出端

  • Comparator简析:
  • 直接比较电压,此处采用运放,当 时输出接近 ,当 时输出接近

Soil Humidity Sensor

  • Key Parameter:Varying Resistance
  • 土壤含水量改变 → 探针间导电能力/等效电阻改变 → 输出电压改变。
  • 一般土壤越湿,导电能力越强;课件实验中 Soil Moisture ↑ → Sensor Voltage ↓。
  • 可通过实验曲线进行 calibration,将传感器电压换算为 Soil Moisture。 Lecture#4

DHT11: Temperature and Humidity Sensor

  • 同时检测 Temperature + Humidity。
  • 内部包含:
    • NTC Thermistor:温度 → Resistance 变化。
    • Humidity Sensing Component:湿度 → Capacitance 变化。
  • 相对湿度:

  • 电容式湿度检测基本关系:

湿度变化会改变介质性质,从而改变 。

  • 传感器出厂时的 Calibration Coefficients 储存在 OTP Memory,由内部检测程序自动进行校准。 Lecture#4

DHT11 Data Transmission

DHT11 使用数字信号向 MCU 发送 40 bits:

依次为:

  1. Humidity Integral Part
  2. Humidity Decimal Part
  3. Temperature Integral Part
  4. Temperature Decimal Part
  5. Checksum
    • Bit 0 / 1 通过 HIGH 电平持续时间不同来区分。
    • 课件示例:
      • Humidity:0011 0101 → 53(课件标注 35% 有误,见下方换算)
      • Temperature:0001 1000 → 24°C
    • Data pin 通常配合上拉电阻连接 MCU。 Lecture#4

课件 p.36 的湿度字节为 0011 0101。按二进制位权计算:,所以数值是 53,不是课件标注的 35%;这是课件中的换算错误。温度字节 0001 1000 则为 。

课件代码区实际展示了以下库名、对象构造和串口初始化。这里仅记录可确认的初始化片段:它不是完整的采样程序;课件没有注明 DHT11 库的版本,完整采样程序需使用所选库对应的读取接口。

#include <DHT11.h>
 
DHT11 dht11(2);
 
void setup() {
  Serial.begin(9600);
}

DHT11 vs DHT22

DHT11DHT22
Voltage3–5 V3–5 V
Max Current2.5 mA2.5 mA
Temperature0–50°C / ±2°C-40–80°C / ±0.5°C
Humidity20–80% / ±5%0–100% / ±2–5%
Sampling Rate1 Hz0.5 Hz
AdvantageLow CostMore Accurate

简单理解:DHT11 便宜且快一点;DHT22 范围更大、精度更高。 Lecture#4

Touch Sensor

  • Key Parameter:Varying Capacitance
  • 基本电容:

  • 未触摸时主要存在 Sensor Capacitance 。
  • 手指靠近/触摸后引入 Finger Capacitance → 总电容增加。
  • 控制 IC 检测电容变化并输出 Touch / No Touch 状态。
  • 课件模块使用 TTP223 Touch Sensor IC。 Lecture#4

Sound Sensor

  • Key Parameter:Varying Output Voltage
  • 使用 Dynamic Microphone:
    • Sound → diaphragm 振动
    • 带动 coil 在 magnetic field 中运动
    • 电磁感应产生变化的输出电压
  • 即:

模块包含:

  • Microphone
  • LM393 Comparator
  • Sensitivity Adjust
  • A0:Analog Output
  • D0:Digital Output
  • Power LED / Sound Detect LED
  • VCC / GND

A0 可观察声音强弱对应的模拟电压;D0 通过 Comparator 判断声音是否超过设定阈值。 Lecture#4

Electromagnetic Induction Review

电流会产生磁场——Ampere’s Law:

变化的磁通会产生 EMF——Faraday’s Law:

其中:

  • :Magnetic Field
  • :Electric Current
  • :Permeability
  • :Magnetic Flux
  • :Electromotive Force (EMF)

也是 Near-Field Inductive WPT 等系统的基础。

Vibrate Sensor

  • 课件使用 SW-18015P Vibration Shaking Switch。
  • 振动 → 内部机械触点状态变化 → 电路产生电压变化。
  • 模块通过 LM393 Comparator 将其转换为稳定的 Digital Output。
  • 可用于检测震动、敲击、移动等事件。 Lecture#4

Ultrasound Sensor

课件使用 HC-SR04,通过测量超声波的 Time of Flight 判断距离。

工作流程:

声速:

因为声波经历“去 + 回”:

因此:

其中 为 Echo HIGH 持续时间。 Lecture#4

HC-SR04 时序:

  1. MCU 向 Trigger 输入约 10 μs HIGH。
  2. Sensor 发出一组约 40 kHz ultrasound pulses。
  3. 超声波遇到物体后反射。
  4. RX 接收到反射波。
  5. Echo HIGH duration = propagation delay。
  6. MCU 根据 Echo 时间计算距离。 Lecture#4

根据课件 p.46 整理的独立完整示例分别将 D7 接 Trig、D8 接 Echo。课件代码中 readUltrasonicDistance(7, 7) 把两者写成同一脚位,与 HC-SR04 的独立 Trig/Echo 接线不一致;以下按四脚模块分别接线。pulseIn(..., 30000UL) 将等待时间限制为 30 ms,返回 0 表示超时,此时不计算距离(参见 Arduino pulseIn() 文档)。

const int triggerPin = 7;
const int echoPin = 8;
 
unsigned long readEchoTimeUs() {
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  return pulseIn(echoPin, HIGH, 30000UL);
}
 
void setup() {
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  digitalWrite(triggerPin, LOW);
  Serial.begin(9600);
}
 
void loop() {
  unsigned long durationUs = readEchoTimeUs();
  if (durationUs == 0) {
    Serial.println("Echo timeout");
  } else {
    float cm = durationUs * 0.017f;
    float inch = cm / 2.54f;
    Serial.print(inch, 2);
    Serial.print(" in, ");
    Serial.print(cm, 2);
    Serial.println(" cm");
  }
  delay(100);
}

函数在触发前先输出 LOW 2 μs,再发送 10 μs HIGH 脉冲;距离按往返时间计算。英寸转换为:

Commonly Used Actuators in IoT

Relay

Relay = 用低功率控制信号控制高功率电路的电磁开关。

  • Coil 通电 → 产生磁场 → 改变机械开关位置。
  • 三个主要接点:
    • COM:Common
    • NC:Normally Closed
    • NO:Normally Open
  • Coil 未通电:COM ↔ NC。
  • Coil 通电:COM ↔ NO。
  • 用途:Arduino 等低压控制器控制 AC 220 V 等高功率负载。 Lecture#4

课件示例:

  • BC547 transistor:放大 Arduino 控制电流、驱动 relay coil。
  • 1N4007 diode:用于处理 relay coil 断电时产生的反向感应电压。
  • 控制电路和高压负载由 Relay 隔离。 Lecture#4

以下是补充实现:假设继电器驱动模块为高电平有效,Arduino 输出只连接模块的 SIG/控制输入;负载由驱动级和继电器触点切换,不能由 Arduino 引脚直接驱动。

const int relayPin = 7;
 
void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);  // 初始关闭(高电平有效模块)
}
 
void loop() {
  digitalWrite(relayPin, HIGH);
  delay(1000);
  digitalWrite(relayPin, LOW);
  delay(1000);
}

Buzzer

利用 **Piezoelectric Disc(压电片)**振动产生声音。

Passive Buzzer

  • 本身没有 oscillator。
  • 需要外部 AC / PWM waveform 驱动。
  • 改变 frequency → 改变音调,可播放旋律。

Active Buzzer

  • 内部自带 oscillator。
  • 输入 DC Voltage 即可自动产生 AC 驱动压电片。
  • 控制简单,但音调控制能力较弱。 Lecture#4

Buzzer Experiment:Arduino 按照不同 frequency 和 duration 输出信号给 Passive Buzzer,从而产生 melody。 Lecture#4

  • tone(pin, frequency, duration):在指定pin产生方波;frequency以Hz为单位,duration以ms为单位。
  • noTone(pin):停止该pin的发声信号。

下面按课件 p.51 的音符频率、音符数组和节拍数组整理成一个独立 sketch,使用 D8 驱动 Passive Buzzer。休止符用 0 表示,并显式跳过 tone();每个音符后均调用 noTone()。tone() 的 duration 参数以毫秒计,但函数本身不等待音符结束,因此用 delay() 控制节拍。旋律只在 setup() 中播放一次,loop() 留空(参见 Arduino tone() 文档)。

const int buzzerPin = 8;
const int NOTE_G3 = 196;
const int NOTE_A3 = 220;
const int NOTE_B3 = 247;
const int NOTE_C4 = 262;
 
const int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3,
  NOTE_G3, 0, NOTE_B3, NOTE_C4
};
const int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
const int noteCount = sizeof(melody) / sizeof(melody[0]);
 
void setup() {
  for (int i = 0; i < noteCount; i++) {
    int noteDuration = 1000 / noteDurations[i];
    if (melody[i] != 0) {
      tone(buzzerPin, melody[i], noteDuration);
    }
    delay(noteDuration * 1.3);
    noTone(buzzerPin);
  }
}
 
void loop() {
}

DC Motor

基本原理:载流导体处于磁场中会受到 Lorentz Force。

通过 Right Hand Rule 判断:

  • Current
  • Magnetic Field
  • Force

之间的方向关系。

Motor 内主要包括:

  • Stator
  • Rotor / Armature
  • Coil
  • Brush
  • Commutator

Rotor coil 受到力矩开始旋转;Commutator 会周期性切换电流方向,使转矩始终推动 rotor 向同一方向连续旋转。 Lecture#4

Vector Calculus Review

Unit Vector:

Dot Product

  • 衡量两个 vector 在相同方向上的程度。
  • 结果为 Scalar。

Cross Product

  • 衡量两个 vector 的方向差异。
  • 结果为 Vector。

Cross Product 的方向由 Right Hand Rule 判断。 Lecture#4

Ampere’s Law

电流周围会产生环形 Magnetic Field:

  • 电流越大 → 磁场越强。
  • 磁场方向由 Right Hand Rule 判断。
  • Solenoid 将多匝线圈产生的磁场叠加,使内部形成较强磁场。 Lecture#4

Control DC Motor

Arduino 通常使用 PWM (Pulse Width Modulation) 控制 DC Motor。

PWM:

  • Frequency / Period 保持固定。
  • 改变 Duty Cycle → 改变平均输出功率 → 改变 motor speed。

因此:

实际 Motor 需要较大电流,因此课件电路使用外部电源和驱动元件,而不是直接由 Arduino I/O 驱动。 Lecture#4

以下是补充实现:电位器接 A0,通过 D9 的 PWM 控制电机驱动器输入;D9 必须连接 driver/MOSFET 等驱动级的控制端,不能直接驱动电机。电机由匹配的外部电源供电,并按驱动电路要求共地。

const int potPin = A0;
const int motorPwmPin = 9;
 
void setup() {
  pinMode(motorPwmPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  int potValue = analogRead(potPin);
  int pwmValue = map(potValue, 0, 1023, 0, 255);
  analogWrite(motorPwmPin, pwmValue);
  Serial.println(pwmValue);
  delay(10);
}

Servo Motor

Servo Motor 是带有 Position Feedback 的电机系统,内部包含:

  • DC Motor:产生旋转。
  • Gear Train:减速并增加 torque。
  • Potentiometer:检测当前角度。
  • Control Unit:比较目标角度与实际角度并自动调整。
  • 属于 Closed-Loop Control。 Lecture#4

Servo 通过 pulse width 控制位置:

  • Frequency ≈ 50 Hz
  • Period ≈ 20 ms
  • 约 1 ms → 0°
  • 1.5 ms → 90°
  • 约 2 ms → 180°
  • 不同 Servo 可扩展到约 0.5–2.5 ms。 Lecture#4

课件 p.58 的映射为 pulsewidth = (angle * 11) + 500,即约 0° → 500 μs、180° → 2480 μs。这是课件示例的映射范围;实际脉宽范围因舵机而异,使用前应匹配具体舵机规格,避免超出机械行程。

以下完整sketch需分别上传,不可将多个setup() / loop()合并。舵机程序保留课件的两层 for 结构:每个角度发送 10 个约 20 ms 周期的脉冲,再从 0° 扫到 180° 并返回;周期包含软件执行开销,因此是近似值。为兼顾 Arduino Uno AVR 上较长的 LOW 间隔,将其拆为毫秒和余数微秒两段等待,作为对课件单次 delayMicroseconds(20000 - pulsewidth) 的补充实现;Arduino 文档说明该函数适合短延时(参见 delayMicroseconds() 文档)。

const int servoPin = 7;
 
void sendServoPosition(int angle) {
  int pulsewidth = (angle * 11) + 500;
  unsigned long lowUs = 20000UL - pulsewidth;
 
  for (int i = 0; i < 10; i++) {
    digitalWrite(servoPin, HIGH);
    delayMicroseconds(pulsewidth);
    digitalWrite(servoPin, LOW);
    delay(lowUs / 1000UL);
    delayMicroseconds(lowUs % 1000UL);
  }
}
 
void setup() {
  pinMode(servoPin, OUTPUT);
}
 
void loop() {
  for (int angle = 0; angle < 180; angle += 2) {
    sendServoPosition(angle);
  }
  for (int angle = 180; angle > 0; angle -= 2) {
    sendServoPosition(angle);
  }
}

Step Motor

Stepper Motor 通过依次给不同 coil 通电改变 stator magnetic field,使 rotor 每次旋转一个固定角度:

特点:

  • 一次移动一个固定 Step。
  • 可以精确控制 angle / position。
  • 改变线圈通电顺序 → 改变旋转方向。
  • 连续快速切换 → 连续旋转。 Lecture#4

Motor Comparison

Motor主要特点课件应用
DC Motor连续高速旋转、主要控制 SpeedWashing Machines、Air Conditioners、Computer Fans
Stepper Motor固定 Step、精确 Position ControlCar Side Mirror Tilts、Security Cameras、3D Printers
Servo MotorFeedback + 精确 Angle ControlCameras、Solar Array / Antenna Positioning、Automatic Doors

Lecture#4


Tutorials and Circuit Simulation

Power Switch Circuit

目的:用低功率控制信号控制外部 Power Supply。

课件 Arduino 电路使用 PMOS;Multisim 中只有 2N7000G NMOS,因此模拟时需要调整电路。 Lecture#4

整体关系:

Simulation 分成两个 Block:

  • **Block 1

    • 建立 circuit schematic。
    • 输入 和 。
    • 同时观察 、 waveform。
    • Voltage range:-15 V ~ +15 V。
    • Time:0 ~ 1 s。
  • **Block 2

    • 使用 MOSFET power switch。
    • 根据 Block #1 的 控制 MOSFET。
    • 同时观察 、 waveform。
    • 课件答案展示 两种状态对应的切换结果。 Lecture#4

Magnetic Field from Solenoid

多匝 coil 中的电流产生的 magnetic fields 相互叠加,形成类似条形磁铁的磁场:

  • Solenoid 内部:磁场较强、方向较一致。
  • 外部:磁力线由一端返回另一端。
  • 相关量:
    • :单位长度内穿过 enclosed surface 的 current turns
    • :积分路径长度
    • :Electric Current
    • :Permeability

其基础仍是 Ampere’s Law:

Lecture#4

第 47、61 页为章节目录页;第 67 页为课程安排,第 68 页为结束页,没有新增 Sensors / Actuators 知识点。


链接