# Raspberry Pi HQ camera - inconsistant exposure time

**URL:** <https://forum.core-electronics.com.au/t/raspberry-pi-hq-camera-inconsistant-exposure-time/17728>\
**Category:** Raspberry Pi\
**Created:** [3 August 2023 23:31 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-hq-camera-inconsistant-exposure-time/17728 "2023-08-03T23:31:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David237054](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@David237054](https://forum.core-electronics.com.au/u/David237054)\
**Post date:** [3 August 2023 23:31 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-hq-camera-inconsistant-exposure-time/17728/1 "2023-08-03T23:31:01Z")

</div>

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.

```auto
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})
picam.configure(config)

picam.start()
while True:
	input("ready")
	a = time.time()
	file_name = "/home/XXXX/Pictures/test_" + str(math.trunc(time.time())) + ".png"
	metadata = picam.capture_file(file_name)
	print(metadata["ExposureTime"])
	print(time.time() - a)
picam.close()

```

---

<div class="post-metadata">

**Author:** ![Trent5487676](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/trent5487676/32/4583_2.png) [@Trent5487676](https://forum.core-electronics.com.au/u/Trent5487676)\
**Post date:** [4 August 2023 03:05 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-hq-camera-inconsistant-exposure-time/17728/2 "2023-08-04T03:05:19Z")

</div>

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](https://github.com/raspberrypi/picamera2/issues/747)) where somebody encounters a similar issue which is resolved by updating `FrameDurationLimits`.

---

<div class="post-metadata">

**Author:** ![Murray125532](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/murray125532/32/6734_2.png) [@Murray125532](https://forum.core-electronics.com.au/u/Murray125532)\
**Post date:** [5 August 2023 23:29 UTC](https://forum.core-electronics.com.au/t/raspberry-pi-hq-camera-inconsistant-exposure-time/17728/3 "2023-08-05T23:29:03Z")

</div>

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.

```auto
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)

```auto
sudo nice -n -19 python3 your_program.py

```

Launching python as above

```auto
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

```auto
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

Murray

p.s. you can try this

```auto
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 
os.nice(-19)

```

Not tried but might be worthwhile

M
