Microwave Human Detection Radar
What You Need to Build This Project
ESP32
LD2450 Microwave Human Detection Radar
Battery
3D-printable enclosure (STL files provided below)
How to Run This Project:
Open the R1BBIT Cloud platform.
Connect your ESP32 and flash it with R1BBIT firmware.
Copy the project script into the code editor.
Upload the script to your ESP32 and run it.
`// LD2450 radar on a Freenove 2.8: a top-down view of the whole field of view.
//
// Wiring: the radar's transmit to GPIO 44 (that is the board's receive), the radar's receive to
// GPIO 43. The second wire is needed for the mode-change command; for reading
// alone the first would be enough. Power 5 V. The console on this board goes to USB,
// so both pins are free for UART1. The rate 256000 is the factory setting.
//
// The module mounts vertically: its antennas sit in a row, and it measures angle
// only in the plane of that row. Laid flat, it resolves a sideways step
// into a tilt it cannot see, and only range is left.
//
// The screen is a top-down view. The sensor is bottom centre, arcs every metre, a 120-degree
// sector, six metres of range - the module's maximum. The dot grows larger the
// closer the target: distance is then visible even out of the corner of the eye.
CONFIG
PSRAM_SIZE 8
PSRAM_TYPE OCTAL
UART1_RX 44
UART1_TX 43
UART1_BAUD 256000
// The codec sits on the same I2C as the touch panel.
I2C_SDA 16
I2C_SCL 15
I2C_FREQ 400000
I2S_BCLK 5
I2S_WS 7
I2S_DOUT 8
I2S_DIN 6
I2S_MCLK 4
I2S_FREQ 16000
I2S_BITS 16
I2S_MODE PHILIPS
I2S_SLOTS STEREO
CODEC ES8311
CODEC_ADDR 0x18
CODEC_I2C 0
CODEC_I2S 0
CODEC_PA_PIN 1
CODEC_PA_ACTIVE_LOW 1
AUDIO_OUT CODEC
SPI_MOSI 11
SPI_MISO 13
SPI_CLK 12
SPI_FREQ 60000000
DISPLAY_DRIVER ILI9341
DISPLAY_CS 10
DISPLAY_DC 46
DISPLAY_RST -1
DISPLAY_BL 45
DISPLAY_BL_ACTIVE_LOW 0
DISPLAY_WIDTH 320
DISPLAY_HEIGHT 240
DISPLAY_INVERT 1
DISPLAY_BGR 1
DISPLAY_FREQ 60000000
DISPLAY_SWAP_XY 1
END_CONFIG
DISPLAY_INIT()
DISPLAY_BRIGHTNESS(100)
SCREEN_COLOR(#070B18)
AUDIO_VOLUME(70)
VAR CNT
VAR X[3], Y[3], SP[3]
VAR
VAR I, PX, PY, PCNT, SZ
VAR MOV[3], WAS[3], COL[3]
VAR TX[3], TY[3], TV[3], HOLD[3]
VAR NT, TMP
VAR NMOV, PMOV, B
VAR R, M
VAR DIM, CARD, EDGE, FIELD, GRID
VAR C0, C1, C2, MOVING
VAR XDIR
VAR X1,X2,X3,Y1,Y2,Y3
DIM = 0x7C8AB5
CARD = 0x111A33
EDGE = 0x1B2544
FIELD = 0x05080F
GRID = 0x24406B
C0 = 0x7CFF9B
C1 = 0x7FD4FF
C2 = 0xFFC46B
MOVING = 0xFF3B30
COL[0] = C0
COL[1] = C1
COL[2] = C2
// Horizontal mirroring. The radar faces you, so its left side
// is your right. Set 1 if the radar faces the same way you do.
XDIR = -1
// --- field of view ---
// Scale: 172 points to 6000 mm, that is 35 mm per point. A 120-degree sector at
// six metres is 10.4 m wide, which in points is 149 each way - exactly
// what there is either side of the sensor, so the far arc fits entirely.
DISPLAY_WIDGET(1, "panel", 4, 4, 312, 188, CARD)
UI_RADIUS(1, 10)
UI_BORDER(1, 1)
UI_COLOR(1, "border", EDGE)
UI_PARENT(1)
DISPLAY_WIDGET(2, "panel", 4, 4, 302, 178, FIELD)
UI_RADIUS(2, 6)
UI_BORDER(2, 1)
UI_COLOR(2, "border", EDGE)
UI_PARENT(0)
UI_PARENT(2)
// Range arcs. An arc is drawn inside its own square with a radius of half that
// square's side, so the square is placed around the sensor. LVGL angles are counted from
// three o'clock clockwise, so a sector facing up is 210..330.
FOR M = 1 TO 6
R = M * 172 / 6
DISPLAY_WIDGET(39 + M, "arc", 151 - R, 176 - R, 2 * R, 2 * R)
UI_ANGLES(39 + M, 210, 330)
UI_LINEWIDTH(39 + M, 1)
UI_COLOR(39 + M, GRID)
UI_COLOR(39 + M, "indicator", FIELD)
UI_COLOR(39 + M, "knob", FIELD)
DISPLAY_WIDGET(49 + M, "label", 155, 178 - R, 26, 14, M + "m", 11, GRID)
NEXT M
// Target markers: hidden with UI_VISIBLE, sized by distance.
DISPLAY_WIDGET(10, "panel", 0, 0, 10, 10, C0)
UI_RADIUS(10, 5)
UI_VISIBLE(10, 0)
DISPLAY_WIDGET(11, "panel", 0, 0, 10, 10, C1)
UI_RADIUS(11, 5)
UI_VISIBLE(11, 0)
DISPLAY_WIDGET(12, "panel", 0, 0, 10, 10, C2)
UI_RADIUS(12, 5)
UI_VISIBLE(12, 0)
DISPLAY_WIDGET(13, "panel", 148, 173, 6, 6, DIM)
UI_RADIUS(13, 3)
DISPLAY_WIDGET(14, "label", 6, 4, 110, 16, "TARGETS 0", 12, DIM)
UI_PARENT(0)
// --- three cards at the bottom, one per target ---
DISPLAY_WIDGET(20, "panel", 4, 196, 100, 40, CARD)
UI_RADIUS(20, 8)
UI_BORDER(20, 1)
UI_COLOR(20, "border", EDGE)
UI_PARENT(20)
DISPLAY_WIDGET(23, "label", 6, 2, 88, 14, "1st, mm", 11, DIM)
DISPLAY_WIDGET(24, "label", 6, 16, 88, 20, "--", 16, C0)
UI_PARENT(0)
DISPLAY_WIDGET(21, "panel", 110, 196, 100, 40, CARD)
UI_RADIUS(21, 8)
UI_BORDER(21, 1)
UI_COLOR(21, "border", EDGE)
UI_PARENT(21)
DISPLAY_WIDGET(25, "label", 6, 2, 88, 14, "2nd, mm", 11, DIM)
DISPLAY_WIDGET(26, "label", 6, 16, 88, 20, "--", 16, C1)
UI_PARENT(0)
DISPLAY_WIDGET(22, "panel", 216, 196, 100, 40, CARD)
UI_RADIUS(22, 8)
UI_BORDER(22, 1)
UI_COLOR(22, "border", EDGE)
UI_PARENT(22)
DISPLAY_WIDGET(27, "label", 6, 2, 88, 14, "3rd, mm", 11, DIM)
DISPLAY_WIDGET(28, "label", 6, 16, 88, 20, "--", 16, C2)
UI_PARENT(0)
LD2450_INIT("UART1", CNT, X[], Y[], SP[], 100)
// The module can track one target or three, and that is its own setting, not
// ours: in single mode it returns one track, however many people stand in front
// of it. The firmware checks the module's reply, so an error here would mean
// the command never reached it.
LD2450_MODE("UART1", MULTI)
PCNT = -1
PMOV = 0
HOLD[0] = 9
HOLD[1] = 9
HOLD[2] = 9
WAS[0] = -1
WAS[1] = -1
WAS[2] = -1
WHILE 1
X1 = X[0]/1000
X2 = X[1]/1000
X3 = X[2]/1000
Y1 = Y[0]/1000
Y2 = Y[1]/1000
Y3 = Y[2]/1000
//PRINT "X="+X1+" Y="+Y1
// A slot in a radar frame is a place in a table, not a person: from frame to
// frame the people in it swap places. Drawing a marker by slot number made it
// jump from one person to another.
NT = 0
FOR I = 0 TO 2
IF Y[I] <> 0 THEN
TX[NT] = X[I]
TY[NT] = Y[I]
TV[NT] = SP[I]
NT = NT + 1
ENDIF
NEXT I
// We order them left to right: the leftmost person is always the first marker,
// and a reshuffle in the radar's table is invisible on screen.
IF NT > 1 THEN
IF TX[0] > TX[1] THEN
TMP = TX[0]
TX[0] = TX[1]
TX[1] = TMP
TMP = TY[0]
TY[0] = TY[1]
TY[1] = TMP
TMP = TV[0]
TV[0] = TV[1]
TV[1] = TMP
ENDIF
ENDIF
IF NT > 2 THEN
IF TX[1] > TX[2] THEN
TMP = TX[1]
TX[1] = TX[2]
TX[2] = TMP
TMP = TY[1]
TY[1] = TY[2]
TY[2] = TMP
TMP = TV[1]
TV[1] = TV[2]
TV[2] = TMP
ENDIF
IF TX[0] > TX[1] THEN
TMP = TX[0]
TX[0] = TX[1]
TX[1] = TMP
TMP = TY[0]
TY[0] = TY[1]
TY[1] = TMP
TMP = TV[0]
TV[0] = TV[1]
TV[1] = TMP
ENDIF
ENDIF
IF CNT <> PCNT THEN
PCNT = CNT
UI_TEXT(14, "TARGETS " + CNT)
ENDIF
FOR I = 0 TO 2
IF I < NT THEN
HOLD[I] = 0
// Size by distance: 12 points up close, 5 at six metres.
SZ = 12 - 7 * TY[I] / 6000
IF SZ < 5 THEN
SZ = 5
ENDIF
IF SZ > 12 THEN
SZ = 12
ENDIF
SZ = ROUND(SZ)
// Millimetres to points: 172 points to 6000 mm, the same on both
// axes. The sensor is at 151, 176. We subtract half the marker so its
// centre lands on the target's coordinate.
PX = 151 + XDIR * TX[I] * 172 / 6000 - SZ / 2
PY = 176 - TY[I] * 172 / 6000 - SZ / 2
IF PX < 0 THEN
PX = 0
ENDIF
IF PX > 302 - SZ THEN
PX = 302 - SZ
ENDIF
IF PY < 0 THEN
PY = 0
ENDIF
IF PY > 178 - SZ THEN
PY = 178 - SZ
ENDIF
UI_SIZE(10 + I, SZ, SZ)
UI_RADIUS(10 + I, ROUND(SZ / 2))
UI_MOVE(10 + I, ROUND(PX), ROUND(PY))
UI_VISIBLE(10 + I, 1)
UI_TEXT(24 + I * 2, ROUND(TX[I]) + "," + ROUND(TY[I]))
// A red dot means the target is moving. The radar gives radial speed in
// cm/s: for someone standing it is zero or jitters around zero, for someone walking
// it is tens. We rewrite the colour only when the state changes.
MOV[I] = 0
IF ABS(TV[I]) >= 5 THEN
MOV[I] = 1
ENDIF
IF MOV[I] <> WAS[I] THEN
WAS[I] = MOV[I]
IF MOV[I] = 1 THEN
UI_COLOR(10 + I, "bg", MOVING)
ELSE
UI_COLOR(10 + I, "bg", COL[I])
ENDIF
ENDIF
ELSE
// The target is gone. We do not hide it at once: the radar does now and then lose a track for
// a frame or two, and a blinking marker reads worse than a lingering one.
HOLD[I] = HOLD[I] + 1
IF HOLD[I] = 4 THEN
UI_VISIBLE(10 + I, 0)
UI_TEXT(24 + I * 2, "--")
WAS[I] = -1
MOV[I] = 0
ENDIF
ENDIF
NEXT I
// As many beeps as there are targets moving right now. Only on a change of number:
// otherwise a walking person would sound on every frame.
NMOV = MOV[0] + MOV[1] + MOV[2]
IF NMOV <> PMOV THEN
PMOV = NMOV
IF NMOV > 0 THEN
FOR B = 1 TO NMOV
BEEP(1200, 70, 100)
PAUSE 60
NEXT B
ENDIF
ENDIF
PAUSE 120
WEND`
1
0 replies