B4R Code Snippet Using a PIR to rotate a servo (Emulating PIR opening/closing powered doors)

SubName: Reading PIR sensor on digital pin 5 to rotate a servo on digital pin 4.
Description: Basically this example is emulating opening and closing powered doors like in a shopping centre (shopping mall), superstore etc.

You can use this simple code to monitor PIR sensors HC-SR501 or HC-SR505 (tested on both). I'm using the PIR to rotate a servo 90° (door will open) for 10 seconds by using a timer. As long as the PIR sensor senses motion, the servo will stay rotated at 90° (door open). When the PIR sensor no longer senses motion the servo will rotate back to 0° (door closed). If whilst the servo is rotating back to 0° (door is closing) the PIR sensor suddenly detects motion again, the servo will instantly stop rotating (stop closing the door) and the servo will reverse direction (opening the door again).

PLEASE NOTE: There are many ways to achieve the same results, using a timer is just one way.

********************* PROGRAM STARTING ****************
AppStart
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door open - 90°
Update in 10000ms
Door open - 90°
Update in 10000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door open - 90°
Update in 10000ms

Libraries needed:
1617673843150.png


B4X:
'WIRE LEGEND Servo and PIR
'GND = GND
'VCC = 5V
'Servo s = Digital Pin 4
'PIR s = Digital Pin 5

'*************************
'***     BOARD TYPE    ***
'***        UNO        ***
'*************************

Sub Process_Globals
    Public Serial1 As Serial
    Private Const ServoSpeedDelay = 40, ServoAngle = 90 As Int    'Servo rotation delay speed, Servo opening angle

    Private Servo, PIR, LED As Pin
    Private SrvDoor As Servo
    Private TmrPIRSense As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    Servo.Initialize(4, Servo.MODE_INPUT)    'Servo on Pin 4
    SrvDoor.Attach(Servo.PinNumber)
    SrvDoor.Write(0)

    PIR.Initialize(5, PIR.MODE_INPUT)        'PIR on Pin 5
    LED.Initialize(13, LED.MODE_OUTPUT)        'UNO LED on Pin 13

    TmrPIRSense.Initialize("PIRSense_Tick", 1000)    'Set the timer to check for motion every 1 second
    TmrPIRSense.Enabled = True                        'Enable the timer
End Sub

Sub PIRSense_Tick
    TmrPIRSense.Enabled = False                'Disable the timer
    If PIR.DigitalRead Then                    'Check for motion
        LED.DigitalWrite(True)                'Turn LED On when the door is open
        TmrPIRSense.Interval = 10000        'Set the timer to 10 seconds (People are walking through the door)
        Do While SrvDoor.Read < ServoAngle    'Check servo angle/door
            Delay(ServoSpeedDelay)            'Delay to slow down the servo speed (Door opening speed)
            SrvDoor.Write(SrvDoor.Read + 1)    'Rotate the servo (Open the door)
        Loop
    Else
        LED.DigitalWrite(False)                'Turn LED Off when the door is closed
        TmrPIRSense.Interval = 1000            'Set the timer back to 1 second    
        Do While SrvDoor.Read > 0            'Check servo angle
            If PIR.DigitalRead Then            'If motion sensed whilst servo(door) is closing then EMERGENCY...
                TmrPIRSense.Interval = 0    'Set the timer to open the door instantly
                TmrPIRSense.Enabled = True    'Enable the timer
                Return                        'SAFETY STOP - Jump out of the sub to stop closing the door
            End If
            Delay(ServoSpeedDelay)            'Delay to slow down the servo speed (Door closing speed)
            SrvDoor.Write(SrvDoor.Read - 1)    'Rotate the servo (Close the door)
        Loop
    End If
    TmrPIRSense.Enabled = True                'Enable the timer

    If SrvDoor.Read = 0 Then Log("Door closed - ", SrvDoor.Read, "°") Else Log("Door open - ", SrvDoor.Read, "°")
    Log("Update in ", TmrPIRSense.Interval, "ms")
End Sub

Arduino wiring
PIR Servo_bb.png


Tested on both the HC-SR505 and HC-SR501 PIR Sensors
Untitled.jpg



Enjoy...
 
Last edited:

Beja

Expert
Licensed User
Longtime User
SubName: Reading PIR sensor on digital pin 5 to rotate a servo on digital pin 4.
Description: Basically this example is emulating opening and closing powered doors like in a shopping centre.

You can use this simple code to monitor PIR sensors (SR-501 or SR-505 (tested on both)). I'm using the PIR to rotate a servo 90° (door open) for 10 seconds by using a timer. As long as the PIR sensor senses motion, the servo will stay rotated at a 90° (door open). When the PIR sensor no longer senses motion, the servo will rotate back to 0° (door closed).

PLEASE NOTE: There are many ways to achieve the same results, this is just one way.

********************* PROGRAM STARTING ****************
AppStart
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door open - 90°
Update in 10000ms
Door open - 90°
Update in 10000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door closed - 0°
Update in 1000ms
Door open - 90°
Update in 10000ms

B4X:
'WIRE LEGEND Servo and PIR
'GND = GND
'VCC = 5V
'Servo s = Digital Pin 4
'PIR s = Digital Pin 5

'*************************
'***     BOARD TYPE    ***
'***        UNO        ***
'*************************

Sub Process_Globals
    Public Serial1 As Serial

    Private Servo, PIR, LED As Pin
    Private SrvDoor As Servo
    Private TmrPIRSense As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    Servo.Initialize(4, Servo.MODE_INPUT)    'Servo on Pin 4
    SrvDoor.Attach(Servo.PinNumber)
    SrvDoor.Write(0) 

    PIR.Initialize(5, PIR.MODE_INPUT)        'PIR on Pin 5
    LED.Initialize(13, LED.MODE_OUTPUT)        'UNO LED on Pin 13

    TmrPIRSense.Initialize("PIRSense_Tick", 1000)    'Set the timer to check for motion every 1 second
    TmrPIRSense.Enabled = True                'Enable the timer
