Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A FREE frequency generator if you have a Raspberry Pi

12,069 views
Skip to first unread message

Jan Panteltje

unread,
Jul 24, 2013, 10:26:17 AM7/24/13
to
The Raspberry Pi as a frequency generator



I was experimenting with the Pifm code,
and wanted to know what sort of frequencies I could get from the GIPO_4 pin 7.

So I wrote some test code, the program is very simple, and only outputs a square wave.
No extra things needed...
BUT the frequency range is from 130 kHz to 250 MHz.
I have only tested to 250 MHz, I think this is the limit.
Some defines in the code for the PLL frequency are likely wrong.
Missing a factor 2, anyways the calculation of the fractional divider is really close.

A free signal generator from 130 kHz to 250 MHz when you have a Pi is nice thing to have.
http://panteltje.com/pub/freq_pi.c

Compile that code with:
gcc -Wall -O4 -o freq_pi freq_pi.c -std=gnu99 -lm

Install it with:
sudo cp freq_pi /usr/local/sbin/

Connect your frequency counter to GPIO header pin 7.

Run it like this for 130 kHz:
freq_pi -f 130000
error on my frequency counter 3 Hz.

Run it like this for 107.2 MHz:
freq_pi -f 107200000
dunno the error, but it wipes out my FM station at 107.2

Run it like this for 250 MHz:
freq_pi -f 25000000

Error is a few kHz on xpsa spectrum analyzer,
but that could as well be the 30 kHz LO offset from the Elonics 4000 tuner..

Nice to test LC circuits, filters, and the 3.3V amplitude is more than enough for most things,
will even drive 3.3V and 5 V logic.







Jan Panteltje

unread,
Jul 25, 2013, 6:18:15 AM7/25/13
to
The Raspberry Pi as a frequency generator

Version 0.2 is on my site:
http://panteltje.com/pub/freq_pi-0.2.c

130 kHz to 250 MHz output

Functionality is basically the same, but added some info on phase noise.
When using a fractional divider and when the fractional part is not zero,
then the frequency will wobble around the requested one.
freq_pi -v -f 1000001 will show the fractional part,
so you are warned.
For simple testing this is probably not important,
but gets very important if you drive some modulator or something.

Next project :-)

No it is not a V3.
Did the Aliens take my precision screwdriver ???
More in the NEXT issue of ....

!






mscott...@gmail.com

unread,
Sep 9, 2014, 2:11:56 PM9/9/14
to
In freq_pi.c I see struct GPCTL defined in a way that doesn't look right. Why aren't all structure fields on unsigned int instead of char? I guess you know this works. I'll have to experiment some to prove what gcc does on RPi.

Jan Panteltje

unread,
Sep 9, 2014, 2:27:10 PM9/9/14
to
On a sunny day (Tue, 9 Sep 2014 11:11:56 -0700 (PDT)) it happened
mscott...@gmail.com wrote in
<824e78f7-8d99-464b...@googlegroups.com>:
That structure definition is part of Pifm.c,
I did not write it.
But it works,
Look at the Broadcom chip I/O document, can't remember what is is called,
on my system
BCM2835-ARM-Peripherals.pdf
IIRC I checked that and it matches the registers.
If you find something peculiar let me know.

abir.b...@gmail.com

unread,
Jun 12, 2015, 8:28:47 PM6/12/15
to
hello sir .
i need to generate a frequency of 14.318mhz with an RPI , is that possible with your code ?

George Herold

unread,
Jun 12, 2015, 10:12:15 PM6/12/15
to
OMG, I totally want a ~200 MHz sig gen.
(Well really 1/2 of 385 MHz.
If I frequency mod our diode laser there,
the side bands, as imaged in our Fabry-Perot cavity,
will overlap. It would be a nice way to measure the length.)

Am I going to have to learn linux to run the
Raspberry Pi?

George H.



George Herold

unread,
Jun 12, 2015, 10:21:37 PM6/12/15
to
The length of the FP cavity.
The cavity is 20 cm. It's confocal.
a tutorial link...
http://www.thorlabs.us/tutorials.cfm?tabID=e9d2d96f-1efd-4ef0-bb17-fa2ca60f8a8d

Geo

Jan Panteltje

unread,
Jun 13, 2015, 2:09:19 AM6/13/15
to
On a sunny day (Fri, 12 Jun 2015 17:28:40 -0700 (PDT)) it happened
abir.b...@gmail.com wrote in
<8a96c7a0-4588-44b1...@googlegroups.com>:

>hello sir .
>i need to generate a frequency of 14.318mhz with an RPI , is that possible with your code ?

