Pages

Friday, August 3, 2018

Motion detection with RCWL-0516 Radar

The most well-known motion detecting module is the PIR of which I described the basics in this story http://lucstechblog.blogspot.nl/2017/01/pir-basics-movement-detection.html and with which I made a simple alarm in this story http://lucstechblog.blogspot.nl/2017/05/ifttt-part-3-basic-alert-over-ifttt.html

However lately there is a lot of  fuss about a new module by the name RCWL-0516. So let's see what the difference with a PIR is.

A PIR makes a kind of infra red map of its location. After a while it makes a new infra red map and compares the two. When there is a change in the map that means there is motion and the PIR the makes its output high.
As this description suggests the PIR depends on a change of location of a heat source.

The RCWL-0516 is a magnetron / radar module. The module sends out pulses and measures the return time. If a change in the return time is detected that indicates that something in the path of the pulses has changed position. So the RCWL-0516 is not heat dependend but detects any movement. In real life that almost boils down to the same as most movements will mostly be made by people or animals which are also heat sources.

The connections


 
Do not let the size of this picture deceive you. The module is only 1.7 x 4 cm !!!

As you can see on the right side the module has 5 connections.

- 3.3 Volt
- GND
- OUT
- VIN
- CDS


I found no official papers on this module on the internet. However there are some hobbyists who have done some research and their findings is what I am using to work with.
 

As long as CDS is high the module functions. So it is the chip-select line. In the beginning I used the module with CDS unattached and it worked. Best results are gained when CDS is attached to +3.3 Volts.  Test what works best for you.

According to the literature I found on the internet the 3.3 Volts pin is an OUTPUT pin. So you should not use it to power the module. Power should be connected to VIN. Again according to the documentation found on the web anything from 4 to 28 Volt will work. I just attached it to the VIN from my NodeMCU unit and that works flawlessly.

First test

I did not find a Fritzing part for the RCWL-0516. I presume that that is because the module is so new.  I tried to make a simple alternative for it.




This first setup is very simple. In fact it almost looks like the first PIR setup.
Just attach a 5 volt source (I used an USB power bank) to GND and VIN and connect OUT to a led with a delimiting 220 Ohm resistor.

When I powered this setup for the first time I thought the module was not working or that I connected it wrongly. The LED was ON all the time. This will happen to you also if you use this setup. But do not worry. The module is not faulty and you did not do anything wrong. The module is just so sensitive that it notices any movement direct and therefore the LED will always be on.

So I sat totally still and the led went out.
When I as much as lifted a finger or nodded my head the LED went on again. This module is very sensitive.

The LED will stay on for about 2 seconds and then goes OFF if no movement is detected.

We have a long corridor in our house and I put this setup on one end and I stood on the other side and the module easily detected my movements. The distance was 6 meter !!!!

Attach it to an ESP8266

The next step was to connect the RCWL-0516 to an ESP8266. I used a NodeMCU for this as it supllies 5 Volts and 3.3 Volts and is easily powered over USB.




As you can see I powered it with 5 Volts from the NodeMCU and attached CDS to 3.3 Volts on the NodeMCU. I attached the led to D5 (through a 220 ohm delimiting resistor) and the RCWL-0516 OUT pin to D6 on the NodeMCU.

Software

For a quick result I wrote a program in ESP-Basic




timer 1000, [test]

cls
wprint |<h1 style="text-align:center;">Luc Volders</br>Radar Tester</br>|
wprint "<br/>"
textbox value
wprint "<br/><br/>"
button "<h2>Off</h2>", [Off]
wprint "<br/>"
wait

[test]
sensor = io(pi,d6)
if sensor = 0 then
  io(po,d5,0)
else
  io(po,d5,1)
endif
value = sensor
wait

[Off]
io(po,d5,0)
end



First pin D5 is set as output (this is where the led is connected) and D6 is defined as input (where the RCWL-0516 output) is connected.
Next step is to set variable A to 0 so we can make an endless loop.
Then the program tests wether D6 is LOW. If that is the case the led stays off and the program jumps to subroutine green that sets the led on the web-page also off.
When D6 is HIGH (movement detected) the program sets the led on and jumps to subroutine red which sets the led on the webpage in the color red.

I put this setup in my room and aimed it at the door. Next I went outside my room and closed the door. I opened my webbrowser on my phone and look at what was happening.
The RCWL-0516 detected movement at a distance of 4 meter through a closed door !!!

This means that we can put this module INSIDE an enclosure and makes it more hide-able in case of an alarm system.

Summarising.

- The RCWL-0516 is a very cheap and yet sensitive motion sensor.
- It can be powered with anything from 3.3 to 28 volts
- The RCWL-0516 measures movement easily on a distance of 6 meter
- The RCWL-0516 measures movement from a distance of 4 meter through a closed door !!!
- The module just uses 1 data pin so can be used with an ESP-01 or even an Attiny85
- As the module can be powered with 3.3 or 5 volts and has a 3.3 signal pin it can be used with a Raspberry Pi

So you can build an enclosure for your project and put the RCWL-0516 inside and it will still work. Testing is recommended though.

Till next time.
Have fun

Luc Volders