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 [here] and join today!

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.

../_images/4.1.1_camera_list.png

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Raphael Kit

337

Raphael Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

GPIO Extension Board

BUY

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

LED

BUY

Button

BUY

Camera Module

BUY

Schematic Diagram

T-Board Name

physical

wiringPi

BCM

GPIO17

Pin 11

0

17

GPIO18

Pin 12

1

18

../_images/4.1.1_camera_schematic.png

Experimental Procedures

Step 1: Build the circuit.

../_images/4.1.1_camera_circuit.png

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 Remote Desktop.

Step 3: Open a Terminal and get into the folder of the code.

cd ~/raphael-kit/python-pi5

Step 4: Run.

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 FileZilla Software.

Warning

If there is an error prompt RuntimeError: Cannot determine SOC peripheral base address, please refer to If gpiozero doesn’t work.

Code

#!/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

  1. Imports necessary libraries for time handling, camera control, and GPIO component control.

    #!/usr/bin/env python3
    from picamera2 import Picamera2, Preview
    from gpiozero import LED, Button
    import time
    import os
    
  2. Retrieves the current user’s login name and home directory for saving photos.

    # Get the current user's login name and home directory
    user = os.getlogin()
    user_home = os.path.expanduser(f'~{user}')
    
  3. Initializes the camera and starts it.

    # Initialize the camera
    camera = Picamera2()
    camera.start()
    
  4. Declares status as a global variable and initializes it to False.

    # Initialize a variable to track the camera's status
    global status
    status = False
    
  5. Initializes an LED connected to GPIO pin 17 and a button connected to GPIO pin 18.

    # Set up LED and button with their GPIO pin numbers
    led = LED(17)
    button = Button(18)
    
  6. Defines a function takePhotos that sets the global variable status to True when the button is pressed.

    def takePhotos(pin):
        """Function to set the camera's status to True when the button is pressed."""
        global status
        status = True
    
  7. Assigns the takePhotos function to be called when the button is pressed.

    try:
        # Assign the function to be called when the button is pressed
        button.when_pressed = takePhotos
    
        ...
    
  8. 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.

    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)
    
  9. Catches a KeyboardInterrupt (like Ctrl+C) and stops the camera preview and turns off the LED before exiting.

    except KeyboardInterrupt:
        # Stop the camera and turn off the LED if a KeyboardInterrupt occurs
        camera.stop_preview()
        led.off()
        pass