Squirrel Logo

Sonoff Basic R2 with motion sensor

Johan Vromans
Articles » Smart Ceiling Light

Sonoff Basic R2 with motion sensor

As part of an ongoing home automation project I want to use a Sonoff Basic R2 to control a ceiling light with a motion sensor. Moreover, I want to embed the Sonoff inside of the lamp.

Sonoff Basic R2

I ordered this one from Ali Express, it turns out to be a revision 1.3.


The first step was to reflash the Sonoff with Tasmota. This is a fairly standard procedure, see e.g. Sonoff Basic - the one that started it all.

Configure WiFI and MQTT as usual.

Adding the PIR


I just happened to have a cheap NS312 from a LED strip controller.

This PIR is a little slow, it takes approx. 2 seconds to respond to motion, and then stays on for another two seconds. Neither will be a problem in our use case.


The Sonoff Basic R2 revision 1.3 has few options to access the GPIO pins. I chose for the easiest: GPIO3 a.k.a. Rx. I wouldn't need the Tx/Rx signals anyways.

Wires soldered on the board. Oops, I accidentally the on/off button. No problem since we don't need it.


A small hole for the PIR wires and the basic setup is ready.

Configuring Tasmota


The NS312 Vout goes high when motion is detected, so we need to configure GPIO3 as Switch_n. See Buttons Schematics for more details.

We're going to use the motion detector to switch the power on when motion is detected, but power off will be controlled by PulseTime. This can easily be achieved by setting the appropriate values for SwitchMode and PulseTime as follows:

    SwitchMode1 13
    PulseTime1 400

Or, as a single command:

    Backlog SwitchMode1 13; PulseTime1 400

SwitchMode1 13 means that the switch is used in pushbutton mode, pressing it turns on, and it switches off after PulseTime. PulseTime, if greater than 111, is measured in seconds with offset of 100, so with a value of 400 the light will stay on for 5 minutes when motion is detected.

If motion is detected while the light is on, the timer restarts. A bit of movement now and then will keep the light on continuously.

Note: This is autonomous and does not require a connection to Home Assistant.

Home Assistant

The Tasmota integration of Home Assistant will automatically pick up the new device via the MQTT broker.

It is seen as a switch, but with the new Change device type of a switch feature it can be treated as a light instead.

Home Assistant Integration

Finishing up

Sonoff inside

Sonoff, sensor and wires inside the ceiling lamp.

Sonoff inside

With the glass cover in place.

Even though it turned out that I had a live wire accessible at the place of the lamp, I chose to connect the Sonoff to the neutral and switched wires. I also configured Tasmota to activate the relais when powered on.

    PowerOnMode 1

As a result, when someone enters the room and tries to turn on the light by flipping the switch, apparently nothing happens (the Sonoff will be powered down). If she then flips the switch again — voila, the light turns on. This is probably less suprising than flipping the switch and nothing happens at all since the switch is no longer connected.


After some time I decided to discard this approach. The PIR reacts too slow to be useful. So in practice only the wall switch was used to turn the light on and off.

© Copyright 2003-2023 Johan Vromans. All Rights Reserved.
articles/tasmota_sonoff_pir/index.html last modified 22:46:01 05-Jul-2022