Control PanTilt from script run in crontab

Hi all,

(In messing around with the code I appear to have solved my question ‘rubber ducking’ style. Nonetheless, I’ll post it here in case anyone else should run into this.)

I have a panTilt hat that will be used in a time lapse. At the moment just as a swivel head to position the camera at the proper angle.

The actual shots I intended to be taken from a shell script, but AFAIK that cannot control the panTilt hat. So, I wrote a python script that positions the panTilt in the proper angle regularly in case something misaligned it.

Thing was, that setting the angles in a script ran from a crontab did not work while running the same script from Thonny worked just fine. The ‘crontabbed’ script wrote all the files I told it to but did no panning or tilting.

Solution was to give the script some time after the pan-tilt lines. Thonny apparently does that on its own but the crontab appears to sever all connections to the still running servos once the script reaches its last line thus stopping the servos. The last sleep(2) from the script below solved it.

Before I had that in there, I occasionally notices a short buzz from the servos, but never long enough to actually position them properly.

OK, so no question. Am curious though as to what the difference between Thonny and the crontab is. I suppose they both just run the script more or less the same way?

Kind regards,

The crontab:
* * * * * /usr/bin/python3 /home/pi/Documents/pantilt/

The python script including generating file based output for testing purposes

import datetime
import pantilthat
import random
from time import sleep
from picamera import PiCamera

camera = PiCamera()

dt_date =
fname = dt_date.strftime('%d-%m-%y %I:%M %S %p') + ".txt"
path = "/home/pi/Documents/"
f = open(path + fname, "w")
f.write("Now the file has more content!")






With no “while true” section, your script will run once and stop. That’s my understanding anyway. I have two Pan Tilt setups but mine are running in Motion Eye OS. Any movement is done manually via action buttons from its web gui.
I have a file I run on boot to set a default position, its not done from Crontab though. I tried to do it that way and it wouldn’t work. It’s a Motion Eye issue.
I run scripts via Crontab in Raspbain all the time with no issues.

I do the following in crontab to run a python file on bootup
@reboot python3 /home/pi/ &

The lack of a loop is bc I just wanted to set the position once (or at times using crontab). I’ll try with the ‘&’ at the end, but my understanding was that that made the script run ‘as a process’(?). But who knows (not a linux, nor a python expert)

Ok, I wasn’t 100% sure what you were wanting to accomplish.
I have one Pan tilt that moves all on its own when the Pi is powered up. On that one I run my position script once on boot to set it back to where I want it pointing. It’s just a python file with pan tilt settings with no while true lop. The second one I bought latter on doesn’t do that, it just stays where it was last pointed? I didn’t bother with running my script on boot with that one. Both are hooked up the same way and have the same version of Motion eye installed?

My positioning file is as follows. I don’t know why, but if I don’t put the 2 second delay in at the end the pan tilt doesn’t go all the way to where I want it to. It just goes part way and quits moving?

import time
import pantilthat