LD2410 presence radar on an Arduino Mega
A Hi-Link LD2410 mmWave radar on the Mega’s Serial1 at 256000 baud: the ld2410 library tells a moving person from a still one, and OUT lights the LED. The whole build, an Arduino Mega and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $52. On Mokxi they're free.
What you'll learn
- Sensors
- IoT
- Using the Presence radar
- Difficulty: Intermediate
- Time: About 10 minutes
- Board: Arduino Mega 2560 R3
- Parts: 3
How it works
A Hi-Link LD2410 mmWave radar on the Mega’s Serial1 at 256000 baud: the ld2410 library tells a moving person from a still one, and OUT lights the LED.
Arduino Mega 2560 R3 runs the sketch below. The circuit around it: Presence radar, LD2410. pinMode sets OUT_PIN as an input. digitalRead reads whether OUT_PIN is HIGH or LOW. delay waits 500 milliseconds. Serial.begin opens the serial port at 115200 baud. millis reads the time since power-up, so the sketch can time things without stopping.
Parts list
3 parts, plus the jumper wires. They retail for about $52; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Presence radar, LD2410 pin GND | Arduino Mega 2560 R3 pin GND |
| Presence radar, LD2410 pin OUT | Arduino Mega 2560 R3 pin 2 |
| Presence radar, LD2410 pin RX | Arduino Mega 2560 R3 pin 18 |
| Presence radar, LD2410 pin TX | Arduino Mega 2560 R3 pin 19 |
| Presence radar, LD2410 pin VCC | 5 V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// LD2410 presence radar on a Mega: a Hi-Link 24 GHz mmWave radar on Serial1.
//
// LD2410 TX -> RX1 (19) LD2410 RX <- TX1 (18) OUT -> pin 2
// VCC -> 5 V GND -> GND
//
// A PIR sees only movement, so the lights go out on someone reading on the
// couch. This radar also sees a person sitting still, by their breathing, and
// tells the two apart. It talks at 256000 baud, too fast for SoftwareSerial,
// so it is on the Mega's second hardware serial port. (At 16 MHz the nearest
// rate the Mega can make is 250000, 2.3 % slow, which the radar still reads.)
//
// Its OUT pin is high while someone is there, and stays high for the no-one
// duration (5 s from the factory) after they leave; the sketch copies it to
// the LED on pin 13. The ld2410 library reads the reports for the details.
//
// Drag the sliders on the radar: who is there, and how far.
#include <ld2410.h>
ld2410 radar;
const int OUT_PIN = 2;
unsigned long lastPrint = 0;
void setup() {
Serial.begin(115200);
Serial1.begin(256000);
pinMode(OUT_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
delay(500);
Serial.print("LD2410 radar: ");
if (radar.begin(Serial1)) {
Serial.print("firmware ");
Serial.print(radar.firmware_major_version);
Serial.print('.');
Serial.print(radar.firmware_minor_version);
Serial.print('.');
Serial.println(radar.firmware_bugfix_version, HEX);
} else {
Serial.println("not connected");
}
}
void loop() {
radar.read();
digitalWrite(LED_BUILTIN, digitalRead(OUT_PIN));
if (millis() - lastPrint < 1000) return;
lastPrint = millis();
if (!radar.isConnected()) {
Serial.println("No reports from the radar");
return;
}
if (!radar.presenceDetected()) {
Serial.println("Nobody here");
return;
}
if (radar.movingTargetDetected()) {
Serial.print("Moving at ");
Serial.print(radar.movingTargetDistance());
Serial.print(" cm (energy ");
Serial.print(radar.movingTargetEnergy());
Serial.print(") ");
}
if (radar.stationaryTargetDetected()) {
Serial.print("Still at ");
Serial.print(radar.stationaryTargetDistance());
Serial.print(" cm (energy ");
Serial.print(radar.stationaryTargetEnergy());
Serial.print(")");
}
Serial.println();
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(115200);
Serial1.begin(256000);
pinMode(OUT_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
delay(500);
Serial.print("LD2410 radar: ");
if (radar.begin(Serial1)) {
Serial.print("firmware ");
Serial.print(radar.firmware_major_version);
Serial.print('.');
Serial.print(radar.firmware_minor_version);
Serial.print('.');
Serial.println(radar.firmware_bugfix_version, HEX);
} else {
Serial.println("not connected");
}
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
radar.read();
digitalWrite(LED_BUILTIN, digitalRead(OUT_PIN));
if (millis() - lastPrint < 1000) return;
lastPrint = millis();
if (!radar.isConnected()) {
Serial.println("No reports from the radar");
return;
}
if (!radar.presenceDetected()) {
Serial.println("Nobody here");
return;
}
if (radar.movingTargetDetected()) {
Serial.print("Moving at ");
Serial.print(radar.movingTargetDistance());
Serial.print(" cm (energy ");
Serial.print(radar.movingTargetEnergy());
Serial.print(") ");
}
if (radar.stationaryTargetDetected()) {
Serial.print("Still at ");
Serial.print(radar.stationaryTargetDistance());
Serial.print(" cm (energy ");
Serial.print(radar.stationaryTargetEnergy());
Serial.print(")");
}
Serial.println();
// ... 1 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The upload fails or nothing runs. Arduino Mega 2560 R3
Under Tools, choose Arduino Mega or Mega 2560 as the board, then pick the right port. An Uno selection uploads the wrong build. - I2C, SPI, or serial pins are in the Uno spots and do not work. Arduino Mega 2560 R3
On the Mega, SDA is pin 20 and SCL is 21. SPI uses 50 to 53, not 10 to 13. Serial1 to Serial3 are on pins 19 to 14.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.