milli Hertz, I think not.

MHz (Mega) yes, but there is a wobble around that frequency caused by the changing
division factor.
So it depends on what you want to do with it.
Also the output is a square wave, so without further filtering you get a lot of harmonics.

If you download the source code this is explained there.
It refers to
BCM2835-ARM-Peripherals.pdf
that you can find with google, and should read.

Jan Panteltje

unread,
Jun 13, 2015, 2:11:00 AM6/13/15
to
On a sunny day (Fri, 12 Jun 2015 19:12:10 -0700 (PDT)) it happened George
Herold <ghe...@teachspin.com> wrote in
<8ec94527-9654-46a6...@googlegroups.com>:
See my reply to other poster about the DDS wobble around the specified frequency.

>Am I going to have to learn linux to run the
>Raspberry Pi?

Absolutely
:-)



>George H.

bca...@protoconnect.com

unread,
Jul 27, 2016, 4:07:17 PM7/27/16
to
Ok, have compiled this, but when I try to run it I get can't open dev/mem

I'm assuming it's a permission thing. but did you have this problem and what did you do to workaround.
Thanks in advance.

Rob

unread,
Jul 27, 2016, 4:09:33 PM7/27/16
to
I don't see the article you are referring to, but the usual solution
to permission problems on the Raspberry Pi is to type "sudo" in front
of the command. You should be familiar with this if you have done some
little admin things on the Pi.

Lasse Langwadt Christensen

unread,
Jul 27, 2016, 5:12:05 PM7/27/16
to
for obvious security reasons only root can open /dev/mem

sudo or being logging an as root should be the answer

-Lasse

raserpps...@gmail.com

unread,
Oct 10, 2016, 7:56:29 PM10/10/16
to
They have Windows 10 on the Raspberry Pi, but if you are going to do this sort of stuff I would DEFINATELY learn linux...you can make a USB with a "live linux" that you can run from your normal computer with windows or whatever you use. And run it off the USB to test it - it's called Live Linux

The RPi uses Raspbian...a variant of Linux Debian

If you want to get Linux Debian onto a USB to try out for free on any computer let me know I will instruct you fully

raserpps...@gmail.com

unread,
Oct 10, 2016, 8:01:42 PM10/10/16
to
Please help me get this running

I compiled it as instructed and it brought up a few errors - little things in the source code.

I'm not efficient in the art of C and have tried to sort the little things - I am thinking it is dependancies on my Pi - maybe Im using a different something or other

DemonicTubes

unread,
Oct 11, 2016, 5:39:58 PM10/11/16
to
You will need to provide more information. What errors are you getting?

Cursitor Doom

unread,
Oct 11, 2016, 7:47:54 PM10/11/16
to

Try comp.lang.c

overlor...@gmail.com

unread,
Oct 17, 2016, 11:50:29 AM10/17/16
to
What does the flowchart for this code look like?

soh...@gmail.com

unread,
May 2, 2017, 8:13:14 AM5/2/17
to
pin 7 doesn't give any output at all

compile gives the following error

pi@raspberrypi:~/freq_pi $ gcc -Wall -O4 -o freq_pi freq_pi-0.2.c -std=gnu99 -lm
freq_pi-0.2.c: In function ‘start_rf_output’:
freq_pi-0.2.c:183:1: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
ACCESS(CM_GP0CTL) = *((int*)&setupword);
^

anyone an ideas?

thanks

Cursitor Doom

unread,
May 2, 2017, 1:49:41 PM5/2/17
to
The obvious thing seems to be to just use a different output pin and edit
the source code accordingly.

Owen Cook

unread,
May 2, 2017, 7:59:19 PM5/2/17
to
Its a warning? Otherwise it compiles and works?

What happens if you remove the -Wall?


Owen

Jan Panteltje

unread,
May 3, 2017, 3:43:42 AM5/3/17
to
On a sunny day (Tue, 2 May 2017 05:13:03 -0700 (PDT)) it happened
soh...@gmail.com wrote in
<9e87aacf-af0d-4d17...@googlegroups.com>:

>pin 7 doesn't give any output at all
>
>compile gives the following error
>
>pi@raspberrypi:~/freq_pi $ gcc -Wall -O4 -o freq_pi freq_pi-0.2.c -std=gn=
>u99 -lm
>freq_pi-0.2.c: In function â start_rf_outputâ :
>freq_pi-0.2.c:183:1: warning: dereferencing type-punned pointer will break =
>strict-aliasing rules [-Wstrict-aliasing]
> ACCESS(CM_GP0CTL) = *((int*)&setupword);
> ^
>
>anyone an ideas?
>
>thanks

