.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _4.1.8_py_pi5: 4.1.5 Welcome ===================================== Introduction ------------- In this project, we will use PIR to sense the movement of pedestrians, and use servos, LED, buzzer to simulate the work of the sensor door of the convenience store. When the pedestrian appears within the sensing range of the PIR, the indicator light will be on, the door will be opened, and the buzzer will play the opening bell. Required Components ------------------------------ In this project, we need the following components. .. image:: ../python_pi5/img/4.1.8_welcome_list.png :width: 800 :align: center It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - Raphael Kit - 337 - |link_Raphael_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_gpio_board` - |link_gpio_board_buy| * - :ref:`cpn_breadboard` - |link_breadboard_buy| * - :ref:`cpn_wires` - |link_wires_buy| * - :ref:`cpn_resistor` - |link_resistor_buy| * - :ref:`cpn_led` - |link_led_buy| * - :ref:`cpn_pir` - \- * - :ref:`cpn_servo` - |link_servo_buy| * - :ref:`cpn_buzzer` - |link_passive_buzzer_buy| * - :ref:`cpn_transistor` - |link_transistor_buy| Schematic Diagram ------------------- ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO18 Pin 12 1 18 GPIO17 Pin 11 0 17 GPIO27 Pin 13 2 27 GPIO22 Pin 15 3 22 ============ ======== ======== === .. image:: ../python_pi5/img/4.1.8_welcome_schematic.png :align: center Experimental Procedures ------------------------- **Step 1:** Build the circuit. .. image:: ../python_pi5/img/4.1.8_welcome_circuit.png :align: center **Step 2:** Change directory. .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **Step 3:** Run. .. raw:: html .. code-block:: sudo python3 4.1.8_Welcome_zero.py After the code runs, if the PIR sensor detects someone passing by, the door will automatically open (simulated by the servo), turn on the indicator and play the doorbell music. After the doorbell music plays, the system will automatically close the door and turn off the indicator light, waiting for the next time someone passes by. There are two potentiometers on the PIR module: one is to adjust sensitivity and the other is to adjust the detection distance. To make the PIR module work better, you You need to turn both of them counterclockwise to the end. .. image:: ../python_pi5/img/4.1.8_PIR_TTE.png :width: 400 :align: center .. warning:: If there is an error prompt ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc` **Code** .. note:: You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to source code path like ``raphael-kit/python-pi5``. After modifying the code, you can run it directly to see the effect. .. raw:: html .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, MotionSensor, Servo, TonalBuzzer import time # GPIO pin setup for LED, motion sensor (PIR), and buzzer ledPin = LED(6) pirPin = MotionSensor(21) buzPin = TonalBuzzer(27) # Servo motor pulse width correction factor and calculation myCorrection = 0.45 maxPW = (2.0 + myCorrection) / 1000 # Maximum pulse width minPW = (1.0 - myCorrection) / 1000 # Minimum pulse width # Initialize servo with custom pulse widths servoPin = Servo(25, min_pulse_width=minPW, max_pulse_width=maxPW) # Musical tune for buzzer, with notes and durations tune = [('C#4', 0.2), ('D4', 0.2), (None, 0.2), ('Eb4', 0.2), ('E4', 0.2), (None, 0.6), ('F#4', 0.2), ('G4', 0.2), (None, 0.6), ('Eb4', 0.2), ('E4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('C4', 0.2), ('B4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('B4', 0.2), ('Bb4', 0.5), (None, 0.6), ('A4', 0.2), ('G4', 0.2), ('E4', 0.2), ('D4', 0.2), ('E4', 0.2)] def setAngle(angle): """ Move the servo to a specified angle. :param angle: Angle in degrees (0-180). """ value = float(angle / 180) # Convert angle to servo value servoPin.value = value # Set servo position time.sleep(0.001) # Short delay for servo movement def doorbell(): """ Play a musical tune using the buzzer. """ for note, duration in tune: buzPin.play(note) # Play the note time.sleep(float(duration)) # Duration of the note buzPin.stop() # Stop buzzer after playing the tune def closedoor(): # Turn off LED and move servo to close door ledPin.off() for i in range(180, -1, -1): setAngle(i) # Move servo from 180 to 0 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait after closing door def opendoor(): # Turn on LED, open door (move servo), play tune, close door ledPin.on() for i in range(0, 181): setAngle(i) # Move servo from 0 to 180 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait before playing the tune doorbell() # Play the doorbell tune closedoor() # Close the door after the tune def loop(): # Main loop to check for motion and operate door while True: if pirPin.motion_detected: opendoor() # Open door if motion detected time.sleep(0.1) # Short delay in loop try: loop() except KeyboardInterrupt: # Clean up GPIO on user interrupt (e.g., Ctrl+C) buzPin.stop() ledPin.off() **Code Explanation** #. The script begins by importing necessary modules. The ``gpiozero`` library is used to interface with the LED, motion sensor, servo motor, and tonal buzzer. The ``time`` module is for handling timing-related functions. .. code-block:: python #!/usr/bin/env python3 from gpiozero import LED, MotionSensor, Servo, TonalBuzzer import time #. Initializes the LED, PIR motion sensor, and tonal buzzer on their respective GPIO pins. .. code-block:: python # GPIO pin setup for LED, motion sensor (PIR), and buzzer ledPin = LED(6) pirPin = MotionSensor(21) buzPin = TonalBuzzer(27) #. Calculates the maximum and minimum pulse widths for the servo motor, incorporating a correction factor for fine-tuning. .. code-block:: python # Servo motor pulse width correction factor and calculation myCorrection = 0.45 maxPW = (2.0 + myCorrection) / 1000 # Maximum pulse width minPW = (1.0 - myCorrection) / 1000 # Minimum pulse width #. Initializes the servo motor on GPIO pin 25 with the custom pulse widths for accurate positioning. .. code-block:: python # Initialize servo with custom pulse widths servoPin = Servo(25, min_pulse_width=minPW, max_pulse_width=maxPW) #. The tune is defined as a sequence of notes (frequency) and durations (seconds). .. code-block:: python # Musical tune for buzzer, with notes and durations tune = [('C#4', 0.2), ('D4', 0.2), (None, 0.2), ('Eb4', 0.2), ('E4', 0.2), (None, 0.6), ('F#4', 0.2), ('G4', 0.2), (None, 0.6), ('Eb4', 0.2), ('E4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('C4', 0.2), ('B4', 0.2), (None, 0.2), ('F#4', 0.2), ('G4', 0.2), (None, 0.2), ('B4', 0.2), ('Bb4', 0.5), (None, 0.6), ('A4', 0.2), ('G4', 0.2), ('E4', 0.2), ('D4', 0.2), ('E4', 0.2)] #. Function to move the servo to a specified angle. Converts the angle to a value between 0 and 1 for the servo. .. code-block:: python def setAngle(angle): """ Move the servo to a specified angle. :param angle: Angle in degrees (0-180). """ value = float(angle / 180) # Convert angle to servo value servoPin.value = value # Set servo position time.sleep(0.001) # Short delay for servo movement #. Function to play a musical tune using the buzzer. Iterates through the ``tune`` list, playing each note for its specified duration. .. code-block:: python def doorbell(): """ Play a musical tune using the buzzer. """ for note, duration in tune: buzPin.play(note) # Play the note time.sleep(float(duration)) # Duration of the note buzPin.stop() # Stop buzzer after playing the tune #. Functions to open and close the door using the servo motor. The ``opendoor`` function turns on the LED, opens the door, plays the tune, and then closes the door. .. code-block:: python def closedoor(): # Turn off LED and move servo to close door ledPin.off() for i in range(180, -1, -1): setAngle(i) # Move servo from 180 to 0 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait after closing door def opendoor(): # Turn on LED, open door (move servo), play tune, close door ledPin.on() for i in range(0, 181): setAngle(i) # Move servo from 0 to 180 degrees time.sleep(0.001) # Short delay for smooth movement time.sleep(1) # Wait before playing the tune doorbell() # Play the doorbell tune closedoor() # Close the door after the tune #. Main loop that constantly checks for motion detection. When motion is detected, it triggers the ``opendoor`` function. .. code-block:: python def loop(): # Main loop to check for motion and operate door while True: if pirPin.motion_detected: opendoor() # Open door if motion detected time.sleep(0.1) # Short delay in loop #. Executes the main loop and ensures that the script can be stopped with a keyboard command (Ctrl+C), turning off the buzzer and LED for a clean exit. .. code-block:: python try: loop() except KeyboardInterrupt: # Clean up GPIO on user interrupt (e.g., Ctrl+C) buzPin.stop() ledPin.off()