Skip to content

A Swift library for the RCWL-0516 Microwave Radar

License

Notifications You must be signed in to change notification settings

uraimo/RCWL-0516-Radar.swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RCWL-0516-Radar.swift

A Swift library for the RCWL-0516 Microwave Radar

Linux-only Swift 4 compatible License: MIT

RCWL-0516 animation

Summary

With this simple library you'll be able to use a RCWL-0516 microwave radar to detect movements of humans(or animals) inside its detection range (4-7 meters all around and a few meters upward, depending on the module and VIN).

This sensor is an alternative to common PIR motion sensors that use infrared light to detect movements and that have their range limited by the reflective surfaces that surrounds them. The RCWL signal instead, can get through relatively thick surfaces made by non-metal materials (doors, windows, walls, etc...).

Hardware Details

All the information available for this mysterious module have been discovered by Joe Desbonnet and collected on GitHub. Check it out if you want to know more.

The module costs a few dollars and can work with a 5V VIN and has a 3.3V OUT output signal that allows us to connect it to a GPIO port of essentially any ARM board.

An additional 3.3V output to power external devices and a light sensor input CDS are also available. Most of the times you'll just connect VIN, GND and then OUT to one of your GPIOs.

Each time the sensor detects movements, the OUT pin, that is normally at 0V when no motion is detected, is set to high (3.3V).

The radar does not give you information on the distance of the object that is moving and does not even give you an approximate location within its range, it just detect continuous motion or new objects entering its range.

Since the module uses a 3Ghz based signal, it will not cause any interference with other wireless devices.

Multiple modules can be used together in the same room.

Update 2024: If you are here because you want to know more about this sensor, check out this link and this link.

Usage

The library revolves around the RCWL0516Radar object.

To create a RCWL0516Radar instance we just need to initialize the object with a GPIO instance obtained from SwiftyGPIO, that points to the board pin where OUT is connected (e.g. P18):

import SwiftyGPIO
import RCWL0516Radar

let gpios = SwiftyGPIO.GPIOs(for:.RaspberryPi2)
var gp = gpios[.P18]!

let r = RCWL0516Radar(gp)

// Check for motion every second
while(true){
    print(r.getStatus())
    sleep(1)
}

Calling getStatus we can know if the sensor is detecting movements or not (be aware that the sensor will need a second or so to understand that the motion has stopped).

To register handlers that will be called when some kind on motion starts or stops in the detection range, use onMotion(closure:) and onMotionStop(closure:).

r.onMotion{
    print("Movement detected!")
}

r.onMotionStop{
    print("No more movement...")
}

Handlers can be unregistered calling clearMotionHandlers().

Supported Boards

Every board supported by SwiftyGPIO: RaspberryPis, BeagleBones, C.H.I.P., etc...

To use this library, you'll need a Linux ARM board with Swift 3.x/4.x.

The example below will use a RaspberryPi 2 board but you can easily modify the example to use one the other supported boards, a full working demo projects for the RaspberryPi2 is available in the Examples directory.

Installation

Please refer to the SwiftyGPIO readme for Swift installation instructions.

Once your board runs Swift, if your version support the Swift Package Manager, you can simply add this library as a dependency of your project and compile with swift build:

  let package = Package(
      name: "MyProject",
      dependencies: [
        .Package(url: "https://github.com/uraimo/RCWL-0516-Radar.swift.git", majorVersion: 2),
      ]
  ) 

The directory Examples contains sample projects that uses SPM, compile it and run the sample with ./.build/debug/Test.

If SPM is not supported, you'll need to manually download the library and its dependencies:

wget https://raw.githubusercontent.com/uraimo/RCWL-0516-Radar.swift/master/Sources/RCWL0516Radar.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/Presets.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SunXi.swift  

And once all the files have been downloaded, create an additional file that will contain the code of your application (e.g. main.swift). When your code is ready, compile it with:

swiftc *.swift

The compiler will create a main executable.

About

A Swift library for the RCWL-0516 Microwave Radar

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages