A project to use the picamera python library to generate a timelapse sequence with the Raspberry Pi camera module and control a fan connected to a PiFace Digital to keep a window demisted. 

I like to use my Raspberry Pi camera module to take timelapse photos during sunrise and sunset and of the clouds as they drift by. Often the results are runied by the windows misting up with condensation either overnight or at times during the day. I recently got a PiFace Digital add on board that I thought would be useful to connect a fan and keep the window clear of condensation around where the camera was set-up. This allowed me to write a short python program to set the fan off to coinside with a time-lapse sequence being taken with Raspistill camera  program.

Then I came across the PiCamera python library that gives you full control of the Raspberry Pi Camera from within a python script. So now I am able to control the camera and the fan attached to the PiFace Digital in the same Python script which makes the whole process a lot easier to manage.

This is a simple project that solves the window misting problem.

I have used a computer cpu fan as it can run relativly quietly and is small but effective enough to keep the area of the window the camera is using clear. The PiFace Digital outputs can be used with 5 Volts as standard but the fan uses 12 Volts at full speed so I have connected it to one of the Relays on the PiFace Digital and used a varible mains adapter as it's power supply. I can then vary the speed based on the amount of condensation I expect to be around while the time-lapse is being taken by adjusting the voltage.

Using the PiCamera Python library and the pifacedigitialio Python library I have written a program that sets the fan off a specified amount of minutes before the time-lapse starts to clear any condensation, then at a specified time starts the time-lapse capture to the memory stick. Once the time-lapse capture is complete the fan is stopped.

I have only been using Python since I got a Raspberry Pi in 2012 for a few projects, so i'm not that good at it.  I was able to acheive the functions I initially needed which was to activate the fan at a specified time and set off a timelapse capture at HD resolutions for a specified amount of time and length all controlled by user settings in the python script.

The plan is to make the code more efficient and except arguments to overide the defaults settings as well as make the other features of the Raspberry Pi camera and the PiCamera library available, but for now it is doing what I wanted it to do.

There is little error checking so currently is at alpha status. You are welcome to use the code if it is of any use to you. 

My Raspberry Pi is WiFi enabled and has a 8gb usb memory stick to save the time-lapse images to, allowing it to be postioned on any window around the house. I then access it via SSH using my phone or a Linux PC to change any settings and activate the program. I also use SSH and VNC remote desktop using a Linux desktop PC to check the camera is positioned correctly by taking test images, this could equally be done on a Windows machine.

Setting up a python webserver to show the preview image in a web browser would be a better option for positioning the camera which would be easier. I plan to add this feature to the script.

 

The Circuit for the Fan to the PIFace Digital

 piface digital relay connectors

A simplified image of the circuit between the PiFace Digitals Relay and the CPU Fan.

  1. The fans red positive + is connected to the + on the power supply.
  2. The fans negative - black wire is connected to the NO (Normal Open) connector on the relay.
  3. The power supplies negative - is connected to C (Common) on the relay.
  4. As you can see from the connections inside the relay C & NO are not connected so no circuit is made. This is when output port is set to OFF or 0
  5. When output port 0 is ON or 1 on the PiFace Digital the connection is made between C and NO of the relay completing the fans circuit.

The fan also has a yellow wire that is used in a computer to monitor the speed of the fan which is not used in the project.

 

PiFanCam code

I will post updated source code and features as they are added. This code is to be run on a Raspberry Pi that has a Camera Module and a PiFace Digital attached. Also the PiFace Digital python3 library pifacedigitalio and the picamera python library needs to be installed. 

This is work in progress so use at your own risk, but works on my set-up.

 

The links at the top of this page relate to articles that contain more information about how to get these libraries.

 

 

