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/3.1.15camera_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/camera_schematic.png

Experimental Procedures

Step 1: Build the circuit.

../_images/3.1.15camera_fritzing.png

Step 2: To connect the camera module and complete the configuration, please refer to: Camera Module.

Step 3: 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 Access for Raspberry Pi.

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

cd ~/raphael-kit/python/

Step 5: Run.

sudo python3 4.1.1_Camera.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.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.

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. After modifying the code, you can run it directly to see the effect.

    #!/usr/bin/env python3

    from picamera import PiCamera
    import RPi.GPIO as GPIO
    import time
import os
user = os.getlogin()
user_home = os.path.expanduser(f'~{user}')


    camera = PiCamera()

    LedPin = 17 # Set GPIO17 as LED pin
    BtnPin = 18 # Set GPIO18 as button pin

    status = False

    def setup():
            GPIO.setmode(GPIO.BCM)
            GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
            GPIO.setup(BtnPin, GPIO.IN)
            camera.start_preview(alpha=200)

    def takePhotos(pin):
            global status
            status = True

    def main():
            global status
            GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=takePhotos)
            while True:
                    if status:
                            for i in range(5):
                                    GPIO.output(LedPin, GPIO.LOW)
                                    time.sleep(0.1)
                                    GPIO.output(LedPin, GPIO.HIGH)
                                    time.sleep(0.1)
                            camera.capture(f'{user_home}/my_photo.jpg')
                            print ('Take a photo!')
                            status = False
                    else:
                            GPIO.output(LedPin, GPIO.HIGH)
                    time.sleep(1)

    def destroy():
            camera.stop_preview()
            GPIO.output(LedPin, GPIO.HIGH)
            GPIO.cleanup()

    if __name__ == '__main__':
            setup()
            try:
                    main()
            except KeyboardInterrupt:
                    destroy()

Code Explanation

GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=takePhotos)

Set the event of BtnPin, when the button is pressed (the level signal changes from high to low) , call the function takePhotos().

def takePhotos(pin):
    global status
    status = True

When takePhotos() is called, modify the status to True.

if status:
    for i in range(5):
        GPIO.output(LedPin, GPIO.LOW)
        time.sleep(0.1)
        GPIO.output(LedPin, GPIO.HIGH)
        time.sleep(0.1)
    camera.capture(f'{user_home}/my_photo.jpg')
    print ('Take a photo!')
    status = False
else:
    GPIO.output(LedPin, GPIO.HIGH)
time.sleep(1)

When status is True, the Raspberry Pi will flash the LED and take a picture. The photo will be named my_photo.jpg and stored in the ~ directory.

Phenomenon Picture

../_images/4.1.1camera.JPG