modify_parameters.ino 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. This program demonstrates how to modify the setup parameters
  3. of the HLK-LD2410 presence sensor.
  4. #define SERIAL_BAUD_RATE sets the serial monitor baud rate
  5. Communication with the sensor is handled by the
  6. "MyLD2410" library Copyright (c) Iavor Veltchev 2024
  7. Use only hardware UART at the default baud rate 256000,
  8. or change the #define LD2410_BAUD_RATE to match your sensor.
  9. For ESP32 or other boards that allow dynamic UART pins,
  10. modify the RX_PIN and TX_PIN defines
  11. Connection diagram:
  12. Arduino/ESP32 RX -- TX LD2410
  13. Arduino/ESP32 TX -- RX LD2410
  14. Arduino/ESP32 GND -- GND LD2410
  15. Provide sufficient power to the sensor Vcc (200mA, 5-12V)
  16. */
  17. #if defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_LEONARDO)
  18. //ARDUINO_SAMD_NANO_33_IOT RX_PIN is D1, TX_PIN is D0
  19. //ARDUINO_AVR_LEONARDO RX_PIN(RXI) is D0, TX_PIN(TXO) is D1
  20. #define sensorSerial Serial1
  21. #elif defined(ARDUINO_XIAO_ESP32C3) || defined(ARDUINO_XIAO_ESP32C6)
  22. //RX_PIN is D7, TX_PIN is D6
  23. #define sensorSerial Serial0
  24. #elif defined(ESP32)
  25. //Other ESP32 device - choose available GPIO pins
  26. #define sensorSerial Serial1
  27. #if defined(ARDUINO_ESP32S3_DEV)
  28. #define RX_PIN 18
  29. #define TX_PIN 17
  30. #else
  31. #define RX_PIN 16
  32. #define TX_PIN 17
  33. #endif
  34. #else
  35. #error "This sketch only works on ESP32, Arduino Nano 33IoT, and Arduino Leonardo (Pro-Micro)"
  36. #endif
  37. // User defines
  38. #define SERIAL_BAUD_RATE 115200
  39. //Change the communication baud rate here, if necessary
  40. //#define LD2410_BAUD_RATE 256000
  41. #include "MyLD2410.h"
  42. MyLD2410 sensor(sensorSerial);
  43. bool restore = true;
  44. byte maxMovingGate, maxStationaryGate, noOneWindow;
  45. bool fineRes;
  46. MyLD2410::ValuesArray movingParameters, stationaryParameters;
  47. void printValue(const byte &val) {
  48. Serial.print(' ');
  49. Serial.print(val);
  50. }
  51. void printParameters(bool first = false) {
  52. if (first) {
  53. Serial.print("Firmware: ");
  54. Serial.println(sensor.getFirmware());
  55. Serial.print("Protocol version: ");
  56. Serial.println(sensor.getVersion());
  57. Serial.print("Bluetooth MAC address: ");
  58. Serial.println(sensor.getMACstr());
  59. }
  60. const MyLD2410::ValuesArray &mThr = sensor.getMovingThresholds();
  61. const MyLD2410::ValuesArray &sThr = sensor.getStationaryThresholds();
  62. Serial.print("Resolution (gate-width): ");
  63. Serial.print(sensor.getResolution());
  64. Serial.print("cm\nMax range: ");
  65. Serial.print(sensor.getRange_cm());
  66. Serial.print("cm\nMoving thresholds [0,");
  67. Serial.print(mThr.N);
  68. Serial.print("]:");
  69. mThr.forEach(printValue);
  70. Serial.print("\nStationary thresholds[0,");
  71. Serial.print(sThr.N);
  72. Serial.print("]:");
  73. sThr.forEach(printValue);
  74. Serial.print("\nNo-one window: ");
  75. Serial.print(sensor.getNoOneWindow());
  76. Serial.println('s');
  77. }
  78. void setup() {
  79. Serial.begin(SERIAL_BAUD_RATE);
  80. #if defined(ARDUINO_XIAO_ESP32C3) || defined(ARDUINO_XIAO_ESP32C6) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_LEONARDO)
  81. sensorSerial.begin(LD2410_BAUD_RATE);
  82. #else
  83. sensorSerial.begin(LD2410_BAUD_RATE, SERIAL_8N1, RX_PIN, TX_PIN);
  84. #endif
  85. delay(2000);
  86. Serial.println(__FILE__);
  87. if (!sensor.begin()) {
  88. Serial.println("Failed to communicate with the sensor.");
  89. while (true) {}
  90. }
  91. Serial.println("Current set of parameters");
  92. printParameters(true);
  93. fineRes = (sensor.getResolution() == 20);
  94. noOneWindow = sensor.getNoOneWindow();
  95. movingParameters = sensor.getMovingThresholds();
  96. stationaryParameters = sensor.getStationaryThresholds();
  97. delay(2000);
  98. //Set the no-one window parameter to 3 seconds
  99. Serial.println("\nSetting the no-one window to 3 seconds");
  100. if (sensor.setNoOneWindow(3)) printParameters();
  101. else {
  102. Serial.println("Fail");
  103. return;
  104. }
  105. delay(2000);
  106. //Change the resolution fine->coarse or coarse->fine
  107. Serial.print("\nSetting the resolution (gate-width) to ");
  108. Serial.print(((fineRes) ? 75 : 20));
  109. Serial.println("cm");
  110. if (sensor.setResolution(!fineRes)) printParameters();
  111. else {
  112. Serial.println("Fail");
  113. return;
  114. }
  115. delay(2000);
  116. //Set the maximum moving gate parameter to 4
  117. Serial.println("\nSetting the maximum moving gate parameter to 4");
  118. if (sensor.setMaxMovingGate(4)) printParameters();
  119. else {
  120. Serial.println("Fail");
  121. return;
  122. }
  123. delay(2000);
  124. //Set the maximum stationary gate parameter to 5
  125. Serial.println("\nSetting the maximum stationary gate parameter to 5");
  126. if (sensor.setMaxStationaryGate(5)) printParameters();
  127. else {
  128. Serial.println("Fail");
  129. return;
  130. }
  131. delay(2000);
  132. //Set the thresholds of gate 3 to 35 (moving) and 45 (stationary)
  133. Serial.println("\nSetting the thresholds of gate 3 to 35 (moving) and 45 (stationary)");
  134. if (sensor.setGateParameters(3, 35, 45)) printParameters();
  135. else {
  136. Serial.println("Fail");
  137. return;
  138. }
  139. Serial.println("\nSuccess!");
  140. }
  141. void loop() {
  142. if (restore) {
  143. restore = false;
  144. Serial.println("\nRestoring the original settings");
  145. delay(2000);
  146. if (sensor.setResolution(fineRes) && sensor.setGateParameters(movingParameters, stationaryParameters, noOneWindow)) {
  147. printParameters();
  148. Serial.println("Done!");
  149. } else {
  150. Serial.println("\nO-oh, something went wrong...");
  151. Serial.println("\nLoad the \"factory_reset\" sketch to restore the defaults...");
  152. }
  153. }
  154. }