Components and supplies
Arduino Micro
Apps and platforms
Arduino.
rogerbit.
Project description
Code
Source code
cpp
1/* 2 * Sketch example for reporting LD2410 readings using the currently configured setup. 3 * 4 * This has been tested on the following platforms... 5 * 6 * On ESP32, connect LD2410 to GPIO pins 32 and 33 7 * On ESP32S2, connect LD2410 to GPIO pins 8 and 9 8 * On ESP32C3, connect LD2410 to GPIO pins 4 and 5 9 * On Arduino Leonardo or other ATmega32u4 boards, connect LD2410 to the hardware serial TX and RX pins 10 * 11 * Serial configuration for other boards will vary and should be assigned by yourself 12 * 13 * There's no example for ESP8266 as it only has one usable UART and won't boot if alternative UART pins are used for the radar. 14 * 15 * For this sketch and other examples to be useful, the board must have two usable UARTs. 16 * 17 */ 18int redPin = 11; // Red pin 19int greenPin = 9; // Green pin 20int bluePin = 10; // Blue pin 21byte com = 0; 22#if defined(ESP32) 23 #ifdef ESP_IDF_VERSION_MAJOR // IDF 4+ 24 #if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 25 #define MONITOR_SERIAL Serial 26 #define RADAR_SERIAL Serial1 27 #define RADAR_RX_PIN 32 28 #define RADAR_TX_PIN 33 29 #elif CONFIG_IDF_TARGET_ESP32S2 30 #define MONITOR_SERIAL Serial 31 #define RADAR_SERIAL Serial1 32 #define RADAR_RX_PIN 9 33 #define RADAR_TX_PIN 8 34 #elif CONFIG_IDF_TARGET_ESP32C3 35 #define MONITOR_SERIAL Serial 36 #define RADAR_SERIAL Serial1 37 #define RADAR_RX_PIN 4 38 #define RADAR_TX_PIN 5 39 #else 40 #error The CONFIG_IDF_TARGET target is not supported 41 #endif 42 #else // ESP32 before IDF 4.0 43 #define MONITOR_SERIAL Serial 44 #define RADAR_SERIAL Serial1 45 #define RADAR_RX_PIN 32 46 #define RADAR_TX_PIN 33 47 #endif 48#elif defined(__AVR_ATmega32U4__) 49 #define MONITOR_SERIAL Serial 50 #define RADAR_SERIAL Serial1 51 #define RADAR_RX_PIN 0 52 #define RADAR_TX_PIN 1 53#endif 54#include <ld2410.h> 55ld2410 radar; 56uint32_t lastReading = 0; 57bool radarConnected = false; 58void setup(void) 59{ 60 pinMode(redPin, OUTPUT); // Set redPin as an output 61 pinMode(greenPin, OUTPUT); // Set greenPin as an output 62 pinMode(bluePin, OUTPUT); // Set bluePin as an output 63 MONITOR_SERIAL.begin(115200); // Serial Monitor feedback 64 // radar.debug(MONITOR_SERIAL); // Uncomment to show library debug info on Serial Monitor. By default, this does not show sensor readings as they are very frequent. 65 #if defined(ESP32) 66 RADAR_SERIAL.begin(256000, SERIAL_8N1, RADAR_RX_PIN, RADAR_TX_PIN); // UART for radar monitoring 67 #elif defined(__AVR_ATmega32U4__) 68 RADAR_SERIAL.begin(256000); // UART for radar monitoring 69 #endif 70 delay(500); 71 MONITOR_SERIAL.print(F("\nConnect LD2410 radar TX to GPIO:")); 72 MONITOR_SERIAL.println(RADAR_RX_PIN); 73 MONITOR_SERIAL.print(F("Connect LD2410 radar RX to GPIO:")); 74 MONITOR_SERIAL.println(RADAR_TX_PIN); 75 MONITOR_SERIAL.print(F("Initializing LD2410 radar sensor: ")); 76 if(radar.begin(RADAR_SERIAL)) 77 { 78 MONITOR_SERIAL.println(F("OK")); 79 MONITOR_SERIAL.print(F("LD2410 firmware version: ")); 80 MONITOR_SERIAL.print(radar.firmware_major_version); 81 MONITOR_SERIAL.print('.'); 82 MONITOR_SERIAL.print(radar.firmware_minor_version); 83 MONITOR_SERIAL.print('.'); 84 MONITOR_SERIAL.println(radar.firmware_bugfix_version, HEX); 85 } 86 else 87 { 88 MONITOR_SERIAL.println(F("not connected")); 89 } 90} 91void loop() 92{ 93 radar.read(); 94 if(radar.isConnected() && millis() - lastReading > 1000) // Report every 1000ms 95 { 96 lastReading = millis(); 97 if(radar.presenceDetected()) 98 { 99 if(radar.stationaryTargetDetected())//If a stationary target is detected, it turns blue 100 { 101 Serial.print(F("Stationary Target BLUE LIGHT: ")); 102 Serial.print(radar.stationaryTargetDistance()); 103 Serial.print(F("cm energy:")); 104 Serial.print(radar.stationaryTargetEnergy()); 105 Serial.println(' '); 106 color(0, 0, 255); // RGB LED as blue 107 } 108 if(radar.movingTargetDetected())//If a moving target is detected, it turns red 109 { 110 Serial.print(F("MOVING TARGET DETECTED RED LIGHT!: ")); 111 Serial.print(radar.movingTargetDistance()); 112 Serial.print(F("cm energy:")); 113 Serial.print(radar.movingTargetEnergy()); 114 color(255, 0, 0);// RGB LED as red 115 } 116 Serial.println(); 117 } 118 else 119 { 120 Serial.println(F("No Target GREEN LIGHT"));//No target in range, turns green 121 color(0,255, 0);// RGB LED as green 122 } 123 } 124} 125//Function for generating color 126void color (unsigned char red, unsigned char green, unsigned char blue) 127{ 128analogWrite(redPin, red*102/255); 129analogWrite(bluePin, blue*173/255); 130analogWrite(greenPin, green*173/255); 131}
Comments
Only logged in users can leave comments
shanren
5 months ago
good job. My testing got LD2410 keep output HIGH, no matter where it face to, how? thanks.
carlosvolt
8 Followers
•0 Projects
5
2
bob-easyeda
2 months ago
Hi, your project is so amazing! Are you interested in OSHWLab Stars :https://oshwlab.com/activities/spark2023 we are offering up to $5000 in coupons for each entry to cover all your fabrication costs.