Example Code for Arduino-Get All Sensor Results
Last revision 2026/02/07
This project demonstrates how to use the DFRobot_C4002 library to retrieve all detection results from the C4002 sensor, including light intensity, target state (no target, static presence, motion), presence and motion target information (distance, energy, speed, direction), etc. Users will learn how to initialize the sensor, configure its parameters, and read various detection data.
Hardware Preparation
- DFRduino Leonardo (SKU:DFR0221)
- C4002 mmWave Motion & Static Presence Module(SKU:SEN0691)
Software Preparation
- Download Arduino IDE Click to download Arduino IDE
- Download the C4002 Library Click to download DFRobot_C4002
Wiring Diagram

Other Preparation Work
- Upload the following code to your Arduino board
- Open the Serial Monitor in Arduino IDE with a baud rate of 115200
- Ensure the sensor is placed in an environment where human presence or motion can be detected
Sample Code
/*!
* @file getAllResults.ino
* @brief This is an example to show how to use the DFRobot_C4002 library to get all the results of the C4002 sensor.
* @copyright Copyright (c) 2025 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author JiaLi([email protected])
* @version V1.0
* @date 2025-11-04
* @url https://github.com/DFRobot/DFRobot_C4002
*/
#include "DFRobot_C4002.h"
#if defined(ESP8266) || defined(ARDUINO_AVR_UNO)
SoftwareSerial mySerial(4, 5);
DFRobot_C4002 c4002(&mySerial, 115200);
#elif defined(ESP32)
DFRobot_C4002 c4002(&Serial1, 115200, /*D2*/ D2, /*D3*/ D3);
#else
DFRobot_C4002 c4002(&Serial1, 115200);
#endif
void setup()
{
Serial.begin(115200);
// Initialize the C4002 sensor
while (c4002.begin() != true) {
Serial.println("C4002 begin failed!");
delay(1000);
}
Serial.println("C4002 begin success!");
delay(50);
// Set the run led to off
if (c4002.setRunLedState(eLedOff)) {
Serial.println("Set run led success!");
} else {
Serial.println("Set run led failed!");
}
delay(50);
// Set the out led to off
if (c4002.setOutLedState(eLedOff)) {
Serial.println("Set out led success!");
} else {
Serial.println("Set out led failed!");
}
delay(50);
// Set the Resolution mode to 80cm.
if (c4002.setResolutionMode(eResolution80Cm)) {
Serial.println("Set resolution mode success!");
} else {
Serial.println("Set resolution mode failed!");
}
delay(50);
/**
* Note:
* 1. eResolution80Cm: This indicates that the resolution of the "distance gate" is 80cm.
* With a resolution of 80 cm, it supports up to 15 distance gates, with a maximum distance of 11.6 meters.
* 2. eResolution20Cm: This indicates that the resolution of the "distance gate" is 20cm.
* With a resolution of 20 cm, it supports up to 25 distance gates, with a maximum distance of 4.9 meters
*/
// Set the detect range to 0-1100 cm
uint16_t clostRange = 0;
uint16_t farRange = 1100;
if (c4002.setDetectRange(clostRange, farRange)) { // Max detect range(0-1100cm)
Serial.println("Set detect range success!");
} else {
Serial.println("Set detect range failed!");
}
delay(50);
// Set the light threshold to 0 lux.range: 0-50 lux
if (c4002.setLightThresh(0)) {
Serial.println("Set light threshold success!");
} else {
Serial.println("Set light threshold failed!");
}
delay(50);
uint8_t gateState[15] = { C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE, C4002_ENABLE };
// Resolution mode:eResolution20Cm,This means that the number of 'distance gates' we can operate is 25
// uint8_t gateState[25] = {C4002_DISABLE,C4002_DISABLE,C4002_ENABLE,...,C4002_ENABLE,C4002_DISABLE};
if (c4002.configureGate(eMotionDistGate, gateState)) { // Operation motion distance gate
Serial.println("Enable motion distance gate success!");
}
delay(50);
if (c4002.configureGate(ePresenceDistGate, gateState)) { // Operation presence distance gate
Serial.println("Enable presence distance gate success!");
}
delay(50);
// Set the target disappear delay time to 1s,range: 0-65535s
if (c4002.setTargetDisappearDelay(1)) {
Serial.println("Set target disappear delay time success!");
} else {
Serial.println("Set target disappear delay time failed!");
}
delay(50);
// Set the report period to 10 * 0.1s = 1s
if (c4002.setReportPeriod(10)) {
Serial.println("Set report period success!");
} else {
Serial.println("Set report period failed!");
}
/* note: Calibration and obtaining all data must have a set cycle */
}
void loop()
{
// Get all the results of the C4002 sensor,Default loop execution
sRetResult_t retResult = c4002.getNoteInfo();
if (retResult.noteType == eResult) {
Serial.println("------- Get all results --------");
// get the light intensity
float light = c4002.getLightIntensity();
Serial.print("Light: ");
Serial.print(light);
Serial.println(" lux");
// get Target state
eTargetState_t targetState = c4002.getTargetState();
Serial.print("Target state: ");
if (targetState == eNoTarget) {
Serial.println("No Target");
} else if (targetState == ePresence) {
Serial.println("Static Presence");
} else if (targetState == eMotion) {
Serial.println("Motion");
}
// get presence count down
uint16_t presenceGateCount = c4002.getPresenceCountDown();
Serial.print("Presence distance gate count down: ");
Serial.print(presenceGateCount);
Serial.println(" s");
// get Presence distance gate target info
sPresenceTarget_t presenceTarget = c4002.getPresenceTargetInfo();
Serial.print("Presence distance: ");
Serial.print(presenceTarget.distance);
Serial.println(" m");
Serial.print("Presence energy: ");
Serial.println(presenceTarget.energy);
// get motion distance gate index
sMotionTarget_t motionTarget = c4002.getMotionTargetInfo();
Serial.print("Motion distance: ");
Serial.print(motionTarget.distance);
Serial.println(" m");
Serial.print("Motion energy: ");
Serial.println(motionTarget.energy);
Serial.print("Motion speed: ");
Serial.print(motionTarget.speed);
Serial.println(" m/s");
Serial.print("Motion direction: ");
if (motionTarget.direction == eAway) {
Serial.println("Away!");
} else if (motionTarget.direction == eNoDirection) {
Serial.println("No Direction!");
} else if (motionTarget.direction == eApproaching) {
Serial.println("Approaching!");
}
Serial.println("--------------------------------");
}
delay(50);
}
Result

Was this article helpful?
