본문 바로가기

사물인터넷

Experiments with the RCWL-0516 – Doppler Radar Distance Sensor

https://dronebotworkshop.com/rcwl-0516-experiments/

 

Experiments with the RCWL-0516 | DroneBot Workshop

The RCWL-0156 is an inexpensive microwave proximity sensor that uses Doppler Radar to detect moving objects. It can be used on its own or with an Arduino. In this article, I will show you how to use this device both ways. Includes Arduino sketches for remo

dronebotworkshop.com

Introduction

Proximity sensing is a common application for intruder alarms, light switches, and other home and industrial automation applications.  And there are several ways of accomplishing it.

We have already looked at using a PIR Sensor, this device senses the change in ambient infrared energy caused when a warm body (human or animal) enters the monitored area.

Another method of proximity detection involves using reflected ultrasonic or light beams, the intruding object reflects the beams back to their source where the time delay between transmission and reception is measured to calculate the distance to the object.

Today we will look at another method of proximity detection, this time using microwaves and a scientific phenomenon called the “Doppler Effect”. As advanced as this sounds it can be accomplished both easily and inexpensively using a unique little sensor, the RCWL-0516.

The RCWL-0516

The RCWL-0516 is a small proximity sensor that uses Dopler Radar to sense the presence of an intruder. It is very versatile and can be used on its own or in conjunction with a microcontroller or microcomputer.

This device does not consume much current and operates in a range of 4 to 28 volts, making it an ideal component for a battery-powered design.

The microwave antennas are integrated onto the small printed circuit board, making it a completely self-contained unit.

The RCWL-0516 also support an optional light-dependent resistor (LDR) to allow the device to operate only in darkness. This can be very useful in light control applications.

Module Description

The module is a single circuit board with the following connections:

Be certain that you note that the 3V3 pin is a 3.3-volt output, not a power supply input. The device has an integrated 3.3 volt voltage regulator which can provide up to 100 mA of current for powering external logic circuitry.

The Output pin is a 3.3-volt logic level pin. When the device detects a moving object it will send this pin HIGH for two seconds.  The device does not measure the distance to the detected object or its velocity, just its presence.

The VIN pin is the positive power supply input, accepting any voltage from 4-volts to 28-volts. The RCWL-0516 does not consume very much current and can easily be powered by the 5-volt output from an Arduino or a Raspberry Pi.

The CDS pins are where you can attach an optional light sensor, a light dependent resistor (LDR).  The LDR can actually be hooked up two ways:

  • Using the two CDS pads on the top of the sensor printed circuit board.
  • By connecting one end to the CDS pin on the main terminal section and the other end of the LDR to ground.

The light sensor will disable the device when it detects ambient light. You may also use the CDS pin as an Enable control for the module.

The RCWL-0516 comes without ant connecting pins attached. For my experiments I soldered a right-angled Dupont connector to the main pins, allowing the device to be mounted “standing up” on a solderless breadboard.  

I also soldered a couple of pins to the CDS pads at the top of the device. If you decide to do this note that these pins don’t observe the standard 0.1-inch spacing common to most electronic components so you will need to use individual pins.

Doppler Effect

The RCWL-0516 employs “Doppler Radar” to detect movement and thus trigger a proximity alert.

Doppler Radar makes use of the Doppler Effect, a phenomenon discovered in 1842 by the Austrian physicist Christian Doppler.

The Doppler Effect describes a change in frequency observed by a stationary observer when the source of the frequency is moving. It applies to waves of all types, water, light, radio, and sound.

You’ve likely experience this effect with sound many times, perhaps without realizing it. If you have heard vehicles whizzing by you while standing near a highway or an ambulance whose siren pitch changes as it speeds towards or away from you then you’ve heard the Doppler Effect in action.

A very common illustration of the Doppler Effect is the sound of a train whistle.  Let’s use that as an example.

When the train is stopped and blows its whistle you will hear it at the same frequency it is produced at. In our simple example, we will assume it has a frequency of 1000Hz.  As the train is stationary our observer hears the tone as 1000Hz.

Now let’s get the train moving.

Our observer is in front of the train, hopefully not on the tracks!

