Thursday, December 15, 2022

Fast pin toggling on a Raspberry Pi Pico

Over the past few years I've built a few WSPR transmitters using an Arduino driving an Si5351 clock generator. They work well. I was thinking about the cheapest possible WSPR beacon transmitter and wondered if a Raspberry Pi Pico might be fast enough to toggle a pin at radio frequencies?

Although I've installed and tinkered with the native developer tools, I find the Arduino library convenient and familiar. 

The Pico has an interesting capability in that you can create little I/O state machines that run on the pins to take the bit banging overhead away from the CPUs. I found a nice library called RP2040_PWM by khoih-prog.

A few lines of code showed that it could easily generate RF. I tried on 7MHz. Here it is on 3MHz:

A decent waveform! Unfortunately to generate WSPR you need to be able to generate very small frequency steps and I wasn't able to do this with the library. Despite small changes in the float value of the frequency, the actual frequency would jump in quite large steps. I wrote to the author who explained:

"That sounds interesting project, but your concern, IMHO, is more accurate than what the RP2040 can provide.

The TOP register to control PWM is only 16-bit, giving 64K resolution can't provide the 1.4648 Hz increments in MHz range (nearly 32-bit resolution)"

The more obvious approach was to test if the Arduino library had extended the existing functions to fully use the extra speed of the 2040 chip.

I tried the tone() function but it wouldn't go up as high as 20kHz. (Worked at 10kHz and I didn't try to find the actual limit).

Next was simply toggling a pin in the loop() function.


The maximum is 862kHz. If I put the toggle code in a while(1) dead loop I could squeeze 957kHz out of it.

Micropython

There is a PWM method in machine in Micropython for the Pico so I gave that a try. The code is super simple:


When run, this starts a PWM oscillator on GPIO16 that keeps going. There does seem to be a bit of jitter for some reason visible on the CRO:


The same problem remains, while you can generate frequencies well up into the MHZ, you don't have the fine grain control needed for WSPR generation. Looks like the Si5351 is a good way to go.

2 comments:

Unknown said...

Guess you could key a bit-banged carrier at 137kHz or 472kHz. A novelty but may be worth trying.

BuzzardsBay said...

Pico and rf. I haven’t tried this yet.

I look at the wspr parameters for the tiny fsk shift, and the time to dwell on each of the bits.

I then look at the $4 usd bare Pico price, and I wonder about a 2 knz shift, for multiple seconds, ifneeded to settle to the new tone,all matched by an identical receiver parameter decidersetup.

The $4 calls to me.

Also, wspr and 4 tones, while beautiful is starting to be superseded by 64 tone schemes with lots more forward error correction.

I love the 3.3 v direct dipole attached getting spots from Boston toAustralia's east coast a few spots per month.

Plenty of room on 10m for the next “funny” experiments.

Bradshaw K1te, buzzards bay Massachusetts