Introduction: Distance Sensor Camera

This instructable is going to show you how to make a Distance Sensor Camera using a raspberry pi. This project will use the raspberry pi and use python 3 for the coding in this project The Distance Sensor Camera will first measure out 100 cm then will blink the RGB LED and will take the picture. Then to show that the photo was taken then the RGB LED will be a solid blue color. Then to access the photo you go to the desk top of the raspberry pi that the photo was taken on.

You will need:

  • 1x Raspberry Pi
  • 1x T-Cobbler
  • 1x Full Sized Breadboard
  • 1x Pi Camera
  • 1x RGB LED (Cathode)
  • 1x Distance Sensor
  • 1x 330 Ω Resistor
  • 1x 560 Ω Resistor
  • Blue Wires
  • Black Wires
  • Red Wires

Step 1:

Acquire the parts and attach the T-Cobbler to the Raspberry Pi and breadboard. Next setup up the ground and power wires. From 5.0 v cut and strip enough of the red wire to fit in the hole next to 5.0 v on the T-Cobbler and put into the positive side of the positive and negative spots on the board on one side. Then do what you just did but with a black wire into the GND and that goes into the negative part. After that go to the other side of the breadboard and connect the two positive sides together and the two negative sides together with wire so that the positive is red and negative is black. As shown in this schematic

Step 2:

Take the Distance sensor, RGB LED, and the pi camera and put them into place on the pi and breadboard. Connect the pi camera to the raspberry pi in the indicated position. Then place the RGB LED into the breadboard and make sure that all of the fully leads go into the hole you put it in. Read up on what RGB LED you have and notice which lead is what. Then find a place for the distance sensor on the breadboard where nothing is in the way. Notice which lead goes where as you will need to know for the next step.

Step 3:

Now finish the wiring of the circuit and find the right resistors for the right position. So to represent power I have used red wires, for ground I used black wires, and for the GPIO wires I have used the blue wires. And in this step we will also be putting the resistors in the correct place by the distance sensor. If needed follow the schematic on how to wire this circuit.

Step 4:

Now for this step we will be coding and for this we will be using python 3. what has to happen is that if the distance between u and the distance sensor is more than 100 cm then the camera will take a photo. But just before the photo it will flash red and after the photo it will be a solid blue color.

Python 3 code

import RPi.GPIO as GPIO
from picamera import PiCamera from time import sleep, time from gpiozero import LED,Button

camera = PiCamera()

GPIO.setmode(GPIO.BCM)

GPIO_TRIGGER = 13
GPIO_ECHO = 19 red= LED(16) green=LED(20) blue = LED(21) again = True

GPIO.setwarnings(False)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN)

def RedLight():
red.blink() green.on() blue.on()

def BlueLight(): red.on() green.on() blue.off()

def GreenLight(): red.on() green.off() blue.on()

def distance():
GPIO.output(GPIO_TRIGGER, True)

sleep(0.00001) GPIO.output(GPIO_TRIGGER, False)

StartTime = time() StopTime = time()

while GPIO.input(GPIO_ECHO) == 0: StartTime = time()

while GPIO.input(GPIO_ECHO) == 1: StopTime = time()

TimeElapsed = StopTime - StartTime distance = (TimeElapsed * 34300) / 2

return distance

try: while again: dist = distance() if dist > 100: camera.start_preview() RedLight() RedLight() sleep(5) camera.capture('/home/pi/Desktop/Image.jpg') camera.stop_preview() BlueLight() again = False print ("Measured Distance = %.1f cm" % dist) sleep(1)

# Reset by pressing CTRL + C
except KeyboardInterrupt: print("Measurement stopped by User") GPIO.cleanup()