#RaspberryPi Pico: Hertz frequency measure

I’ve only got a cheapo oscilloscope, and it’s often inconvenient to use. A more convenient alternative, if I just want to measure frequencies, is to use an Arduino Nano. I found that it taps out at approx. 70kHz (code here). At frequencies higher than that, it just gives bad readings, as it just can’t process the interrupts fast enough.

I decided to implement an equivalent device using the Pico. The code is straightforward enough, and doesn’t need any real explanation:

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"

using  u64 = uint64_t;
using  u32 = uint32_t;
using  u16 = uint16_t;

u32 counter = 0; // I'm reasonably confident this is atomic

// On GP2 (physical pin 4)
void gpio_callback(uint gpio, uint32_t events) 

bool repeating_timer_callback(struct repeating_timer *t) {
	u32 tmp = counter;
	counter = 0;
	printf("%d\n", tmp);
	return true;

int main() 
	gpio_set_irq_enabled_with_callback(2, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);

	struct repeating_timer timer;
	add_repeating_timer_ms(-1000, repeating_timer_callback, NULL, &timer);


	return 0;

Just hook up the input to GP2, and read the frequency every second. One day I might try to incorporate PWM with it.

I’m interested in how high a frequency i can go. I want to try maxing out an ATTiny85 running at 8MHz, and see if the Pico can keep up. I will report back with my findings.

I’ve been having further thoughts on my little synthesiser that I reported on recently. I’m trying to decide the best way to go. Should I make it just to run on a Pico? Should I run the code using square-waves, or should I use a DAC?

Both approaches are reasonable. People have certainly built synths out of square waves. But I got to thinking. I want to revisit the idea using a Raspberry Pi as a synth. The logic behind this is that the Pi has a beefy processor (compared to MCUs, at least), so I could probably do lots of fancy digital processing.

There’s a problem, though. Ah, always the same problem: the Pi is not a real-time system, What to do? Well, the Pico gave me an idea. The MP4921 DAC has a latch pin to send buffered data to output. I had eskewed it in the past as being too complicated.

Although I had vaguely entertained the idea in the past, but never done anything about it, I have reconsidered my stance. I should be able to get precise timing using the Pico. In fact, I figured that I don’t really need a Pico at all, and can use an ATTiny85 as a latching timer. Running at 8MHz, it should be plenty fast enough. All it is doing is timing the firing of the latch.

The plan is: pick a frequency, say 32kHz, and get the Tiny to toggle a pin low at that rate. I need to keep the pin low for at least 100ns, and then toggle it high again. It would be nice to keep the pin low for around that time.

How close can I get? It’s not an issue I need to sweat too much. The Tiny shouldn’t take that long using a few no-ops. So I’m hoping I will get close. I don’t want the time to be too big, because I want give the Pi as much time as practicable to send it’s data via to the MP4921 via SPI.

The idea is that the Pi has an interrupt which detects, along with the DAC, when the timing goes high (and low in the case of the DAC). It knows it can send a fresh batch of data.

Hmm, I’m thinking that there’s a lot of merit in this approach.

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s