Raspberry Pi Camera & Ultrasonic With Python code
Raspberry Pi Project with RPI Camera to Take Photo & Video By Ultrasonic Sensor
At a certain distance between the ultrasonic sensor and the approaching object, will give command to camera to take photo or video for any thing become near the device.
Python 3.7 Code:
#NSR Information Systems - www.nsris.com
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
from picamera import PiCamera
from time import sleep
camera = PiCamera()
TRIG = 5
ECHO = 17
print("Distance Measurement In Progress")
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(18,GPIO.OUT)
def measure():
GPIO.output(TRIG, False)
print("Waiting For Sensor To Settle")
time.sleep(.5)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print("Distance:",distance,"cm")
if distance < 280 :
print("LED on")
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print("LED off")
GPIO.output(18,GPIO.LOW)
timestr = time.strftime("%Y-%m-%d %H:%M:%S")
camera.start_preview()
sleep(5)
camera.capture("/home/pi/Desktop/Camera/{0}.jpg".format(timestr))
camera.stop_preview()
camera.start_preview()
camera.start_recording("/home/pi/Desktop/Camera/{0}.h264".format(timestr))
sleep(5)
camera.stop_recording()
camera.stop_preview()
while True:
measure()
- Hits: 3521