MOCKSTACKS
EN
Questions And Answers

More Tutorials









Raspberry Control Servo Rotation with PIR Motion Sensor


Next, we will use the PIR Motion Sensor to control the Servo to realize the automatic door.

Hardware Connection


In this project, the electronic hardware we need to use is as follows:

• Raspberry Pi Pico

• Grove Shield for Pi Pico

• Grove – Servo

• Grove – PIR Motion Sensor


Connect the PIR Motion Sensor to D18, and connect the Servo to D20.

Write a Program


First, introduce the required library, and define the pins of the Servo and the PIR Motion Sensor. Here, we refer to the newly built servo library.

1 from machine import Pin 
2 from utime import sleep 
3 from servo import SERVO 
4 servo = SERVO(Pin(20)) 
5 miniPir = Pin(18, Pin.IN)

There are only two states of the PIR Motion Sensor, triggered and not triggered. Therefore, we only need to judge whether the returned value is 1. If so, we print “Motion Detected" in the Shell area to indicate that the sensor is triggered.

When the Servo turns to 160° , it means that the door is open. After waiting for 10 seconds, the door will close automatically. The complete program is as follows:

1 from machine import Pin 
2 from utime import sleep
3 from servo import SERVO 
4 servo = SERVO(Pin(20)) 
5 miniPir = Pin(18, Pin.IN)
6
7 while True:
if minipir.value() == 1:
print('Motion Detected') 
servo.turn(160) 
sleep (10)
servo. turn(20)

Run the program, save it in any location of the computer. Wave your hand around the PIR
Motion Sensor to see the effect of the Servo rotation.

Conclusion

In this page (written and validated by ) you learned about Raspberry Control Servo Rotation with PIR Motion Sensor . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Welcome Device.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.