#!/usr/bin python3
#Version 0.2 Alpha
#www.RaspberryConnect.com
#Program using a Raspberry Pi Camera and a PiFaceDigital board that has a Fan connected to an output port. #This program is for taking a timelapse sequence of Jpg images. #The Fan should be activated enough minutes before the Time Lapse starts to clear Condensation from the window. #If fan time is within the "fan_start" minutes of the start then the fan will activate straight away. # Once the time Lapse is complete the Fan should switch off
#Current limitations: # No error checking # Have to Kill the process to stop it during waiting and timelapse phase.
#Future - Option to press button on PiFace Digital to switch fan on and off manually if required without stopping time lapse. #Option to use button on PiFace Digital to stop Time Lapse, Start Fan and Timelapse Now, and button to shutdown PI #Receive arguments at runtime to override defaults #Extend Camera Options that can be configured. #Error Checking #Better program Flow #View preview image in a web browser for postioning the camera.
#*********User Config****** #Base Filename - 4 digit sequence of numbers will be added to the file # a filename of "capture" will be saved at capture0001.jpg, capture0002.jpg etc fname = "capture"
# Path to Image folder. Update this to the path of a memorystick. Default is current folder. imgpath = "./"
# Picture Resolution in Pixels height = 1080 width = 1920
#Capture Start Date, no leading zeros startday = 27 startmonth = 2 startyear = 2014
#Capture start Time, 24 hour clock, no leading zeros i.e. enter 1 not 01 for 1am starthour = 10 startmin = 0 startsec = 0
#Timelapse Length - how long in time you want the timelapse to last for. capturedays = 0 capturehours = 7 capturemins = 0 capturesecs = 0 image_delay = 5 #in seconds, how long between each images for timelapse
#Fan fanport = 0 #can be 0-6. PiFace Digital output port to be activated. 7 used to to show when image being captured via LED flash. fan_start = 45 # Time in minutes fan starts before timelapse starts. if within limit it will start straight away fanuse ="yes" # Options: yes no, Activate fan or device on PiFace output port
#*********User Config End********
#***** Main Program ***** import datetime, os, picamera from sys import exit import pifacedigitalio from time import sleep pfd = pifacedigitalio.PiFaceDigital() dt = datetime pfname = imgpath+fname+"{counter:04d}.jpg" tl=dt.datetime(startyear,startmonth,startday,starthour,startmin,startsec) #Capture Start time to varible tl_length=dt.timedelta(hours=capturehours,minutes=capturemins,seconds=capturesecs) #length of capture in seconds to varible i =0 print ("Timelapse set to start at:", tl)
def tl_countdown(): """ Calculate time to Capture start Time""" tl_start =(tl-dt.datetime.now()).total_seconds() return tl_start
def fan_countdown(): """Calculate time to fan start time""" fan_start2 = (tl-dt.datetime.now()).total_seconds()-fan_start*60 return fan_start2
#check if start is in the future if tl_countdown() = 0: print("Start time is in the past. Capture wn't be started") exit()
#check how long to fan start time if fanuse.lower() =="yes": if fan_countdown() = fan_start*60 : if fanuse.lower()=="yes": print("Fan will start in ", round(((tl-dt.datetime.now()).total_seconds()- fan_start*60)/60,2), "minutes") print("Capture will start in ", round(((tl-dt.datetime.now()).total_seconds())/60,2), "minutes") #pause until fan start time if not already active if pfd.output_pins[fanport].value == 0 and fanuse.lower() == "yes": sleep(fan_countdown()) pfd.output_pins[fanport].value =1 print("Fan Activated at ", dt.datetime.now())
#pause until capture start time sleep(tl_countdown()) print ("Camera activated at ", dt.datetime.now()) tl_end = tl + tl_length
#Time Lapse capture with picamera.PiCamera() as cam: cam.resolution = (width, height) cam.start_preview() sleep(1) for filename in cam.capture_continuous(pfname): pfd.output_pins[7].value = 1 # Flash Piface Digital LED 7 when photo taken i = i+1 if dt.datetime.now() >= tl_end: cam.stop_preview() if pfd.output_pins[fanport].value == 1: pfd.output_pins[fanport].value = 0 pfd.output_pins[7].value = 0 print ("Timelapse ended at ", dt.datetime.now()) print ("\n",i, " Photos where taken") break pfd.output_pins[7].value = 0 sleep(image_delay) if pfd.output_pins[fanport].value == 1: pfd.output_pins[fanport].value = 0 if pfd.output_pins[7].value == 1: pfd.output_pins[7].value = 0 print("Power Down Fan")

Though this project is designed to activate a fan it will activate anything connected to a PiFace Digital output port during a time lapse sequence such as a light or motor.

 

 

 

 


Add comment