Can anybody explain a newbie how to connect a PIR sensor to a Sonoff(Tasmota firmware)?

5,570 views
Skip to first unread message

and...@gmail.com

unread,
Apr 28, 2017, 2:39:46 AM4/28/17
to SonoffUsers
Hi group!

I tried looking for a newbie-friendly tutorial on how to connect a PIR sensor directly on to my sonoff for motion detection, but couldn't find any. Also looked in the group and the questions answered are beyond my understanding. The wiki also didn't help:/

To give you an idea of my knowledge(or lack thereof) on the matter, the sonoff was the first time I ever dealt with electronics and PCBs and connecting jumper wires etc, and that was a few months ago.

With that in mind, can anybody explain to me how I would go about connecting a PIR sensor to my sonoff basic and how to configure it on the Tasmota firmware? Or point me to a tutorial I can follow please?

Cheers

and...@gmail.com

unread,
Apr 28, 2017, 2:50:42 AM4/28/17
to SonoffUsers

Pascal

unread,
Apr 28, 2017, 4:28:11 AM4/28/17
to SonoffUsers



Works fine with Domoticz
Auto Generated Inline Image 1
Auto Generated Inline Image 2

Sebastian Lorenzen

unread,
Apr 28, 2017, 4:58:05 AM4/28/17
to SonoffUsers
Thank you for these pictures. Was also looking for something like this.
You have connected a PIR Sensor and a second relay?

For my purpose I just need the PIR sensor. Did you do anything with the Sensor in terms of voltage? I think they run at 5V?

Pascal

unread,
Apr 28, 2017, 6:00:17 AM4/28/17
to SonoffUsers
The second relay is only for exlain the config to you.
I use the pir direct on the Sonoff / Domotics, GPIO.. switch to Domoticz IDX...
3.3 volt from the Sonoff works perfect

Sebastian Lorenzen

unread,
Apr 28, 2017, 6:20:33 AM4/28/17
to SonoffUsers
Thanks. Is it possible to not directly switch the Sonoff and just inform the home automation if light should go on or not.
Eg in the middle of the night I want to switch on just a small light not the main light.

Pascal

unread,
Apr 28, 2017, 7:06:40 AM4/28/17
to SonoffUsers
Of course, send the pir signal to Domoticz and tell what and when to do.

My pir sends a command to Domoticz and turn the Hue lamp to 60% on for 2 minutes in the evening and 30% for one minute at night, after last motion. Two pirs together? Make a group.
There are many examples on the Domoticz forum.

I think it will go the same way with other programs than Domoticz.

Sebastian Lorenzen

unread,
Apr 28, 2017, 3:10:35 PM4/28/17
to SonoffUsers
Great, if Domoticz can do this, I can also realize this with FHEM I'm currently using.

David Lang

unread,
Apr 28, 2017, 3:19:03 PM4/28/17
to Sebastian Lorenzen, SonoffUsers
Yes, you configure the input as a switch and set switchtopic, there is a page on
the wiki on this.

David Lang

On Fri, 28 Apr 2017, Sebastian Lorenzen wrote:

> Date: Fri, 28 Apr 2017 03:20:33 -0700 (PDT)
> From: Sebastian Lorenzen <sebastian...@outlook.com>
> To: SonoffUsers <sonof...@googlegroups.com>
> Subject: Re: Can anybody explain a newbie how to connect a PIR sensor to a
> Sonoff(Tasmota firmware)?

Sebastian Lorenzen

unread,
May 2, 2017, 6:47:28 AM5/2/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
Hello again. I had some time now to played around with my basic and a pir sensor  (HC-SR501)
I followed the wiki and connected the pir to GPIO14 and configured it as a switch (9)
I also configured switch mode to have a different topic for the pir.

Now I get on and off every 5 seconds regardless what I do with the poti on the pir.
I also changed the Jumper and tried a different pir sensor.

Console:
11:41:31 MQTT: cmnd/basic_01_pir/POWER1 = ON
11:41:35 MQTT: cmnd/basic_01_pir/POWER1 = OFF
11:41:37 MQTT: cmnd/basic_01_pir/POWER1 = ON
11:41:44 MQTT: cmnd/basic_01_pir/POWER1 = OFF
11:41:48 MQTT: cmnd/basic_01_pir/POWER1 = ON
11:41:55 MQTT: cmnd/basic_01_pir/POWER1 = OFF
11:41:59 MQTT: cmnd/basic_01_pir/POWER1 = ON
11:42:02 MQTT: cmnd/basic_01_pir/POWER1 = OFF
11:42:04 MQTT: cmnd/basic_01_pir/POWER1 = ON
11:42:13 MQTT: cmnd/basic_01_pir/POWER1 = OFF

