Taking a Photo with the Raspberry Pi on Pushing a Button
Using a tactile button to trigger the Raspberry Pi to take a photo
Firstly, set up the tactile button, wires, and raspberry pi. The left wire from the button to the GPIO17 on the Pi and then the right to a ground. Install the camera module via the camera port next to the ethernet port.
Test installation is correct with the following:
libcamera-hello
Get it to save a photo
lib camera-hello -o image.jpg
Test the circuit out through the following code:
from RPi import GPIO
import time
import datetime
from picamera2 import Picamera2, Preview
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,GPIO.PUD_UP)
while True:
GPIO.wait_for_edge(17,GPIO.FALLING)
print("button")
time.sleep(0.5)
picam2 = Picamera2()
camera_config = picam2.create_preview_configuration()
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL)
picam2.start()
time.sleep(5)
current_datetime = datetime.datetime.now()
picam2.capture_file(f"{current_datetime}.jpg")
What we are doing here is importing the Pi Camera and the GPIO library. Next, we are ensuring we can access the camera. Then starting a preview.
We set the GPIO using the Broadcom convention to address by the GPIO number, rather than board number. We configure the pin to be an input and set it to be pulled up. The wait_for_edge() takes the PIN number as the first argument and then GPIO_falling as the second so that the function is triggered when the signal goes from high to low. The camera then takes a photo and saves the file. Finally, stopping the preview.
Note: If the picture comes out upside down the add the following:
picam2.hflip = True
picam2.vflip = True
Some serious selfies, just