Hi,
I am having an issue getting this radar sensor to work with an Arduino.
I have tried the DFRbot example code but without success.
Does anyone have any advice?
Hi,
I am having an issue getting this radar sensor to work with an Arduino.
I have tried the DFRbot example code but without success.
Does anyone have any advice?
Hi @Samuel282450, welcome to the forums!
I had a few questions regarding your project,
Could you link the exact product you’re using? We offer multiple DFR motion sensors but the specific one you seem to be referring to is a Waveshare product. I assume I’m missing something .
Could you send pictures of your wiring and physical setup? That way we can double check everything has been connected correctly.
Do you receive any errors when running the example code? Sending these errors/snippets of your code could be super helpful.
Hopefully, I can offer a bit more support once you send those through!
I am using the waveshare product. Human Micro-Motion Detection mmWave Sensor | Buy in Australia | WS-26536 | Core Electronics
#include <SoftwareSerial.h>
SoftwareSerial radarSerial(2, 3); // RX, TX for Arduino Uno
const int numReadings = 5; // Number of readings for the moving average
float readings[numReadings]; // Array to hold readings
int readIndex = 0; // Current index
float total = 0; // Total of the readings
float average = 0; // Average value
void setup() {
Serial.begin(9600);
radarSerial.begin(115200);
Serial.println("Radar initialized.");
// Initialize readings array
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
}
void loop() {
if (radarSerial.available()) {
String radarData = radarSerial.readStringUntil('\n');
radarData.trim();
if (radarData.endsWith("ON")) {
int rangeIndex = radarData.indexOf("Range ");
if (rangeIndex != -1) {
int endIndex = radarData.indexOf("ON", rangeIndex);
if (endIndex != -1) {
// Extract distance string and trim it
String distanceStr = radarData.substring(rangeIndex + 6, endIndex);
distanceStr.trim(); // Trim any whitespace
float distance = distanceStr.toFloat();
// Update the readings array for smoothing
total -= readings[readIndex]; // Subtract the last reading
readings[readIndex] = distance; // Add the new reading
total += readings[readIndex]; // Add the new reading
readIndex = (readIndex + 1) % numReadings; // Move to the next index
average = total / numReadings; // Calculate the average
// Print the smoothed distance
Serial.print("Smoothed presence detected at distance: ");
Serial.println(average);
} else {
Serial.println("Distance data not found.");
}
} else {
Serial.println("Presence detected, distance unknown.");
}
} else {
Serial.println("No presence.");
}
}
delay(500);
}
I found an example code. I now want to change the detection distance, and if detection is positive, turn on an LED. Also, the serial monitor output is somewhat like “Range 279,” so I want to change the number to meters. The wiki for this sensor (https://www.waveshare.com/wiki/HMMD_mmWave_Sensor#Software) says that there are 15 gates, and the distance is 0.7cm.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 4); // RX, TX
void setup() {
// Start the serial communication with a baud rate of 115200
Serial.begin(115200);
mySerial.begin(115200);
// Wait for the serial port to initialize
while (!Serial) {
delay(100);
}
// Hex string to send
String hex_to_send = "FDFCFBFA0800120000006400000004030201";
sendHexData(hex_to_send);
}
void loop() {
// Read and print serial data
readSerialData();
}
void sendHexData(String hexString) {
// Convert hex string to bytes
int hexStringLength = hexString.length();
byte hexBytes[hexStringLength / 2];
for (int i = 0; i < hexStringLength; i += 2) {
hexBytes[i / 2] = strtoul(hexString.substring(i, i + 2).c_str(), NULL, 16);
}
// Send bytes through software serial
mySerial.write(hexBytes, sizeof(hexBytes));
}
void readSerialData() {
// Read and print data from software serial
while (mySerial.available() > 0) {
char incomingByte = mySerial.read();
Serial.print(incomingByte);
}
}
From what I can tell the following section is reads the serial from the sensor and prints that to the output serial monitor.
You’ll need to add some code to isolate the number that is being returned in the “Range XXX” output and then do some maths on that to convert it to a meter value.
Once you have that you’ll be able to create a loop that checks if the meter value is within the range required to turn on the LED.
Hi,
Thanks, this worked ^^^.
I was unable to change the range. I bought a USRT to USB that communicates with the device’s configuration app, however, nothing I change seems to make a difference the target range is always 2.8m.
Does anyone have any thoughts?
Hi @Samuel282450,
I take it there’s no change to the range at all even if the sensor is moved?