frequency counter

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
katesfb
Posts: 22
Joined: Sun Dec 18, 2016 8:09 pm

frequency counter

Post by katesfb » Tue May 16, 2017 3:48 am

Hi,
I have an arduino program that counts the frequency of a light to frequency converter and i wish to do this project with the pyboard - does a library exist to do this. if not what is the best aproach.

Any help is much apreciated.

Cheers.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: frequency counter

Post by pythoncoder » Tue May 16, 2017 5:00 am

It would help if you could give the frequency range involved and an idea of the required accuracy.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: frequency counter

Post by dhylands » Tue May 16, 2017 6:50 am

You can configure a timer to use an external pin as a clock source. MicroPython's timer code doesn't directly support this, but you can tweak the registers directly to get the desired effect.

I put together a sample here:
https://github.com/dhylands/upy-example ... counter.py

Running on my pyboard with a jumper wire between X1 and X4, I get this output:

Code: Select all

MicroPython v1.8.7-374-g52b6764 on 2017-03-08; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> 
>>> import freq_counter
Freq = 10003
Freq = 10000
Freq = 10000
Freq = 10000

katesfb
Posts: 22
Joined: Sun Dec 18, 2016 8:09 pm

Re: frequency counter

Post by katesfb » Tue May 16, 2017 8:49 pm

Hi,
And thanks for the responses.

The frequency range of the device (Taos TS238 Light-Frequency converter) is ~6hz (dark) to ~500khz. The application is for a cheap photometer that i found here (https://hackaday.com/2015/08/11/simple- ... en-source/). The original design makes use of an atmega chip (328/168) and also uses a bluetooth chip to communicate with a cell phone however i dont require the cell phone side (seems like over complicaton to me) but do require an SD card for logging so thought that the pyboard may be ideal for this. The plan is to add a serial LCD display at some point. But in the short term just need to test the basics of reading frequency.

Thanks for the frequency counter example. Thats exactly what i was looking for, i doubt that i would have been able to sort that myself - only a novice python programmer really. I will let you know how it goes.

Thanks for your help.

Cheers.

User avatar
RWLTOK
Posts: 53
Joined: Thu Dec 14, 2017 7:24 pm

Re: frequency counter

Post by RWLTOK » Sun May 12, 2019 9:25 pm

Dave,

I am doing something similar. My application is crystal calibration of a BLE radio. I can set up a 6MHz output on one of the radio IO pins to input to a pyboard as a clock input, hence, using the PYBOARD as a frequency counter gated by a one pulse per second (1pps) output from a GPS receiver. The goal is to configure the PYBOARD to trigger a capture of the counter driven by the 6MHz clock at every 1pps of the GPS clock. I really want to do this in uPython. As a proof of concept, timer outputs can be used to feed back into the PYBOARD standing in for the 6MHz and 1pps.

Any thoughts on how to set this up?

Regards,

Rich

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: frequency counter

Post by dhylands » Mon May 13, 2019 4:33 am

You can manipulate the timer registers directly from upy. Use the stm module. You can see an example here:
https://github.com/dhylands/upy-example ... r/gated.py

For your use case, you want to have an external clock source (described in the STM32F4xx Reference Manual in section 18.3.3 - Clock selection).

I think that you should also be able to use input capture mode to capture the value of the counter each second (using the PPS signal to trigger the input capture).

I haven't actually tried this, but that's the gist of what you want to try.

I'd recommend using a 32-bit timer (TIM2 or TIM5) rather than a 16-bit timer.

User avatar
RWLTOK
Posts: 53
Joined: Thu Dec 14, 2017 7:24 pm

Re: frequency counter

Post by RWLTOK » Tue May 21, 2019 2:08 am

Dave,

Thanks for the suggestions. I will report back on my progress. Copy the 32-bit timer. Since I am measuring a known frequency and just tweaking to get better that 5ppm, a 16-bit would work, but 32-bit would be more robust and allow for testing with unknown frequencies.

Take care,

Rich

Post Reply