Custom sensor using NodeMCU and RCWL-0516 [Solved]

You just need to use the above code. The PIR code is needed if you are attaching HC-SR501 sensor to the board. Also note that neither one of them are perfect and have their pros and cons.

The RCWL-0516 passes through obstructions like walls, doors etc and can create false positives in certain situations.

Great @PanMat thanks a lot!

Just wanted to say thanks @PanMat - I recieved a 5 pack of these sensors today, wired one directly to a D1mini (no cap or extra resister) and it works perfectly, the range is approx 4 meters. Perfect for a room based multi-sensor.

1 Like

Thanks for this thread.

For reference, we can get the current value of the sensor by soldering a wire to one of the legs of this IC and inputting it to A0. This makes it possible to adjust the sensitivity to some extent by setting the threshold.

  • Wiring position, It is the fifth leg from the top on the right side of the image. (I used 0.2mm Enamel Wire (Polyurethane Copper Wire).)

  • YAML for ESPHome

sensor:
  - platform: adc
    name: "$devicename Analog Input"
    id: ainp
    internal: True
    pin: A0
    update_interval: 100ms
    filters:
      - multiply: 1000
    on_value: 
      then:
        - lambda: |-
            static float avr = 0.0;
            float val = id(ainp).state;
            float res;
            res = abs(avr - val);
            avr = (avr + val) / 2;
            id(diff).publish_state(res);
            if (res > id(diff_trh).state) {
              id(to_on).execute();
            }

  - platform: template
    name: "$devicename Analog Diff"
    id: diff
    update_interval: never
    internal: True

switch:
  - platform: gpio
    pin:
      number: D4
      inverted: True
    name: "$devicename LED"
    id: led

binary_sensor:
  - platform: gpio
    id: dev_m_sensor
    name: "$devicename Device Motion Sensor"
    pin:
      number: D6
      inverted: False
      mode: INPUT

  - platform: template
    id: cos_m_sensor
    name: "$devicename Costume Motion Sensor"
    on_press: 
      then:
        - switch.turn_on: led
    on_release: 
      then:
        - switch.turn_off: led

number:
  - platform: template
    name: "$devicename Threshold"
    id: diff_trh
    optimistic: true
    min_value: 10
    max_value: 1000
    step: 1
    initial_value: 30
    restore_value: true
    mode: box

  - platform: template
    name: "$devicename Hold Time (sec)"
    id: hold_time
    optimistic: true
    min_value: 0.1
    max_value: 120
    step: 0.1
    initial_value: 2
    restore_value: true
    mode: box

script:
  - id: to_on
    mode: restart
    then:
      - lambda: "id(cos_m_sensor).publish_state(true);"
      - delay: !lambda "return id(hold_time).state * 1000;"
      - lambda: "id(cos_m_sensor).publish_state(false);"
3 Likes

For all those who still have the problem that the sensor is permanently “on”: Take a look at the voltage (Vin). I had accidentally applied only 3.3V instead of 5V, so of course the sensor didn’t react…