any hint how to solve it?

David Lang

unread,
May 2, 2017, 1:23:16 PM5/2/17
to Sebastian Lorenzen, SonoffUsers
It sounds as if your PIR leaves the pin floating when it's not switched, so try
hooking a pull-up resistor to the pin (a 4-10k resistor between the pin and
3.3v)

David Lang

On Tue, 2 May 2017, Sebastian Lorenzen wrote:

> Date: Tue, 2 May 2017 03:47:28 -0700 (PDT)
> From: Sebastian Lorenzen <sebastian...@outlook.com>
> To: SonoffUsers <sonof...@googlegroups.com>
> Cc: sebastian...@outlook.com, da...@lang.hm
> Subject: Re: Can anybody explain a newbie how to connect a PIR sensor to a
> Sonoff(Tasmota firmware)?
>
>>> From: Sebastian Lorenzen <sebastian...@outlook.com <javascript:>>
>>> To: SonoffUsers <sonof...@googlegroups.com <javascript:>>

Sebastian Lorenzen

unread,
May 3, 2017, 4:00:47 AM5/3/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
I put in a 10k  resistor and it gets a bit better. Still a lot of "random" on and off . Also a 4.7k resistor is not working better.

Interesting observation: When I power on the sonoff and pir and put in the resistor in it afterwards, it is almost ok.
When I wire everything together and power it on, it is the same behavior as without the resistor.
It looks like it kind of adapts to the resistor while booting up.

thoughts?

Sebastian

David Lang

unread,
May 3, 2017, 4:39:07 AM5/3/17
to Sebastian Lorenzen, SonoffUsers
double check that your PIR sensor is not outputting 5v.

David Lang

On Wed, 3 May 2017, Sebastian Lorenzen wrote:

> Date: Wed, 3 May 2017 01:00:46 -0700 (PDT)
> From: Sebastian Lorenzen <sebastian...@outlook.com>
> To: SonoffUsers <sonof...@googlegroups.com>
> Cc: sebastian...@outlook.com, da...@lang.hm
> Subject: Re: Can anybody explain a newbie how to connect a PIR sensor to a
> Sonoff(Tasmota firmware)?
>
> I put in a 10k resistor and it gets a bit better. Still a lot of "random"
> on and off . Also a 4.7k resistor is not working better.
>
> Interesting observation: When I power on the sonoff and pir and put in the
> resistor in it afterwards, it is almost ok.
> When I wire everything together and power it on, it is the same behavior as
> without the resistor.
> It looks like it kind of adapts to the resistor while booting up.
>
> thoughts?
>
> Sebastian
>
> Am Dienstag, 2. Mai 2017 19:23:16 UTC+2 schrieb David Lang:
>>
>> It sounds as if your PIR leaves the pin floating when it's not switched,
>> so try
>> hooking a pull-up resistor to the pin (a 4-10k resistor between the pin
>> and
>> 3.3v)
>>
>> David Lang
>>
>> On Tue, 2 May 2017, Sebastian Lorenzen wrote:
>>
>>> Date: Tue, 2 May 2017 03:47:28 -0700 (PDT)
>>> From: Sebastian Lorenzen <sebastian...@outlook.com <javascript:>>
>>> To: SonoffUsers <sonof...@googlegroups.com <javascript:>>
>>> Cc: sebastian...@outlook.com <javascript:>, da...@lang.hm <javascript:>

Sebastian Lorenzen

unread,
May 3, 2017, 6:28:40 AM5/3/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
Did. Output is 3.2 Volts and 0.17 (Without resistor) It changes in the same way as the sonoff is registering movement.
If the resistor is between 3.3 Volts and middle pin, it hast 3.3 and 0.32 Volts.

David Lang

unread,
May 3, 2017, 9:44:12 AM5/3/17
to Sebastian Lorenzen, SonoffUsers
check the output of the PIR when it's not connected to the sonoff at all.
k

Sebastian Lorenzen

unread,
May 3, 2017, 2:15:25 PM5/3/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
I have disconnected the output and measured voltage.
Behavior is gone then. It stays off when no motion is detected. Also voltage on output goes down to zero. When connected to the Sonoff it stays at 0.17V

