Adjustable clock generator

58 views
Skip to first unread message

Dylan Hall

unread,
Oct 1, 2025, 7:48:27 PM (10 days ago) Oct 1
to retro-comp
I thought the following might be useful for others.

I've been trying to debug a timing issue with my current project and I decided I needed to be able to vary the clock frequency in small increments to try and reproduce the issue.

It turns out the Raspberry Pi Pico PWM widget is pretty good at this.
The only down side is the output is 3.3V so depending on what you feed it into this may be an issue. I put a 74AHCT125 between the Pico and the test circuit and other than the duty cycle being slightly off it worked very well.

To make the process interactive I installed MicroPython on the Pico and connected the USB to a terminal program so I could talk to the Python REPL thing.

The PWM is counting clock cycles from the main system clock, so is very course at higher frequencies (~40Mhz in my case) so you adjust the frequency by changing the system clock instead.

Basic process was:

# setup the environment using GPIO pin 0
import machine
from machine import Pin, PWM

# 1:1 duty cycle, set the system clock to twice the desired frequency (40Mhz)
machine.freq(80000000)
pwm0 = PWM(Pin(0), freq=40000000, duty_u16=32768)
# print the pwm frequency to confirm it's working
pwm0.freq()

# 3:2 duty cycle, set system clock to 5 x desired frequency
machine.freq(200000000)
pwm0 = PWM(Pin(0), freq=40000000, duty_u16=int(65536*3/5))

# 2:1 duty cycle, set the system clock to 3 x desired frequency
machine.freq(120000000)
pwm0 = PWM(Pin(0), freq=40000000, duty_u16=int(65536*1/2))

# 2:3 duty cycle, set system clock to 5 x desired frequency
machine.freq(200000000)
pwm0 = PWM(Pin(0), freq=40000000, duty_u16=int(65536*2/5))

At this point you can adjust the frequency by making changes using machine.freq() in fairly small steps. If you request a frequency the system clock can't achieve it will issue an error. By default the Pico runs at 125Mhz and I think is rated for 200Mhz. In practice it seems happy up to about 250Mhz, but your mileage may vary. 

Dylan

Brian Cockburn

unread,
Oct 2, 2025, 7:46:58 PM (9 days ago) Oct 2
to retro-comp
Surprised you didn't try something like an Si5351module connected via I²C to the Pico (or Arduino).  Search AliExpress (or your favourite online tat-bazaar) for Si5351 module.

Dylan Hall

unread,
Oct 2, 2025, 10:04:36 PM (9 days ago) Oct 2
to Brian Cockburn, retro-comp
I wish I'd known they exist a couple weeks ago!  Thanks

Dylan


--
You received this message because you are subscribed to the Google Groups "retro-comp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to retro-comp+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/retro-comp/e2fffd15-2a9b-4206-9f0b-60ec62367f4en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages