print_parameters.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. This program prints the current 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. void printValue(const byte &val) {
  44. Serial.print(' ');
  45. Serial.print(val);
  46. }
  47. void printParameters() {
  48. Serial.print("Firmware: ");
  49. Serial.println(sensor.getFirmware());
  50. Serial.print("Protocol version: ");
  51. Serial.println(sensor.getVersion());
  52. Serial.print("Bluetooth MAC address: ");
  53. Serial.println(sensor.getMACstr());
  54. const MyLD2410::ValuesArray &mThr = sensor.getMovingThresholds();
  55. const MyLD2410::ValuesArray &sThr = sensor.getStationaryThresholds();
  56. Serial.print("Resolution (gate-width): ");
  57. Serial.print(sensor.getResolution());
  58. Serial.print("cm\nMax range: ");
  59. Serial.print(sensor.getRange_cm());
  60. Serial.print("cm\nMoving thresholds [0,");
  61. Serial.print(mThr.N);
  62. Serial.print("]:");
  63. //Print using global function
  64. mThr.forEach(printValue);
  65. Serial.print("\nStationary thresholds[0,");
  66. Serial.print(sThr.N);
  67. Serial.print("]:");
  68. //Print using lambda
  69. sThr.forEach([](const byte &val) {
  70. Serial.print(' ');
  71. Serial.print(val);
  72. });
  73. Serial.print("\nNo-one window: ");
  74. Serial.print(sensor.getNoOneWindow());
  75. Serial.println('s');
  76. }
  77. void setup() {
  78. Serial.begin(SERIAL_BAUD_RATE);
  79. #if defined(ARDUINO_XIAO_ESP32C3) || defined(ARDUINO_XIAO_ESP32C6) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_LEONARDO)
  80. sensorSerial.begin(LD2410_BAUD_RATE);
  81. #else
  82. sensorSerial.begin(LD2410_BAUD_RATE, SERIAL_8N1, RX_PIN, TX_PIN);
  83. #endif
  84. delay(2000);
  85. Serial.println(__FILE__);
  86. if (!sensor.begin()) {
  87. Serial.println("Failed to communicate with the sensor.");
  88. while (true) {}
  89. }
  90. printParameters();
  91. delay(2000);
  92. Serial.println("Done!");
  93. }
  94. void loop() {
  95. }