Posts: 1
Joined: Wed May 30, 2018 4:50 pm

My 1st RPi/Project Help! - help needed with time delay photography

Wed May 30, 2018 5:12 pm

I would like to buy an RPi and program it to take photos with the pi camera after a touch sensor is pressed and the 3 second timer counts down.

Is that possible and if so, I'd like to know what else I need besides: Rpi3, Camera v2, touch display (3.5").
Do I need a stronger power adapter to run the camera and the display?

ANY HELP with items/necessities would be greatly appreciated!

- Mark

Posts: 4175
Joined: Sun Aug 05, 2012 3:12 pm

Re: My 1st RPi/Project Help!

Wed May 30, 2018 5:22 pm

It should work fine with the official pi psu assuming the touch display is powered by the pi.

Which touch sensor, or do you mean the touch display?

Here's an Python example with on screen button and a switch between GPIO pin 26 and gnd

Code: Select all

#!/usr/bin/env python3
import os
import pygame, sys
import time
import shutil
from pygame.locals import *
import subprocess, glob
import RPi.GPIO as GPIO
import signal
from datetime import datetime
sw = 26 # Take photo button - GPIO pin 26

# set screen sizes
width = 480
height = 320

GPIO.setup(sw,GPIO.IN,pull_up_down = GPIO.PUD_UP)


windowSurfaceObj = pygame.display.set_mode((width, height), pygame.NOFRAME, 24)
start = 0

# start subprocess
rpistr = "raspistill -n -o /run/shm/test.jpg -tl 0 -t 0 -w " + str(width) + " -h " + str(height)
p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)

def button (bx1,by1,bx2,by2,height,bColor):
   greyColor = pygame.Color(128,128,128)
   dgryColor = pygame.Color(64,64,64)
   redColor = pygame.Color(255,0,0)
   whiteColor = pygame.Color(255,255,255)
   colors = [greyColor,dgryColor,redColor, whiteColor]
   Color = colors[bColor]

def keys(msg,fsize,fcolor,fx,fy,upd):
   dgryColor = pygame.Color(64,64,64)
   greenColor = pygame.Color(0,255,0)
   colors = [dgryColor,greenColor]
   color = colors[fcolor]
   fontObj = pygame.font.Font('freesansbold.ttf',fsize)
   msgSurfaceObj = fontObj.render(msg, False,color)
   msgRectobj = msgSurfaceObj.get_rect()
   msgRectobj.topleft =(fx,fy)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
   windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
   if upd ==1:

while True:
   while os.path.exists('/run/shm/test.jpg') == False:
   imagefile = ('/run/shm/test.jpg')
   image = pygame.image.load(imagefile)
   catSurfaceObj = image
   button (width - 50,height - 50,50,50,0,0)
   if start == 0:
      keys ("Save",14,0,width - 40,height- 30,1)
      keys ("Save",14,1,width - 40,height- 30,1)

   if GPIO.input(sw)== 0: # save picture if GPIO button pressed
      start = time.time()
      keys ("Save",14,1,width - 40,height- 30,1)
   if start > 0:
      keys (str(3-(int(time.time()-start))),14,1,width - 30,height- 15,1)
      if time.time() - start > 3: # sets 3 second timer
         now =
         fname = ('/home/pi/"{0:%c}".jpg'.format(now)) 
         keys ("Save",14,0,width - 40,height- 30,1)
         start = 0
# read mouse or keyboard

   for event in pygame.event.get():
       if event.type == QUIT:
          os.killpg(, signal.SIGTERM)
       elif event.type == MOUSEBUTTONDOWN or event.type == KEYDOWN:
          kz = 0
          if event.type == KEYDOWN:
             kz = event.key
             if kz == K_ESCAPE:
                os.killpg(, signal.SIGTERM)
          if event.type == MOUSEBUTTONDOWN:
             mousex,mousey = event.pos
             # save picture if on mouse button or touch screen pressed, whilst cursor on screen button
             if mousex > width - 50 and mousey > height - 50:
                start = time.time()
                keys ("Save",14,1,width - 40,height- 30,1)
Last edited by gordon77 on Thu May 31, 2018 9:49 am, edited 1 time in total.

User avatar
Posts: 4175
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: My 1st RPi/Project Help!

Wed May 30, 2018 5:51 pm

Your should have a read and do ... h-picamera

Then at the end have a look at the stop motion project, this shows you how to use a physical push button to take a picture.

(For the push button, if you don't have one, just have two bits of wire and touch them together to be your button.)

This is all easier on a monitor.

If you are using a touch screen, you'll need to follow instructions to wire it up and wire up the touch part.
Then create an on screen button, perhaps look at the developing GuiZero python module to just create an on screen button. Or create a webpage button with a live camera display.

And then develop it further as your imagination an knowledge takes you.

Forum Moderator
Forum Moderator
Posts: 5157
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: My 1st RPi/Project Help! - help needed with time delay photography

Thu May 31, 2018 12:44 pm

Thread title changed to something more meaningful
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):

Return to “General discussion”