Regarding C4001 Presence Detection Sensor

userHead jrodrigues9 2024-05-07 17:13:28 67 Views0 Replies

I am working on a project using the C4001 human presence detection sensor, to which extent I was using the library written by Zhixin Liu.

 

I m using the DFRobot Firebeetle ESP32S3 with the Arduino framework alongside PlatformIO to add multiple presence detection sensors which would provide us with readings for presence detected within a certain range. I used I2C initially, which worked really well. However, I was unable to get it working with UART for some reason. I made use of the starter code provided with minimal changes made to it. I will attach the code at the end of this post; in case you would like to double check what I have written. In summary, when I open the serial monitor from within VSCode, I don't get any form of output. In fact, this seems to happen whenever I try to call any of the radar's written functions.

 

Would you be able to advise on what I am doing wrong, so I can fix the associated issues?

 

Looking forward to hearing from  you. Many thanks in advance for your time and consideration.

 

Kind regards

 

#include "DFRobot_C4001.h"


 

//#define I2C_COMMUNICATION //use I2C for communication, but use the serial port for communication if the line of codes were masked


 

#ifdef I2C_COMMUNICATION

   const uint8_t RadarAddress = 0x2A;     //default iic_address

   /*

   * DEVICE_ADDR_1 = 0x2B

   */

    DFRobot_C4001_I2C radar(&Wire, RadarAddress);

#else

/* ---------------------------------------------------------------------------------------------------------------------

 *    board   |             MCU                | Leonardo/Mega2560/M0 |    UNO    | ESP8266 | ESP32 |  microbit  |   m0  |

 *     VCC    |            3.3V/5V             |        VCC           |    VCC    |   VCC   |  VCC  |     X      |  vcc  |

 *     GND    |              GND               |        GND           |    GND    |   GND   |  GND  |     X      |  gnd  |

 *     RX     |              TX                |     Serial1 TX1      |     5     |   5/D6  |  D2   |     X      |  tx1  |

 *     TX     |              RX                |     Serial1 RX1      |     4     |   4/D7  |  D3   |     X      |  rx1  |

 * ----------------------------------------------------------------------------------------------------------------------*/

/* Baud rate cannot be changed */

#if defined(ARDUINO_AVR_UNO) || defined(ESP8266)

SoftwareSerial mySerial(4, 5);

DFRobot_C4001_UART radar(&mySerial, 9600);

#elif defined(ESP32)

DFRobot_C4001_UART radar1(&Serial1, 9600, /*rx*/ D2, /*tx*/ D3);

#else

DFRobot_C4001_UART radar(&Serial1, 9600);

#endif

#endif


 

void setup() {

  Serial.begin(115200);

  delay(2500);

  testSerial.begin(9600);


 

  while (!radar.begin()) {

    Serial.println("NO Deivces !");

    delay(1000);

  }

  Serial.println("Device connected!");


 

  // speed Mode

  radar.setSensorMode(eSpeedMode);


 

  sSensorStatus_t data;

  data = radar.getStatus();

  //  0 stop  1 start

  Serial.print("work status  = ");

  Serial.println(data.workStatus);


 

  //  0 is exist   1 speed

  Serial.print("work mode  = ");

  Serial.println(data.workMode);


 

  //  0 no init    1 init success

  Serial.print("init status = ");

  Serial.println(data.initStatus);

  Serial.println();


 

  /*

   * min Detection range Minimum distance, unit cm, range 0.3~20m (30~2500), not exceeding max, otherwise the function is abnormal.

   * max Detection range Maximum distance, unit cm, range 2.4~20m (240~2500)

   * thres Target detection threshold, dimensionless unit 0.1, range 0~6553.5 (0~65535)

   */

  if (radar.setDetectThres(/*min*/ 11, /*max*/ 1200, /*thres*/ 10)) {

    Serial.println("set detect threshold successfully");

  }


 

  // set Fretting Detection

  radar.setFrettingDetection(eON);


 

  // get confige params

  Serial.print("min range = ");

  Serial.println(radar.getTMinRange());

  Serial.print("max range = ");

  Serial.println(radar.getTMaxRange());

  Serial.print("threshold range = ");

  Serial.println(radar.getThresRange());

  Serial.print("fretting detection = ");

  Serial.println(radar.getFrettingDetection());

}


 

void loop() {

  Serial.print("target number = ");

  Serial.println(radar.getTargetNumber());  // must exist

  Serial.print("target Speed  = ");

  Serial.print(radar.getTargetSpeed());

  Serial.println(" m/s");


 

  Serial.print("target range  = ");

  Serial.print(radar.getTargetRange());

  Serial.println(" m");


 

  Serial.print("target energy  = ");

  Serial.println(radar.getTargetEnergy());

  Serial.println();

  delay(100);

}