Alan Wang
Published © CC BY-NC-SA

Raspberry Pi Zero Flash Cam

Make an action camera in a vintage flash unit with Raspberry Pi Zero and camera module.

IntermediateShowcase (no instructions)1,618
Raspberry Pi Zero Flash Cam

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Micro USB OTG hub
×1
LED (generic)
LED (generic)
×2
5V2A USB power bank
×1
Pi Zero camera
×1
Touch sensor
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Schematics

Raspberry Pi Zero Flash Cam

Code

Raspberry Pi Zero Flash Cam

Python
# Pi Zero Flash Cam
from picamera import PiCamera, Color
from gpiozero import Button, LED
import os
import time

usb_path = "/mnt/mydisk/"

# setup button (touchpad) and two LEDs
button = Button(18)
greenLED = LED(23)
redLED = LED(24)

# initialize camera and LEDs
camera = PiCamera()
camera.resolution = (1280, 720)
camera.framerate = 30
camera.video_stabilization = True
camera.meter_mode = "matrix"
camera.image_effect = "film"
camera.annotate_text_size = 24
camera.annotate_foreground = Color('white')
camera.annotate_background = Color('blue')
greenLED.on()
redLED.off()
print("Camera standby")

# main program
while True:

    # when user pressed the touch sensor (returns 1; button.is_pressed=0)
    if not button.is_pressed:
        
        # wait until user released touch sensor
        while not button.is_pressed:
            pass

        try:
            # get current time
            filename = time.strftime("%Y%m%d%H%M%S", time.localtime())
            nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

            # setp PiCamera and record video for 12 seconds
            camera.annotate_text = "Pi Zero Flash Cam / " + nowtime
            greenLED.off()
            redLED.on()
            print("Start recording...")
            camera.start_recording("/home/pi/tmp.h264")
            camera.wait_recording(12)
            camera.stop_recording()
            redLED.off()
            print("End recording")

            # if user's USB disk mounted, output MP4 to USB
            # automount USB in terminal mode: https://www.raspberrypi.org/documentation/configuration/external-storage.md
            print("Output MP4...")
            if os.path.exists(usb_path):
                os.system("MP4Box -add /home/pi/tmp.h264 -fps 30 -noprog " + usb_path + filename + ".mp4")
                print("MP4 outputed to USB disk.")
            else:
                os.system("MP4Box -add /home/pi/tmp.h264 -fps 30 -noprog /home/pi/" + filename + ".mp4")
                print("USB disk not found. MP4 outputed to system disk.")

            # blink green LED as successful
            for i in range(2):
                greenLED.on()
                time.sleep(0.25)
                greenLED.off()
                time.sleep(0.25)

        except Exception as ex:
            print("Video capture/output error:")
            print(type(ex).__name__)
            print(ex.args)
            # blink red LED as failed
            for i in range(5):
                redLED.on()
                time.sleep(0.1)
                redLED.off()
                time.sleep(0.1)

        print("Camera standby")
        greenLED.on()

    time.sleep(0.1)

Credits

Alan Wang

Alan Wang

16 projects • 23 followers
Former book translator and amateur film photographer

Comments