Automation HAT timing


I am trying to use the Automation HAT as a pulse generator and the timing seems to be quite erratic when I attach a scope. The code below should generate pulses every 1s and displaying the time in the debug window seems to confirm the timing but when I measure the pulses with a storage scope the timing is up to half a second out.
Sorry about the indentation. It gets stripped out when I hit post.

My code is
#!/usr/bin/env python

import time
import math
import automationhat

if automationhat.is_automation_hat():

lTriggers = 0
starttime = time.clock()
trigger_on = 0

while True:
time_ms = 1000.0 * (time.clock() - math.floor(time.clock()))
if trigger_on == 0:
if time_ms > 900:
trigger_on = 1
if trigger_on == 1:
if time_ms < 50:
trigger_on = 0
lTriggers = lTriggers + 1
if lTriggers >= 100:


You can fix indentation by embedding your code in three backticks:

Like this!

Try something like this, and see if it gives you more precise timings:

while True:
    state = int(time.time()) % 2