In this guide, you’ll learn how to use the RCWL-0516 Microwave Radar Proximity sensor to detect motion with the Raspberry Pi Pico programmed using MicroPython firmware. We’ll show you how to wire the sensor and write a Python Script to detect when motion is detected. The RCWL-0516 is a great alternative to the popular PIR motion sensor.
New to the Raspberry Pi Pico? Read the following guide: Getting Started with Raspberry Pi Pico (and Pico W).
Table of Contents:
Throughout this tutorial, we’ll cover the following contents:
- Introducing the RCWL-0516 Microwave Radar Proximity Sensor
- RCWL-0516 Sensor Pinout
- Microwave Radar Proximity Sensor vs PIR Motion Sensor
- Wiring the RCWL-0516 Sensor to the Raspberry Pi Pico
- Raspberry Pi Pico with the RCWL-0516 Sensor – MicroPython Script
We have a similar tutorial using Raspberry Pi Pico with Arduino IDE: Raspberry Pi Pico: RCWL-0516 Microwave Radar Proximity Sensor (Arduino IDE).
Prerequisites – MicroPython Firmware
To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.
The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.
Introducing the RCWL-0516 Microwave Radar Proximity Sensor
The RCWL-0516 is a small, inexpensive sensor that uses microwave radar to detect the presence of moving objects. The sensor works by emitting a beam of microwaves and then detecting the Doppler shift in the reflected waves as objects move past.
Usually, these sensors are sold as a pack of five and don’t come with header pins. So, you may need to get header pins separately and then solder them yourself.
Where to Buy?
You can check the following link on Maker Advisor and compare the price in different stores.
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
How does it work?
The RCWL-0516 sensor has a built-in oscillator that generates a microwave signal at a frequency of 3.18 GHz. The sensor then sends out this signal in a 360-degree pattern.
When an object moves within the sensor’s range, the reflected waves are picked up by the sensor’s receiver. The receiver then measures the frequency of the reflected waves and compares it to the frequency of the original signal. If the frequency of the reflected waves has changed, the sensor knows that an object has moved.
The RCWL-0516 sensor has a single output pin that goes HIGH when it detects movement. It outputs LOW when no motion is detected.
RCWL-0516 Sensor Features
The RCWL-0516 has a detection range of up to 7 meters and can detect objects moving at speeds of up to 2 meters per second. It also has a built-in adjustable delay time, which can be used to prevent the sensor from triggering repeatedly on the same object.
Here’s a summary of some of the key features of the RCWL-0516 sensor:
- Uses microwave radar to detect moving objects
- Detection range of up to 7 meters
- Can detect objects moving at speeds of up to 2 meters per second
- Built-in adjustable delay time
- Low power consumption
- Inexpensive
RCWL-0516 sensor specifications:
- Supply voltage: 4–28 VDC
- Operating frequency: 3.18 GHz
- Sensing distance: 5–7 m
- Output level: 3.4V High <0.7 Low
- Output drive: 100mA
- Output timing: 2 second retrigger with motion
You can get more information about the sensor on the following GitHub page:
Optional Light Depend Resistor (LDR) Sensor
The sensor comes with the option to solder a light-depend resistor (light sensor) if you want your sensor to operate just in dark conditions, for example. You can get the output of the LDR sensor on the LDR pin. Alternatively, you can also connect the LDR to the CDS pin.
When the output of the LDR is bigger than 0.7V, the OUT pin will output a HIGH signal when motion is detected. If motion is detected but the output of the LDR is smaller than 0.7V, the output will be LOW. This means that when attaching an LDR, the sensor will only sense motion when it’s dark.
You can adjust the sensitivity of the LDR, by connecting a resistor on the R-CDS pads (see the following section), or by adding a pull-up resistor externally in parallel with the CDS pin.
In my case, I added a pull-up 22KOhm resistor to the LDR pin so that it could detect motion when there is low light. Without the resistor, not even in very dark conditions I had a positive output. You might need to try different resistance values to see which one works best for your scenario.
Adjustment components
At the back of the sensor, there are three pads for additional SMD components (0805 dimensions):
The following information was taken from this GitHub page.
- C-TM: Regulate the repeat trigger time. The default (unpopulated) time is 2s. An SMD capacitor to extend the repeat trigger time. Pin 3 of the IC emits a frequency (f), and the trigger time in seconds is given by (1/f) * 32678.
- R-GN: The default detection range is 7m, adding a 1M resistor reduces it to 5m.
- R-CDS: Resistor in parallel with the 1M pullup. Without R-CDS, the lowest resistance of the LDR (i.e. highest light level) where the output is enabled is ~269kΩ (=0.7V). Adding resistance here decreases the LDR resistance of the enable/disable threshold. If the LDR resistance at the desired light level threshold is <269k then you could add an external resistor in series with the LDR.
RCWL-0516 Microwave Radar Proximity Sensor Pinout
The RCWL-0516 microwave radar proximity sensor has five pins:
- 3V3: this is the output from the voltage regulator (not the power pin)
- VIN: this is the power input pin. The sensor can be powered by a voltage range of 4-28V.
- GND: this is the ground pin.
- OUT: this is the output pin. The output pin goes HIGH when the sensor detects movement and remains LOW when it doesn’t.
- CDS: This pin is used to connect a light-dependent resistor (LDR). The LDR can be used to disable the sensor in bright light conditions.
The following table shows the pinout of the RCWL-0516 microwave radar proximity sensor:
3V3 | 3.3V power output (not to power the sensor) |
GND | ground pin |
OUT | output pin (goes HIGH when motion is detected) |
VIN | input voltage to power the sensor (4V to 28V) |
CDS | Light-dependent resistor output |
Microwave Radar Proximity Sensor vs PIR Motion Sensor
The microwave radar proximity sensor is many times used as an alternative to the PIR motion sensor, depending on the project application. The following table compares both sensors:
RCWL-0516 Microwave Radar | PIR Motion Sensor | |
How it works? | Active Sensor (emits microwave signals and detects reflections). | Passive Sensor (detects infrared radiation emitted by objects). |
Detection Range | Longer range, typically up to 7+ meters. | Shorter range, typically a few meters, depending on the model. |
Sensing Through Obstacles | Can sense through non-metallic materials. | Obstructed by certain materials (e.g., glass) |
Sensitivity to Motion | Highly sensitive, may give false positives. | Not so sensitive, may miss subtle movements. Only detects living things that emit heat. |
Coverage Area | Broad coverage with wide radar pattern. | Narrow field of view. |
Connecting the RCWL-0516 Microwave Radar Proximity Sensor to the Raspberry Pi Pico
Follow the next table or schematic diagram to wire the RCWL-0516 microwave radar proximity sensor to the Raspberry Pi Pico:
RCWL-0516 Sensor | Raspberry Pi Pico |
3V3 | don’t connect |
GND | GND |
OUT | GPIO22 (or any other GPIO of your choice) |
VIN | VBUS (or preferably an external power supply between 4V to 28V) |
CDS | don’t connect |
We’ll also connect an LED to GPIO 21 to give us a visual output when motion is detected. You can connect the LED to any other digital pin.
Parts Required:
We’re using the VBUS pin to power the RCWL-0516 sensor, but for better results, we recommend using an external power source (4V to 28V).
Raspberry Pi Pico with the RCWL-0516 Sensor – MicroPython Script
Using the MicroPython IDE of your choice, copy the following code to the main.py file. This script reads the output of the sensor and lights up an LED and prints in the MicroPython shell when motion is detected. The sensor outputs a HIGH signal when it detects motion and a LOW signal when idle.
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-rcwl-0516-micropython/
from machine import Pin
import time
# Pin numbers for LED and sensor
led_pin = 21
sensor_pin = 22
# Set the LED pin as an output and the sensor pin as an input
led = Pin(led_pin, Pin.OUT)
sensor = Pin(sensor_pin, Pin.IN)
# Initialize state variables
state = 0
val = 0
while True:
val = sensor.value() # read sensor value
if val == 1: # check if the sensor is HIGH
led.on() # turn LED ON
if state == 0:
print("Motion detected!")
state = 1 # update variable state to HIGH
else:
led.off() # turn LED OFF
if state == 1:
print("Motion stopped!")
state = 0 # update variable state to LOW
# Add a small delay to avoid constant checking of the sensor
time.sleep(0.1)
How the Code Works
Start by importing the required modules: machine module is used to interact with hardware components, and we are importing Pin from it to control GPIO pins. The time module is used to introduce delays and control the timing of the program.
# Import required libraries
from machine import Pin
import time
Next, define the pins for the LED and for the output pin of the sensor. We’re connecting the LED to GPIO 21 and the sensor to GPIO 22
# Pin numbers for LED and sensor
led_pin = 21
sensor_pin = 22
Next, create two Pin objects, one to refer to the LED (led) and another to refer to the microwave radar sensor (sensor). The LED is defined as an output using Pin.OUT and the sensor as an input using Pin.IN.
# Set the LED pin as an output and the sensor pin as an input
led = Pin(led_pin, Pin.OUT)
sensor = Pin(sensor_pin, Pin.IN)
Then, create some variables to keep track of the state of the sensor. Basically, val is used to temporarily store the real-time output value of the sensor, while state is used to keep track of whether motion has been detected or not over time.
# Initialize state variables
state = 0
val = 0
The program enters an infinite loop (while True), meaning it will run indefinitely, repeatedly checking for motion.
while True:
We start by reading the value of the motion sensor using sensor.value(). The method returns 0 if no motion is detected and 1 if motion is detected. The result is stored in the val variable.
val = sensor.value() # read sensor value
If the sensor’s output is 1 (motion detected), we turn on the LED.
if val == 1: # check if the sensor is HIGH
led.on() # turn LED ON
Then, we check if the previous status was 0. If so, it means the state has changed and that motion has been detected. We print a message in the shell and change the state variable to 1.
if state == 0:
print("Motion detected!")
state = 1 # update variable state to HIGH
If the sensor’s output is 0 (no motion detected), we turn the LED off.
else:
led.off() # turn LED OFF
If the previous state was 1 and, if now the current value of the sensor is 0, it means motion has stopped, and we can set the state variable to 0.
if state == 1:
print("Motion stopped!")
state = 0 # update variable state to LOW
The code will continue to loop indefinitely, repeatedly checking for motion and updating the LED status and the messages on the MicroPython shell accordingly.
Demonstration
Save the code to your Raspberry Pi Pico board using Thonny IDE or any other MicroPython IDE of your choice.
Copy the code provided to a new file on Thonny IDE.
With the code copied to the file, click on the Save icon. Then, select Raspberry Pi Pico.
Save the file with the following name: main.py.
Note: When you name a file main.py, the Raspberry Pi Pico will run that file automatically on boot. If you call it a different name, it will still be saved on the board filesystem, but it will not run automatically on boot.
Click the little green button “Run Current Script” or press F5.
You’ll get messages on the MicroPython shell when motion is detected.
Additionally, the LED will light up when motion is detected.
If you have an LDR connected, you’ll need to decrease the luminosity for the sensor to detect motion.
Wrapping Up
In this tutorial, you learned how to use the RCWL-0516 microwave radar proximity sensor with the Raspberry Pi Pico (programmed with MicroPython firmware) to detect motion in your surroundings. The RCWL-0516 might be a good alternative to the PIR motion sensor depending on your project requirements.
Learn more about the Raspberry Pi Pico with our exclusive eBook:
Want to learn more about the Raspberry Pi Pico? Check out the following tutorials:
- Getting Started with Raspberry Pi Pico (and Pico W)
- Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained
- Raspberry Pi Pico: Control Digital Outputs and Read Digital Inputs (MicroPython)
- Raspberry Pi Pico: Read Analog Inputs (MicroPython)
Check out all our Raspberry Pi Pico Guides »
If you prefer to program the Raspberry Pi Pico using Arduino IDE, you can get started with the following tutorial:
Thanks for reading.
I had not been lucky with RCWL-0516 since with two previous orders from different stores in Aliexpress the triggering distance was only a few centimeters. I had to order to a third store, and via Amazon to get some properly working units.
Regards from Peru.
Hi.
Thanks for your feedback.
Can you give me an idea of the maximum distance range?
Regards,
Sara
Hi Sara, sure! The “good ones” reached about 3-4 meters while the others went randomly between 2 and 5 centimeters.
Pin numbers in the raw code are not correct:
led_pin = 2
sensor_pin = 12
Further down the article in the explanation they are correct:
Pin numbers for LED and sensor
led_pin = 21
sensor_pin = 22
Hi.
You’re right.
Thanks for letting me know.
I’ll fix the code.
Regards,
Sara