I have reconnected the cable and the issue went away for a while. After a reboot it is back. On off every few seconds.
As soon as I put my multi-meter on the circuit it is stable

Sebastian Lorenzen

unread,
May 4, 2017, 11:28:38 AM5/4/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
The problem was probably related to the cables I have used. (Ready-for-use china ware)
The whole day my multimeter was attached to the cable using crocodile clamps and all was stable. Even when I disconnected the multimeter it was still working good. As soon as I detached the clamps it started again.

With new cables no issues so far :-)

I will monitor this again...

Sebastian Lorenzen

unread,
May 5, 2017, 8:17:32 AM5/5/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
Ok, bad news. It was not the cable. Same behavior as before :-(

I will try another sonoff. I already tried all of my 5 pir sensors and all of them show the issue. Don't think all of them are broken.

David Lang

unread,
May 5, 2017, 2:53:59 PM5/5/17
to Sebastian Lorenzen, SonoffUsers
what is the output voltage of your PIR modules? some of them are 5v, not the
3.3v that the sonoff is designed for.

Sebastian Lorenzen

unread,
May 8, 2017, 11:25:34 AM5/8/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
they are designed for 5v. I have removed the diode which converts it to 3.3 v. So it is nativ 3.3 now.

Sebastian Lorenzen

unread,
May 8, 2017, 12:00:22 PM5/8/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
hm, I probably try this hack: https://forum.mysensors.org/topic/225/hc-sr501-motion-sensor/25
More later this week.

Gijs Noorlander

unread,
May 14, 2017, 5:33:55 PM5/14/17
to SonoffUsers, sebastian...@outlook.com, da...@lang.hm
I cannot find the blog post where I read about it, but if I remember correctly, that blog was about the sensitivity of the PIR used to the digital processing in the ESP-modules.
I am not sure what was causing the issues. You may expect the strong fluctuating load of the ESP-module may cause some issues with the PIR, since these are almost always sensitive to voltage drops, which cause false detections. (See Click-On-Click-Off sensors on low battery)
But his conclusion was that he tried a PIR sensor with a digital output, so he even could use long cables without erradic detections.


I'm very interested in other solutions, since I will also try some of these sensors myself later in the project.

Sebastian Lorenzen

unread,
May 16, 2017, 11:05:11 AM5/16/17
to SonoffUsers
I would also be highly interested in these pir sensors.

Don Willingham

unread,
May 25, 2017, 11:28:22 PM5/25/17
to SonoffUsers
This video https://youtu.be/jpjfVc-9IrQ is somewhat related. He had some trouble with noisy results from a PIR, so he switched to another one. It's not a guide to sonoff+PIR, but may be of some use.

Przemysław Bierut

unread,
Oct 2, 2017, 8:26:37 AM10/2/17
to SonoffUsers
Have you found final solution for problem with connecting PIR to sonoff?

Sebastian Lorenzen

unread,
Oct 2, 2017, 8:46:30 AM10/2/17
to SonoffUsers
sorry, now. :-(

Phil

unread,
Oct 2, 2017, 11:24:52 AM10/2/17
to SonoffUsers

Sebastian Lorenzen

unread,
Oct 2, 2017, 5:35:21 PM10/2/17
to SonoffUsers
nice. But this does not use a pir sensor directly with the Sonoff. That what is not working well.
Maybe we need some kind of logical detachment in signaling between sonoff and pir as they influence each other in a bad way.

Heinz-Josef Gode

unread,
Oct 15, 2017, 2:03:28 AM10/15/17
to SonoffUsers
I have a running configuration with the HC-501SR and sonoff-tasmota.

As I am not sure what input mode the sonoff uses, I inserted a transistor (BC546) in between the HC-501 output and the sonoff basic GPIO14. The PIR switches GPIO14 to low when active. The HC-501 is powered via the 5v hack by the sonoff (interrupt RF solder pin near U2 of sonoff basic from parts side and then bridge the nearby u2 pin (1117-3) to the now free pin).

HC-501                                             sonoff basic
VCC <----------+------------ U2 (out pin of 1117-3)
               |
               V LED
              ---
               |
               -
              2k2
               _
               |
            c  +-----------> GPIO14
            b /
out >-[4k7]--< BC546
              \
            e  Y
               |
GND -----------+----------- GND


The only issue I have is that the SwithModes do not really do invert! So SwitchMode 1 and 2 behave the same (as SwitchMode 3 and 4). I am using a SwitchTopic 'switch02'. If PIR is active, it raises the out pin to ~3V and the transistor is switched on. The LED lights and GPIO14 will be switched LOW.
Regardless os SwitchMode is set to 1 or 2, the sonoff sends MQTT /cmnd/switch02/POWER2 OFF for this LOW input on GPIO14.
For the sonoff tasmota I configured "GPIO14 sensor" to "10 switch2".

The above works for me, except for having to invert my code to use OFF as active and ON as inactive PIR sense.

I assume GPIO14 is internally pulled-up (by tasmota code?). But that does not explain, why is does not change from ON/OFF to OFF/ON when using Switchmode 2 instead of 1.

~Josef

Gijs Noorlander

unread,
Oct 15, 2017, 4:08:17 AM10/15/17
to SonoffUsers
I think you do want to set it to Switchmode 3 or 4, described in this section of the wiki: https://github.com/arendst/Sonoff-Tasmota/wiki/Understanding-SwitchMode-and-SwitchTopic
Then you only get one response, which makes it easier to debug.
About the pull-up. Your schema does the pull up via the LED + resistor, even when the internal pull-down is activated. Although I would expect a drop from Vcc to be about 2V (drop over the LED, depending on its color). So what is Vcc? 5V or 3.3V?
The internal pull-up and pull-down resistors have a much higher resistance (100k???) so they are easy to override via external resistors.
You are now using an NPN transistor. If you want to change its logic, you should use a PNP transistor.
Only thing is, you are now "protected" by the voltage drop of the LED, when using Vcc of 5V, so you should place the LED between Vcc and the transistor. (between the Emitter and Vcc of the PNP transistor)
This drop will take about 1.8 ... 2 V from the Vcc, which is about 3 .. 3.2V when using 5V as Vcc.  (ESP8266 is not 5V tolerant)


But still the easiest way should be to invert the behavior in Tasmota, which is where the switchmodes are for.

Michał Tomasik

unread,
Jan 6, 2018, 12:56:47 AM1/6/18
to SonoffUsers
Guys,
can you please tell me how to connect additional 5Vrelay to Sonoff tasmota and make it works?

Mike Roberts

unread,
Jan 6, 2018, 4:00:12 AM1/6/18
to SonoffUsers
Michal

You will have to find a spare GPIO to use.  The guide will show you how to set it up. You will also need a 5v supply and a transistor/MOSFET driver e.g a 2N7002  (the ones used in the guide are rated higher for when these outputs drive a LED string).
Mike

Yann Mengin

unread,
Jun 13, 2018, 4:56:03 AM6/13/18
to SonoffUsers
Hi Pascal

I am using Home Assistant and I am trying to do what you describe with Domoticz. I have configure the SONOFF switch GPIO01 to Serial Out (as switch 2) and GPIO14 as Sensor (Switch 1).

When I move in front of the sensor I can see  an MQTT switch defined in home assistant changing status  (OFF to ON)  but returning to OFF immediately. How can I define MQTT switch or binary sensor linked to Switch 1 or Switch 2 above?

Help appreciated
.
Thanks

Angelo Santagata

unread,
Jun 13, 2018, 6:54:24 AM6/13/18
to Yann Mengin, SonoffUsers
hey 

I use the following to hook the PIR sensor to the RF433 bridge to HA

mqtt:
  broker: 192.168.0.204

sensor rfbridge:
  - platform: mqtt
    name: "RFBridge"
    state_topic: "tele/sonoffRFridge/STATE"

sensor rfbridgeResult:
  - platform: mqtt
    name: "RFBridgeResult"
    state_topic: "tele/sonoffRFridge/RESULT"
    # value_template:  '{{ value_json["RfReceived"]["RfKey"] }}'
    value_template:  '{{ value_json.RfReceived.RfKey }}'
    expire_after: 20
    json_attributes:
      - RfReceived


I think the magic bit you want is the "expires_after" predicate

--
You received this message because you are subscribed to the Google Groups "SonoffUsers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonoffusers+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/sonoffusers.
To view this discussion on the web, visit https://groups.google.com/d/msgid/sonoffusers/59b7b444-347c-4d1b-b0f6-80d97cd71937%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


Angelo

Yann Mengin

unread,
Jun 13, 2018, 11:36:53 AM6/13/18
to Angelo Santagata, Yann Mengin (Gmail), SonoffUsers
Thank you Angelo!

I’ll study that (while I am also trying to figure out by using “sensors” and automation rules). I’m almost there
Reply all
Reply to author
Forward
0 new messages