Wednesday, June 19, 2024

Frequency Counter using an RP2040

Readers: If you want to build the project featured in today's post, please go to PCBWAY's Community pages--a gerber file ready to download and/or fabricate as well as Kicad files and a BOM are here.  Firmware for the reader (work very much in progress but frequency reading the OLED display function) is here.

Also please visit PCBWAY's site--using the link here--it will help this blog immensely. Thanks.

====

I want to build a clock multiplier for my bench, but first I needed a way to accurately read frequencies of incoming signals. Between 10hz and 100K?  Sure. Good start.  

I got that working this morning:


For prototype hardware I used an already fabricated PCB I designed: post here.

This time, as with many times before: my patient and tolerant sponsor, PCBWAY, helped ameliorate my frustration with breadboarding....for $5 and shipping, I threw most all my error prone dupont jump cables into the dumpster.  

Then I coded  in C using more reliable hardware--a PCB:

When I breadboard, and it doesn't work, why not?  Is it a broken dupont cable? A wire put into the wrong .1mm hole? A dumb design mistake? All of the above? The PCB I used this time is an encoder + GPIO  development board centered around a PICO dev board which in turn is centered on the RPI RP2040 MCU.  A mouthful but it saved time and anguish. Get this PCB here.


Unfortunately for this bench configuration not all the breadboards were gone! 

To buffer the incoming signal I tried out two different designs, "anything to clock", see posts here and here--each A2C version was on its own PCB which in turn was mounted on a breadboard. 

These small PCB's rectified incoming signals to turn them into rising and falling edges and clamped the output to 0-3.3V so as to not blow up the RP2040's GPIO inputs.  


The firmware for the reader is based on a cool RP2040 counting algo by Jeremy P. Bentham--see his blog post here and Github page here

I modified Mr. Bentham's code a little, but not much, and made it display observed frequencies on a small OLED display using David Schram's open source C library for SSD1306'shere

A few minor code changes were needed, for instance, the frequency reading algorithm put the frequency value into an integer in memory, but for the  Schram1306 library I needed a string.

Easy--there is a function in C to change ints to strings that I didn't know about--snprintf:

#include <stdio.h>

#include <stdlib.h>

char buffer[10];

int value = 234452;

snprintf(buffer, 10, "%d", value);

//buffer -- input string

//10  -- base for conversion

//%d   decimel output

//value   input number to convert to a string.

Another cool thing--the Bentham code traps the frequency and sends it out the RP2040's UART pins if you configure CMAKE to do so. Not sure what I'd use that serial stream for, but hey, it worked, and for the final build I will probably bring this data to the front panel on a TRS jack.

The ink on the firmware from the reader is still wet--there might be bugs? This is version  "less than point one", folks. 

But for me, JOY! it worked.

Get the code here.

WIRING

Adding a wiring diagram below because in 3 days I will forget how I wired all the dev board crap on my bench and be extremely unhappy to have to figure it out again. Live and learn?  Usually nope, this time--maybe.



The breadboard equivalent of the dev board. LED's and GPIOs currently not implemented in code are not shown.




Pico Pinout.....


Still to Do:

--I need another algorithm to analyze incoming low frequencies--say .5hz or .1hz to maybe 10hz. Not useful for audio, but in music synthesis, recording studios, bench automation and other environments I see these low frequency rising and falling edges--frequently

--Utilizes the second core of the RP2040--looks pretty easy, see the video here. I've been putting experimenting with this important feature; maybe next time?

--Auto-ranging--the counting algorithms should change based on the rough frequency seen at input. So far I can think of 3 different settings: .01hz to 10hz, about 10hz to 2K, and 2K and above. So some sort of ranging is needed but I will probably get lazy and put this on a switch which might be good enough.

And of course, once I have everything jumpered together on the bench I need to turn it into a working prototype. More to come, but this project is starting to feel like a nuisance to me. 

The gerber to PCB part is super easy, thanks to my sponsors.  My own dedication to finishing this one--not as much.

More coming up. Until then, enjoy the beginning of summer.



No comments:

Post a Comment

JTAG to SWD Converter

Readers: If you'd like to build the project featured in today's post, please go to PCBWAY's Community pages--gerber file, KiCAD ...