.. 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.1_py_pi5: 4.1.1 Camera =================== Introduction ----------------- Here we will make a camera with a shutter, when you press the button, the camera shoots while the LED flashes. Required Components ------------------------------ In this project, we need the following components. .. image:: ../python_pi5/img/4.1.1_camera_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_button` - |link_button_buy| * - :ref:`cpn_camera_module` - |link_camera_buy| Schematic Diagram ----------------------- ============ ======== ======== === T-Board Name physical wiringPi BCM GPIO17 Pin 11 0 17 GPIO18 Pin 12 1 18 ============ ======== ======== === .. image:: ../python_pi5/img/4.1.1_camera_schematic.png :align: center Experimental Procedures ------------------------------ **Step 1:** Build the circuit. .. image:: ../python_pi5/img/4.1.1_camera_circuit.png :width: 800 :align: center **Step 2:** Go into the Raspberry Pi Desktop. You may need a screen for a better experience, refer to: `Connect your Raspberry Pi `_. Or access the Raspberry Pi desktop remotely, for a detailed tutorial please refer to :ref:`remote_desktop`. **Step 3:** Open a Terminal and get into the folder of the code. .. raw:: html .. code-block:: cd ~/raphael-kit/python-pi5 **Step 4:** Run. .. raw:: html .. code-block:: sudo python3 4.1.1_Camera_zero.py After the code runs, press the button, the Raspberry Pi will flash the LED and take a picture. The photo will be named ``my_photo.jpg`` and stored in the ``~`` directory. .. note:: You can also open ``4.1.1_Camera_zero.py`` in the ``~/raphael-kit/python/`` path with a Python IDE, click Run button to run, and stop the code with Stop button. If you want to download the photo to your PC, please refer to :ref:`filezilla`. .. warning:: If there is an error prompt ``RuntimeError: Cannot determine SOC peripheral base address``, please refer to :ref:`faq_soc` **Code** .. code-block:: python #!/usr/bin/env python3 from picamera2 import Picamera2, Preview from gpiozero import LED, Button import time import os # Get the current user's login name and home directory user = os.getlogin() user_home = os.path.expanduser(f'~{user}') # Initialize the camera camera = Picamera2() camera.start() # Initialize a variable to track the camera's status global status status = False # Set up LED and button with their GPIO pin numbers led = LED(17) button = Button(18) def takePhotos(pin): """Function to set the camera's status to True when the button is pressed.""" global status status = True try: # Assign the function to be called when the button is pressed button.when_pressed = takePhotos # Main loop while True: # Check if the button has been pressed if status: # Blink the LED five times for i in range(5): led.on() time.sleep(0.1) led.off() time.sleep(0.1) # Capture and save a photo camera.capture_file(f'{user_home}/my_photo.jpg') print('Take a photo!') # Reset the status status = False else: # Turn off the LED if not capturing led.off() # Wait for a short period before checking the button status again time.sleep(1) except KeyboardInterrupt: # Stop the camera and turn off the LED if a KeyboardInterrupt occurs camera.stop_preview() led.off() pass **Code Explanation** #. Imports necessary libraries for time handling, camera control, and GPIO component control. .. code-block:: python #!/usr/bin/env python3 from picamera2 import Picamera2, Preview from gpiozero import LED, Button import time import os #. Retrieves the current user's login name and home directory for saving photos. .. code-block:: python # Get the current user's login name and home directory user = os.getlogin() user_home = os.path.expanduser(f'~{user}') #. Initializes the camera and starts it. .. code-block:: python # Initialize the camera camera = Picamera2() camera.start() #. Declares ``status`` as a global variable and initializes it to ``False``. .. code-block:: python # Initialize a variable to track the camera's status global status status = False #. Initializes an LED connected to GPIO pin 17 and a button connected to GPIO pin 18. .. code-block:: python # Set up LED and button with their GPIO pin numbers led = LED(17) button = Button(18) #. Defines a function ``takePhotos`` that sets the global variable ``status`` to ``True`` when the button is pressed. .. code-block:: python def takePhotos(pin): """Function to set the camera's status to True when the button is pressed.""" global status status = True #. Assigns the ``takePhotos`` function to be called when the button is pressed. .. code-block:: python try: # Assign the function to be called when the button is pressed button.when_pressed = takePhotos ... #. Continuously checks if the ``status`` is ``True``. If so, it blinks the LED five times, captures a photo, and resets ``status``. If not, the LED remains off. There is a 1-second delay between each loop iteration. .. code-block:: python try: ... # Main loop while True: # Check if the button has been pressed if status: # Blink the LED five times for i in range(5): led.on() time.sleep(0.1) led.off() time.sleep(0.1) # Capture and save a photo camera.capture_file(f'{user_home}/my_photo.jpg') print('Take a photo!') # Reset the status status = False else: # Turn off the LED if not capturing led.off() # Wait for a short period before checking the button status again time.sleep(1) #. Catches a KeyboardInterrupt (like Ctrl+C) and stops the camera preview and turns off the LED before exiting. .. code-block:: python except KeyboardInterrupt: # Stop the camera and turn off the LED if a KeyboardInterrupt occurs camera.stop_preview() led.off() pass