|
本次尝试使用 Micropython 进行开发。官方提供了 Python 驱动,暂无原生 Micropython 驱动,但二者语法相近,移植难度不大。 1、硬件介绍C4002 是 DFRobot 推出的 24GHz 毫米波人体存在传感器模块。模块突破了传统PIR传感器只能检测大幅运动的局限,可在10x10m的有效检测范围内,同步侦测运动人体与静止/微动人体,并支持运动速度检测、运动方向识别(靠近/远离)及环境光检测功能。 工作频率为 24GHz~24.25GHz 检测距离:运动最远 11 米、微动 / 静止最远 10 米,支持参数配置 可检测人体运动、静止存在状态,同时识别运动方向与运动速度 探测角度:120°×120° 采用 UART 通信接口,波特率 115200bps 模块内置光照传感器,检测范围 0~50lux 整体尺寸为 22mm × 26mm
2、驱动目前官方仅提供 C/C++、Python 版本驱动,未适配 Micropython。我借助 AI 工具,将 Python 驱动进行移植改造,成功适配出可用的 Micropython 驱动。 驱动详见附件。 3、快速上手以 ESP32-C3 为例,硬件接线如下: [td]C4002 | ESP32 | 说明 | | VCC | 5V | 电源 | | GND | GND | 地线 | | TX | GPIO0 | 串口接收 | | RX | GPIO1 | 串口发送 |
注意: 一定要接 5V,3.3V 供电可能不稳定。 附上简易测试示例代码:
- import time
- import machine
- from DFRobot_C4002 import DFRobot_C4002, PresenceTarget, MotionTarget
-
- # 初始化 UART,使用 GP0(TX) 和 GP1(RX)
- # 默认波特率: 115200
- c4002 = DFRobot_C4002(uart_num=1, baud=115200, tx=0, rx=1)
-
- # 初始化 GPIO2 作为雷达输出引脚的输入端
- radar_output_pin = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_DOWN)
- c4002.set_out_pin_mode(c4002.OUT_PIN_MODE1)
-
- def setup():
- """初始化传感器"""
- while c4002.begin() != True:
- print("C4002 初始化失败!")
- time.sleep_ms(500)
-
- print("C4002 初始化成功!")
- time.sleep_ms(50)
-
- # 关闭指示灯
- if c4002.set_run_led_state(c4002.LED_OFF):
- print("设置运行指示灯成功!")
- else:
- print("设置运行指示灯失败!")
-
- if c4002.set_out_led_state(c4002.LED_OFF):
- print("设置输出指示灯成功!")
- else:
- print("设置输出指示灯失败!")
-
- # 设置分辨率模式(80cm 分辨率,支持 15 个距离门限)
- if c4002.set_resolution_mode(c4002.RESOLUTION_80CM):
- print("设置分辨率模式成功!")
- else:
- print("设置分辨率模式失败!")
-
- # 设置检测范围 0-1100cm
- if c4002.set_detect_range(0, 1100):
- print("设置检测范围成功!")
- else:
- print("设置检测范围失败!")
-
- # 设置光照阈值(0 = 无论光照条件如何始终检测)
- if c4002.set_light_thresh(0):
- print("设置光照阈值成功!")
- else:
- print("设置光照阈值失败!")
-
- # 启用所有距离门限
- gate_state = [
- c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
- c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
- c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
- c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
- c4002.C4002_ENABLE, c4002.C4002_ENABLE, c4002.C4002_ENABLE,
- ]
-
- if c4002.configure_gate(c4002.MOTION_DISTANCE_GATE, gate_state):
- print("启用运动距离门限成功!")
-
- if c4002.configure_gate(c4002.PRESENCE_DISTANCE_GATE, gate_state):
- print("启用存在距离门限成功!")
-
- # 设置上报周期(10 * 0.1s = 1s)
- if c4002.set_report_period(10):
- print("设置上报周期成功!")
-
-
- def get_gpio2_status():
- """读取 GPIO2 的状态并返回中文描述"""
- pin_value = radar_output_pin.value()
- if pin_value == 1:
- return "HIGH (检测到目标)"
- else:
- return "LOW (未检测到目标)"
-
-
- def loop():
- """主循环"""
- ret_result = c4002.get_note_info()
-
- if ret_result.note_type == c4002.RESULT:
- print("------- 获取全部结果 --------")
-
- # 光照强度(lux)
- light = c4002.get_light_intensity()
- print("光照强度: {:.2f} lux".format(light))
-
- # 目标状态
- target_state = c4002.get_target_state()
- print("目标状态: ", end="")
- if target_state == c4002.NO_TARGET:
- print("无目标")
- elif target_state == c4002.PRESENCE:
- print("静态存在")
- elif target_state == c4002.MOTION:
- print("运动")
-
- # 存在倒计时
- presence_count_down = c4002.get_presence_count_down()
- print("存在倒计时: {} s".format(presence_count_down))
-
- # 存在目标信息
- presence_target = c4002.get_presence_target_info()
- print("存在距离: {:.2f} m".format(presence_target.distance))
- print("存在能量: {}".format(presence_target.energy))
-
- # 运动目标信息
- motion_target = c4002.get_motion_target_info()
- print("运动距离: {:.2f} m".format(motion_target.distance))
- print("运动能量: {}".format(motion_target.energy))
- print("运动速度: {:.2f} m/s".format(motion_target.speed))
- print("运动方向: ", end="")
- if motion_target.direction == c4002.AWAY:
- print("远离!")
- elif motion_target.direction == c4002.APPROACHING:
- print("靠近!")
- elif motion_target.direction == c4002.NODIRECTION:
- print("无方向!")
-
- # GPIO2 状态
- print("GPIO2 输出: {}".format(get_gpio2_status()))
-
- print("--------------------------------")
-
-
- def main():
- """主函数"""
- setup()
- while True:
- loop()
- time.sleep_ms(10)
-
-
- if __name__ == "__main__":
- main()
复制代码
运行后,可以在shell看到: - MPY: soft reboot
- C4002 初始化成功!
- 设置运行指示灯成功!
- 设置输出指示灯成功!
- 设置分辨率模式成功!
- 设置检测范围成功!
- 设置光照阈值成功!
- 启用运动距离门限成功!
- 启用存在距离门限成功!
- 设置上报周期成功!
- ------- 获取全部结果 --------
- 光照强度: 52.60 lux
- 目标状态: 静态存在
- 存在倒计时: 10 s
- 存在距离: 1.51 m
- 存在能量: 99
- 运动距离: 1.33 m
- 运动能量: 99
- 运动速度: 0.02 m/s
- 运动方向: 无方向!
- GPIO2 输出: LOW (未检测到目标)
复制代码
|