Well, since I wrote that code, or at leat most if it, long ago, it was for the older Pi 2?
New ones need code change.
The compile warning is irrelevant.
I do not have the latest Pi so no idea.
What model do you use?

Jasen Betts

unread,
May 3, 2017, 6:31:00 AM5/3/17
to
not an error, a warning.

--
This email has not been checked by half-arsed antivirus software

Jan Panteltje

unread,
May 4, 2017, 5:40:46 AM5/4/17
to
On a sunny day (Tue, 2 May 2017 05:13:03 -0700 (PDT)) it happened
soh...@gmail.com wrote in
<9e87aacf-af0d-4d17...@googlegroups.com>:

>pin 7 doesn't give any output at all
>
>compile gives the following error
>
>pi@raspberrypi:~/freq_pi $ gcc -Wall -O4 -o freq_pi freq_pi-0.2.c -std=gn=
>u99 -lm
>freq_pi-0.2.c: In function â start_rf_outputâ :
>freq_pi-0.2.c:183:1: warning: dereferencing type-punned pointer will break =
>strict-aliasing rules [-Wstrict-aliasing]
> ACCESS(CM_GP0CTL) = *((int*)&setupword);
> ^
>
>anyone an ideas?
>
>thanks

PS,
you do not come back it seems,
but anyways somebody ported that code to a later raspi model
(
I lost count of all those models:
https://en.wikipedia.org/wiki/Raspberry_Pi
}
Latest:
http://panteltje.com/panteltje/newsflex/download.html#freq_pi

It is fun to write code for something new,
it gets boring after having to re-write it again and again each time
a new sort of berry drops of the tree.
I managed to get some older models...

Raspi foundation works against themselves that way.
But you can't stop 'progress'.
Although without ethernet? Not my idea of progress..
And now all its clones...


soh...@gmail.com

unread,
May 8, 2017, 5:58:45 AM5/8/17
to
thanks guys, it is now working after reinstall the OS. thanks ps its a rsp2 2 years old

bill.ru...@gmail.com

unread,
Apr 24, 2019, 9:39:37 PM4/24/19
to
On Wednesday, July 24, 2013 at 10:26:17 AM UTC-4, Jan Panteltje wrote:
> The Raspberry Pi as a frequency generator
>
Jan, does v0.7 work on an RPi 3B+?
Per your instructions I have compiled it getting the warning and installed it and it is running, but there is no activity on GPIO4 (pin 7).
sudo freq -f 200000 ==> I changed the name to freq

I am running Raspbian over Noobs whatever the latest builds are - I just loaded them.
I am also a noobs so take that into consideration.

I originally loaded 0.2 but I don't know how to remove it (diff filename than 0.7). apt-get remove can't find the package.
thanks

Cursitor Doom

unread,
Apr 25, 2019, 12:55:53 PM4/25/19
to
On Wed, 24 Apr 2019 18:39:32 -0700, bill.ruggirello wrote:

> I am running Raspbian over Noobs whatever the latest builds are - I just
> loaded them.
> I am also a noobs so take that into consideration.
>
> I originally loaded 0.2 but I don't know how to remove it (diff filename
> than 0.7). apt-get remove can't find the package.
> thanks

That message you're replying to is 6 years old! :-D



--
This message may be freely reproduced without limit or charge only via
the Usenet protocol. Reproduction in whole or part through other
protocols, whether for profit or not, is conditional upon a charge of
GBP10.00 per reproduction. Publication in this manner via non-Usenet
protocols constitutes acceptance of this condition.

Winfield Hill

unread,
Apr 25, 2019, 1:49:34 PM4/25/19
to
Cursitor Doom wrote...
>
> On Wed, 24 Apr 2019 18:39:32 -0700, bill.ruggirello wrote:
>
>> I am running Raspbian over Noobs whatever the latest builds
>> are - I just loaded them.
>> I am also a noobs so take that into consideration.
>>
>> I originally loaded 0.2 but I don't know how to remove it
>> (diff filename than 0.7). apt-get remove can't find the package.
>> thanks
>
> That message you're replying to is 6 years old! :-D

Y'all should go out and purchase a cheap Chinese dual-channel
Function Generator, like an MHS-5225A, or a FeelTech FY2224,
or Adoner FY6600. Or better yet, FeelTech FY6600, it goes
to 60MHz, Arbitrary Functions to 10MHz, to 20Vp-p, and has a
10-line color programming display, plus USB link. $99 and up.


--
Thanks,
- Win

Johnny B Good

unread,
May 5, 2019, 4:43:19 PM5/5/19
to
Good advice! Even if it happens to be six years too late. :-)