As the train moves, it blows its whistle, which still has a frequency of 1000Hz. However, the movement of the train compresses the sound waves in front of the train and expands them behind the train.  A boat does the same thing to waves of water as it moves.

Our observer, in front of the train, hears the compressed waves. This compression causes the frequency that he hears to rise, so the train sounds like it has a higher pitched whistle than it actually does. The frequency will fluctuate as the train gets closer and as it changes speed.

Now let’s see what happens when the observer is behind the train.

Because the waves from the whistle are exp[anded behind the train the observer will hear a lower frequency than the 1000Hz.  As the train moves away or changes speed this frequency will vary.

This is the Doppler Effect.

The Doppler Effect has many scientific applications:

  • Astronomy – Astronomers measure the red or blue shifting of light to determine the speed and distance of objects like galaxies. In fact, just about everything in the universe shows a red-shift, indicating that it is moving away from us. This observation led to the discovery of the expanding universe by Edwin Hubble in 1929.
  • Sattelite Communications – Unless they are geostationary satellites move in relation to the Earth. Dynamic Doppler Compensation is used to compensate for the change in carrier frequencies between the satellite and the ground stations.
  • Medical – An Echocardiogram machine employs the Doppler Effect to measure the velocity and direction of blood as it is pumped to and from the heart.
  • Doppler Radar – This type of RADAR measures the velocity of distant objects by seeing how they alter the microwave carrier frequency. It is used in all sorts of applications including weather monitoring and tracking aircraft. It can also be used by self-driving cars to detect other vehicles.

It is, of course, Doppler Radar that is used by the RCWL-0516 sensor.

So now that we know how the sensor operates and its pinouts let’s put it to use.

Experiments without Arduino

The RCWL-0516 is entirely capable of being used on its own. So for the first experiments we will perform we will use it with just the addition of a few components.

Let’s start by seeing how it performs all by itself.

Experiment 1 – Basic Hookup

For our first experiment we will use the most basic hookup possible. All you will need, in addition to the RCWL-0516, is an LED, a dropping resistor, a few wires and a source of power.

I used my 5-volt bench power supply for this experiment but you could use another power source if you wish. A 6-volt battery would work well.

I chose a 220-ohm resistor as my dropping resistor, but any resistor with a value of 150 to 470 ohms would work. The color of the LED is also unimportant.

After hooking this up on a solderless breadboard I observed that when I made a movement near the sensor the LED would light up for about two seconds. Which is exactly what it is supposed to do!

One other thing I was able to observe was that the device was just as sensitive when I covered it with a small pill bottle. The microwaves essentially “see” through the plastic bottle. This means you could build one of these sensors inside of a plastic enclosure, ideal if you want to hide it or protect it from the elements.

I also noticed that the angle of the sensor made a difference in its sensitivity. If you have difficulty getting this to work try changing the angle or direction that you have your RCWL–0516 sensor oriented.

With that done it was on to the next experiment.

Experiment 2 – Adding a Light Sensor

For the next experiment, we will make use of an LDR, or Light Dependent Resistor.

As its name would imply an LDR is a resistor whose resistance changes when exposed to light. In the old days this was called a “photocell”.  Most LDRs experience a decrease in resistance when exposed to a light source.

The RCWL-0516 has a couple of pads made specifically for attaching an LDR. You can use just about any LDR that you can get your hands on.

Hooking this up is pretty easy, LDR’s are not polarity-sensitive so you can connect it up any way you want to. If you don’t want to use the pads at the top of the RCWL-0516 the use the CDS pin on the bottom and connect your LDR between it and the ground connection.

You should now observe that the sensor produces no output when the LDR is exposed to light. But turn off the lights and it is business as usual.

This has a lot of practical applications for controlling room lights or for detecting intruders at night.

As you can see the RCWL-0516 is a pretty handy device on its own!

Adding an Arduino

While the RCWL-0516 can be used on its own it also works well as a sensor for a microcontroller or a microcomputer. You can use the RCWL-0516 to detect movement and then have your controller take additional action.

I’m going to show you a couple of ideas for using it with an Arduino Uno. I have no doubt that you can come up with many more.

Arduino Hookup

I’ve seen a number of articles and videos about hooking up the RCWL-0516 to an Arduino so that you can turn on an LED when it detects motion. And while this does serve to illustrate how the hookup is performed it really has no practical purpose – after all, you can just drive the LED directly with the RCWL-0516 and eliminate the need for the Arduino!

Instead, I wanted to use the Arduino for something practical. And the first thing I came up with was a latching device. You can see how to wire it up here.

In this circuit, I have used an Arduino Uno, an RCWL-0516, an LED, a couple of resistors and a push button switch.

The resistor attached to the LED is a dropping resistor, I used a 220-ohm resistor for this. The color of the LED is entirely up to you. You could also use a relay instead of an LED if you wanted to control something that needed more current or voltage, if you do make sure you observe all safety precautions.

Best bet is to use an LED during development, it’s much safer that way.

The other resistor is a pull-up resistor for the switch, I used a 10K resistor.

Arduino Sketch

The purpose of this device is to keep the LED on after the proximity detector is triggered. In the basic hookup, the one without an Arduino, the LED only remained on for about two seconds. This time I want it to be left on.

The push button is provided to turn off the LED, in a sense it “rearms” the circuit.

Here is the sketch I used to make it all happen:

/*  
  RCWL-0516 Latching Demo
  RCWL0516_latch_demo.ino
  Demonstrates using RCWL-0516 Microwave Sensor
  With pushbutton
  
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
int Sensor = 2;     // RCWL-0516 Input Pin
int Pbut = 3;       // Push button Input Pin
int LED = 13;       // LED Output Pin
 
int sensorval = 0;  // RCWL-0516 Sensor Value
int pbval = 0;      // Push button value
 
 
void setup() {
  pinMode (Sensor, INPUT);  // RCWL-0516 as input
  pinMode (Pbut, INPUT);    // Push button as input
  pinMode (LED, OUTPUT);    // LED as OUTPUT
  digitalWrite(LED, LOW);   // Turn LED Off
}
 
void loop(){
 
  sensorval = digitalRead(Sensor);  // Read Sensor value
  pbval = digitalRead(Pbut);        // Read Push button value
  
  if (sensorval == HIGH) {        
    digitalWrite(LED, HIGH);  // Turn LED On
  }
  
  if (pbval == LOW) {        
    digitalWrite(LED, LOW);  // Turn LED Off
  }
}

It is actually a pretty simple sketch.

We start by defining the pins that the RCWL-0516 sensor, the push-button, and LED are connected to.  We also define a couple of variables to hold the values of the sensor and push button.

In the setup we set the sensor and push-button connections as inputs and the LED connection as an output. We also turn off the LED.

The loop is where all the action is.

In the loop we first read the value of both the RCWL-0516 sensor and the push-button.

  • When the sensor is triggered it will read HIGH.
  • When the push-button is pressed it will read LOW.

We then use a couple of If statements to check these values.

  • If the sensor is HIGH (triggered) then we turn the LED on.
  • If the push-button is LOW (button pressed) we turn off the LED.

And that’s basically it. The LED will be turned on when the sensor is triggered.  The only way to turn it off is to press the button. Simple but effective.

Hook it up, load up the sketch to the Arduino and give it a try. You should see the LED illuminate when you make a bit of motion. Unlike the last experiment, it wil remain on until you press the button to turn it off.

A circuit like this would have many practical applications. You could use it as an intruder alarm or as a control for lights.

You could also combine it with the LDR to have it only work in dark rooms.

Arduino Remote Sensor Project

For our final Arduino experiment, I wanted to make a remote sensor. This way I could detect an intruder in another room.

You will need two Arduino Uno’s for this, you could use other types of Arduino’s but that might require a wiring change as I’m using the SPI Interface as part of the project and it is on different pins on different Arduino’s.

You will also need a 433 MHz RF transmitter and a 433 MHz receiver. I have used these devices before, so if you are not familiar with them please see my article “Using Inexpensive 433MHz Transmit and Receive Modules with Arduino” to bring yourself up to speed.

A couple of LED’s and dropping resistors are all that is left in the parts list. Now let’s see how to hook them up and program them. I’ll start with the transmitter first.

Arduino Remote Sensor Transmitter Hookup

The RCWL-0516 is hooked up to use the 5-volt power output from the Arduino Uno. The data out pin is connected to Arduino digital I/O pin number 2.

The LED anode is connected to a dropping resistor and that is in turn connected to pin 13 on the Arduino. You could, of course, eliminate the LED and just use the onboard LED on your Arduino Uno which is internally connected to pin 13. I just thought an external LED would be easier to see.

The hookup to the 433 MHz transmitter is actually very simple. The VCC is connected to the Arduino 5-volt output, and the GND is connected to ground. The DATA pin is connected to Arduino pin 12, which is the SPI input.

Arduino Remote Sensor Transmitter Code

Before you can write the code to make the transmitter and receiver work you will need to add a library to your Arduiono IDE.  This library will assist you in operating the 433 MHz radio modules.

The library is called RadioHead and I have discussed it before in the article “Using Inexpensive 433MHz Transmit and Receive Modules with Arduino”. It can be used with many RF modules, including the 433 MHz modules we are using here.

You can download the RadioHead library from the Airspayce website. It’s a ZIP file and the link is buried on the page, look at the diagram below to see how to find it.

Once you have the ZIP file downloaded you’ll need to install it in your Arduino IDE. It will install a number of example sketches as well as the library itself.

  • Open the Sketch menu item from the top menu bar.
  • Select Include Library. A sub-menu will appear.
  • Select Add .ZIP Library.
  • A dialog box will open. Navigate to the location you saved the ZIP file and select it.
  • Click OK. The library will be installed into your Arduino IDE.

Now that the library is installed you can load the sketch. You can download it from the link in the Resources box at the end of this article of just copy it here.

/*  
  RCWL-0516 Remote Transmit Demo
  RCWL0516_remote_xmit_demo.ino
  Demonstrates remote using RCWL-0516 Microwave Sensor
  Use with RCWL-0516 Remote Receive Demo
  
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Include libraries for transmitter
#include <RH_ASK.h> // Include RadioHead Amplitude Shift Keying Library
#include <SPI.h>  // Include dependant SPI Library 
 
// Define Input and Output
int Sensor = 2;     // RCWL-0516 Input Pin
int LED = 13;       // Output LED
 
// Define variables
int sensorval = 0;  // RCWL-0516 Sensor Value
String str_out;   // Output String
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup() {
 
  rf_driver.init(); // Initialize ASK Object
  pinMode (Sensor, INPUT);  //RCWL-0516 as input
  pinMode (LED, OUTPUT);    //LED as OUTPUT
  digitalWrite(LED, LOW);  // Turn LED Off
  
}
 
void loop()
{
 
  sensorval = digitalRead(Sensor);  //Read Sensor value
  
  if (sensorval == HIGH) {        
    str_out = "1";
    digitalWrite(LED, HIGH);  // Turn LED On
  }else{
    str_out = "0";
    digitalWrite(LED, LOW);  // Turn LED Off
  }
      
    // Compose output character
    static char *msg = str_out.c_str();
    
    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
  
}

The sketch starts by including the RadioHead Library. We also include the SPI library, which is part of your Arduino IDE.

Next, we define pins for the sensor and LED. Note that it is not necessary to define pin 12 as the connection to the transmitter as the SPI library takes care of that on its own.

After that, we define variables to represent the sensor value and to use as a holder for the string that we plan to transmit.

The next line creates an Amplitude Shift Keying object, ASK is the method that we use to format the data during transmission. RadioHead takes care of all the ugly details behind this, which include acknowledgments and error checking.

In the setup we initialize the transmitter. We also set the pinModes of the sensor and LED pins and then turn the LED off.

Now to the loop!

In the loop we get the value from the sensor. We then use an IF-ELSE statement to evaluate it and perform the folowing:

  • If the sensor is HIGH that means we detected something. We set the output string to “1” and turn on the LED.
  • If the sensor is LOW then nothing is detected. We set the output string to “0” and turn off the LED.

We then compose a message with the string we just created.

Finally, we send the string and wait for an acknowledgment from the receiver indicating that it was received successfully.

And that’s it for the transmitter. Now let’s look a the other end, the receiver side.

Arduino Remote Sensor Receiver Hookup

Here is how we will hook up our receiver to a second Arduino Uno.

The hookup here is even simpler than the transmitter.

The receiver module VCC is connected to the Arduino Uno 5-volt power output. The GND is connected to the Arduino ground.

The receiver module has two pins for data out, you can use either one as they are internally connected together. Connect one of these pins to Arduino Uno pin 11, which is the SPI input.

The only other connections are to the LED, which is connected to pin 13 through a dropping resistor like it was in the transmitter. Although the diagram shows a green LED I used a blue one (blue LED’s don’t show up well on my blue background so it’s green here), you can actually use any color you like.

Now that you have the receiver wired up let’s look at the code you’ll need to run it.

Arduino Remote Sensor Receiver Code

The receiver code also makes use of the RadioHead library.

/*  
  RCWL-0516 Remote Receive Demo
  RCWL0516_remote_rcv_demo.ino
  Demonstrates remote using RCWL-0516 Microwave Sensor
  Use with RCWL-0516 Remote Transmit Demo
  
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/
 
// Include libraries for receiver
#include <RH_ASK.h> // Include RadioHead Amplitude Shift Keying Library
#include <SPI.h>  // Include dependant SPI Library 
 
// Define Output
int LED = 13;       // Output LED
 
// Define variable
String str_out;   // Output String
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup() {
 
  rf_driver.init(); // Initialize ASK Object
  pinMode (LED, OUTPUT);    //LED as OUTPUT
  digitalWrite(LED, LOW);   // Turn LED Off
  
}
 
void loop()
{
    // Set buffer to size of expected message
    uint8_t buf[1];
    uint8_t buflen = sizeof(buf);
    // Check if received packet is correct size
    if (rf_driver.recv(buf, &buflen))
    {
      
      // Message received with valid checksum
      // Get values from string
      
      // Convert received data into string
      str_out = String((char*)buf);
      
  if (str_out == "1") {        
    digitalWrite(LED, HIGH);  // Turn LED On
  }else{
    digitalWrite(LED, LOW);  // Turn LED Off
  }
               
    }
}

We start by including the RadioHead library and the Arduino SPI library, just as we did in the previous sketch.

The only pin we need to define is pin 13 for the LED, the receiver uses the SPI library which already defines pin 11 as receive.

Again we define a string variable to hold the received string.  And we create an ASK object as we did before.

In the setup we initialize the receiver and define the LED as an output. We also turn the LED off.

Now the loop.

In the loop we set a receive buffer for the size of the data string we are expecting, which is 1 in our case.

We then check to see if we have received a valid packet. If we have we get the data from it and pass it to our string variable.

We then check the string value:

  • If it has a value of “1” we turn on the LED.
  • If it has a value of “0” we turn off the LED

And from there the loop ends and we start all over again.

Testing the Remote Sensor

Hook up both of the Arduino’s and load up the code. You will need to power each one individually, I powered one with a 9-volt battery and the other one with the USB cable from my computer.

You should probably start with the two units fairly close together. After you determine that tthey are functioning correctly you can experiment with moving them apart.

You can increase the range by soldering a small coiled wire to both the transmitter and receiver to act as an antenna, as I have done in my setup.

If all is working well you should see both LED’s light when the detector is triggered. You may notice a very slight time delay between the transmit and receive, this is normal.

You can now detect those fiendish jellybean thieves from the room next door!

Conclusion

The RCWL-0516 is an inexpensive yet effective proximity sensor. It has a lot of obvious uses in both intruder detection and home automation and as the microwaves it uses are not affected by plastic or wooden barriers it can be hidden away in strategic locations.

Hopefully, this article and its associated video have given you some insight into how you might use this Dopler Radar sensor in one of your own designs.

Time to start experimenting!

 

Resources

Arduino Sketches – The three sketches I wrote for this article, in one ZIP file.

RCWL-0516 Information – A useful resource on GitHub that describes this sensor in detail.

Doppler Effect – A Wikipedia article on the Doppler Effect.

RadioHead Library – The RadioHead packet library for Arduino.