Raspberry Pi HQ camera - inconsistant exposure time

Hi all,

I am using a Raspberry Pi HQ camera to take ultra long exposure photographs for a university project.

Despite entering the exposure time for 60 seconds, when I calculate the actual time it has taken for the image to be taken, it returns very inconsistent times (which are accurate as timed by stopwatch).

I have written the following code, if anyone has any advice around how to ensure I can obtain a consistent exposure time, it would be greatly appreciated.

import time
import math
from picamera2 import Picamera2, Preview

picam = Picamera2()
config = picam.create_preview_configuration(controls = {"FrameDurationLimits": (60000000, 120000000), "AnalogueGain": 16, "ExposureTime": 60000000, "AeEnable": False, "AwbEnable": False})

while True:
	a = time.time()
	file_name = "/home/XXXX/Pictures/test_" + str(math.trunc(time.time())) + ".png"
	metadata = picam.capture_file(file_name)
	print(time.time() - a)

1 Like

Hi @David237054 - can you provide any more details about what insonsistent means?
Are we talking differences of greater than a second?
at what point does the timing accuracy break down? 60s? 30s? 10? Knowing this threshold might shed light on a fundamental limitation of the library and help you find where things are coming apart.

It looks like you’ve done everything correctly to me. I even found an old issue (closed) where somebody encounters a similar issue which is resolved by updating FrameDurationLimits.

1 Like

HI @David237054 ,

If you are looking for ‘absolutely precise’ and ‘consistent’ times, you will not get that. You are working within a mutli-user, multi-tasking operating system - the Pi uses a version of Linux/Unix. Accuracy to a second - probably ok, to milliseconds, maybe … , to microsecond, no.

The OS is busy doing other tasks as well as running your camera program, and the inherent time-slicing/time-sharing system processes attempt to allocate ‘time’ evenly/appropriately/fairly … . .
If you run these commands at the command prompt you will see all the other processes that want to share time with your camera program.

ps    # this will probably on show 2-3 processes

ps -ax  # This will show a LOT od processes

ps -ax | wc -l  # this will give a count of the lines returned

On my Pi 4, the last command returns a count of 200 processes, I have geany, thonny and a web browser running, all within the GUI environment ( a time-hungry beast), but if you scroll through the output from the ps -ax command you will see a heap more of other system processes - all also wanting access to the processor.

I hope that you can see that it is not simple to get very precise timings…

You can increase the priority of your program by using the ‘nice’ command as shown when launching your program from the command line ( this ‘removes’ all the Thonny processes from the list of things wanting processor time)

sudo nice -n -19 python3 your_program.py

Launching python as above

mjt@raspberrypi:~ $ sudo nice -n -20  python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.

I have extracted some lines from a slightly extended ps command after launching with the nice command

mjt@raspberrypi:~ $ ps axl | grep python
0  1000  1055   824  20   0 145928 57436 futex_ Sl   ?        946:47 /usr/bin/python3 /usr/bin/thonny
4     0 14656 14416  20   0  13016  4040 -      S+   pts/1      0:00 sudo nice -n -20 python3
4     0 14657 14656   0 -20  15304  7464 -      S<+  pts/1      0:00 python3
0  1000 14660 14571  20   0   7448   488 pipe_r S+   pts/2      0:00 grep --color=auto python
mjt@raspberrypi:~ $

You can see the -20 in the 6th (priority) column in the display – everything else is running at the default priority 0.

See the info from the ‘man nice’ manual page. Note you must sudo to get the higher -negative nice values as they are restricted to the root user.

hope this helps


p.s. you can try this

import os
os.nice(20)  # will 'reduce' the process priority to the lowest level
             #  not what you want ;-(
             # maybe IF the program is launched using sudo python3 ....
             # THEN you might be able to use 

Not tried but might be worthwhile


1 Like