BTW, I think you probably meant to write "FY6800" for that last
recommendation (although it's essentially just a slightly revised FY6600
with improved "haptics" and an "earth loop hazard" thrown in for free -
this free gift can be refused by connecting a 10 or even a 1 kR resistor
in series with the stolen from the PSU to main board ribbon cable ground
return wire and the PE tag on the IEC C13/14 mains socket).

There's a whole load of useful info on the FY6600 (and the FY6800) here:

<https://www.eevblog.com/forum/testgear/feeltech-fy6600-60mhz-2-ch-vco-
function-arbitrary-waveform-signal-generator/1825/>

--
Johnny B Good

murali.ace...@gmail.com

unread,
Jun 6, 2019, 6:36:07 AM6/6/19
to
On Thursday, 25 July 2013 13:18:15 UTC+3, Jan Panteltje wrote:
> The Raspberry Pi as a frequency generator
>
> Version 0.2 is on my site:
> http://panteltje.com/pub/freq_pi-0.2.c
>
> 130 kHz to 250 MHz output
>
> Functionality is basically the same, but added some info on phase noise.
> When using a fractional divider and when the fractional part is not zero,
> then the frequency will wobble around the requested one.
> freq_pi -v -f 1000001 will show the fractional part,
> so you are warned.
> For simple testing this is probably not important,
> but gets very important if you drive some modulator or something.
>
> Next project :-)
>
> No it is not a V3.
> Did the Aliens take my precision screwdriver ???
> More in the NEXT issue of ....
>
> !

sir i have really impressed with the code..can we change the minimum frequency to 12khz to maximum frequency to 2mhz..let me know if it is possible...

TTman

unread,
Jun 6, 2019, 9:22:31 AM6/6/19
to

>> !
>
> sir i have really impressed with the code..can we change the minimum frequency to 12khz to maximum frequency to 2mhz..let me know if it is possible...
>

From the software... So in short, No unless you change the clock source
in the Pye...

clock sources are 650 MHz, 400 MHz, and 200 MHz

So the lowest frequency we can make is 200,000,000 / 4095.4095 =
48,835.165323516 Hz

But I get 61,043 Hz

4095.4095 * 61043 = 249,996,082.108499999 Hz....

But for MASH 1,
MASH min DIVI min output freq average output freq
max output freq
0 (int divide) 1 source / ( DIVI ) source / ( DIVI )
source / ( DIVI )
* 1 2 source / ( DIVI ) source / ( DIVI + DIVF /
1024 ) source / ( DIVI + 1 )
2 3 source / ( DIVI - 1 ) source / ( DIVI + DIVF /
1024 ) source / ( DIVI + 2 )
3 5 source / ( DIVI - 3 ) source / ( DIVI + DIVF /
1024 ) source / ( DIVI + 4 )

200,000,000 / (4095 = 48840.048840048
200,000,000 / (4095 + (4095/1024) ) = 48792.400011912

So
61043 * (4095 + .3999023437) = 249995496.238766479 Hz, looks liike we
have a 250 MHz clock.

Lowest frequency then is 61,043 Hz,
highest frequency then is 250,000,000 / 1 = 250,000,000 Hz

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Jan Panteltje

unread,
Jun 6, 2019, 10:14:58 AM6/6/19
to
On a sunny day (Thu, 6 Jun 2019 03:36:02 -0700 (PDT)) it happened
murali.ace...@gmail.com wrote in
<79c40d2b-468e-45ca...@googlegroups.com>:
Simpe software loop, not hardware

set pin high
usleep X or nops for pulse width
set pin low
usleep Y or nops for delay between pulses.

Command line option for frequency and pulse width

delay Y is 1/f seconds - pulse-width X
or something along those lines.

You'd have to make sure the task switch never happens..

Simpler to use a PIC for those low frequencies.
Program it in asm. serial interface or up-down buttons whatever.

Even simpler 555 timer?

Even more simple UJT transistor? (not sure if those go op to 2 MHz,
2 mHz ? sure with a big enough capacitor.

Or use the 74HC4046, it easily does 1 Hz to 2 MHz and more.







John Larkin

unread,
Jun 7, 2019, 10:58:22 AM6/7/19
to
We've made nice sine waves, using software DDS and the DAC on a small
ARM chip. Frequency range is limited to basically audio, but we can
control amplitude and frequency nicely.

We just ran some code in a periodic interrupt, but some DMA thing
could be done too, I guess.

My benchtop B+K analog function generator is handy. It has knobs.


--

John Larkin Highland Technology, Inc

lunatic fringe electronics

0 new messages