End Sub

Sub PIRSense_Tick
    TmrPIRSense.Enabled = False                'Disable the timer
    If PIR.DigitalRead Then                    'Check for motion
        SrvDoor.Write(90)                    'Open the servo/door
        LED.DigitalWrite(True)                'Turn LED On when the door is open
        TmrPIRSense.Interval = 10000        'Set the timer to 10 seconds (People walking through a door)
    Else
        SrvDoor.Write(0)                    'Close the servo/door
        LED.DigitalWrite(False)                'Turn LED Off when the door is closed
        TmrPIRSense.Interval = 1000            'Set the timer back to 1 second
    End If
    TmrPIRSense.Enabled = True                'Enable the timer
    If SrvDoor.Read = 0 Then Log("Door closed - ", SrvDoor.Read, "°") Else Log("Door open - ", SrvDoor.Read, "°")
    Log("Update in ", TmrPIRSense.Interval, "ms")
End Sub

Arduino wiring
View attachment 111074


Enjoy...

Thank you Peter, I will test this example.. many will benefit from it. already saved it.
I have a feeling that the pins on SR505 are different.. this is the problem (the SR505).. the project I am working on is if sensor then the door will open, and will stay open until sensor signal is removed by the visitor going away.. sorry for not explaining it correctly. the sensor in this example is not SR505.. are the pins compatible?
I would appreciate it if you kindly put the SR505 schematic
 

Peter Simpson

Expert
Licensed User
Longtime User
I would appreciate it if you kindly put the SR505 schematic

Hello @Beja
As I said in the PM, I've tested this code with both the HC-SR501 and HC-SR505, the results were basically the same (as far as I'm concerned). The reason why I used the HC-SR501 image above is simply because Fritzing does not have the HC-SR505 image as standard, otherwise I would have used the HC-SR505 image instead.

The HC-SR505 has a sense time delay of 8 seconds +/- 30%, I'm setting the code delay above to wait 10 seconds which is more than enough time according to both my HC-SR501 and HC-SR505 tests.

Anyway, I've updated the code above to both slow down the servo rotation speed and to also stop rotating the servo (stop closing the door) if motion is detected whilst the rotating/closing, thus nobody would get chopped in half ;)

I have a question for you, are you 100% sure that your hardware is working correctly???

I'm sending you a PM...
 
Last edited:

Beja

Expert
Licensed User
Longtime User
Hello @Beja
As I said in the PM, I've tested this code with both the SR501 and SR505, the results were basically the same (as far as I'm concerned). The reason why I used the SR501 image above is simply because Fritzing does not have the SR505 image as standard (otherwise I would have used the SR505 image).

The SR505 has a re-sense time delay of 8 seconds +/- 30%, I'm setting the code delay above to 10 seconds which is more than enough time according to both my SR501 and SR505 tests.

Anyway, I've updated the code above to both slow down the servo rotation speed and to also stop rotating the servo (stop closing the door) if motion is detected whilst the rotating/closing, thus nobody would get chopped in half ;)

I have a question for you, are you 100% sure that your hardware is working correctly???

I'm sending you a PM...

Dear Peter, thanks and again, no talk about the software at all. the code is working perfectly.. If there're words that could express this more I would have used them.
The problem (with my project) is the hardware, not with the code.. the code is perfect.. very perfect. That's why I asked for the schematic to see how it's connected.
The connections I have now for the 505 is: (with the 8-pin smt chip and two yellow capacitors (or chips) facing you...
connecting the right pin to Vcc, the let to gnd and the middle to pin 5.. this connection burned one PIR sensor so far. So it's either all 5 sensors that I bought yesterday from Amazon are defective, or the 505 pinout is not compatible with the 501.
 

Beja

Expert
Licensed User
Longtime User
Hi Pete,
It's working like a charm.. problem was my 5v power supply.. so much noise. Thank you for the great code snippet/tutorial.
 

Peter Simpson

Expert
Licensed User
Longtime User
Hello @Beja
...
I have a question for you, are you 100% sure that your hardware is working correctly???
...
Hi Pete,
It's working like a charm.. problem was my 5v power supply.. so much noise. Thank you for the great code snippet/tutorial.


My response
Hmm, so you had a hardware issue ;)
I'm pleased that you've finally got it working ?

P.S. According to your question in the forum, you owe me $50 as I already send you a working solution last week lol ???
Only kidding about the $50. You saying that the above code is working for you is thanks enough for me.


Enjoy...
 

Beja

Expert
Licensed User
Longtime User
My response
Hmm, so you had a hardware issue ;)


Enjoy...

Yep! if you go back to all of my messages you will see that I was talking about h/w problem.
Please send me your PayPal or Zelle account . I may need you for technical support ?
 

Peter Simpson

Expert
Licensed User
Longtime User
...
Please send me your PayPal or Zelle account . I may need you for technical support ?
Sorry I can't do that. The initial test code I wrote for you last week I wrote without testing and with no hardware to hand. The fact that it was working is just a bonus.

Enjoy...
 

Beja

Expert
Licensed User
Longtime User
Sorry I can't do that. The initial test code I wrote for you last week I wrote without testing and with no hardware to hand. The fact that it was working is just a bonus.

Enjoy...

Thanks for being so innocent.. I was replying to post #7.. just a joke.
Thank you for the solution and so many people on the forum and it's visitors will benefit from it.
 
Top