Wednesday, 29 October 2014

Generating Radio Frequencies using the "Clock" pin GPIO 4

After many months of being busy with work, and more recently demonstrating the Raspberry Pi Wobbulator at radio rallies in Ireland and England, I have finally had the chance to return to doing what I enjoy most - experimenting with the Raspberry Pi.

It has been known for some time that (with some clever coding) the Raspberry Pi can generate an FM signal in the VHF broadcast band and transmit music (from a .wav file) or the signal from a microphone over considerable distances (10's or metres) using no more than a short length of wire connected to the "clock" pin (GPIO 4). Click here for full details.

More recently, I came across a modified version of the RPi.GPIO library which makes it very easy to generate radio frequencies using the Raspberry Pi's clock pin. Click here for details, where you will also find many other useful tutorials.

First of all, make sure your distro is up to date. For instructions on how to do this please click here.

To use the modified RPi.GPIO library you must first install the "python-dev" package, or the "python3-dev" package if (like me) you want to use Python 3. You can do this by using the "apt-get" command as follows:

pi@raspberrypi ~ $ sudo apt-get install python3-dev_

Once this package has installed, download the modified RPi.GPIO library using the "wget" command:

pi@raspberrypi ~ $ wget https://www.cl.cam.ac.uk/projects/
raspberrypi/tutorials/robot/resources/RPi.GPIO-0.3.1a.zip_

Extract the ".zip" file:

pi@raspberrypi ~ $ unzip RPi.GPIO-0.3.1a.zip_

Go to the "RPi.GPIO-0.3.1a" folder:

pi@raspberrypi ~ $ cd RPi.GPIO-0.3.1a_

And finally install the library using the following command:

pi@raspberrypi ~ $ sudo python3 setup.py install_

This will take a while, but after it has finished, launch IDLE3 and enter the following few lines of Python code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.ALT0)
GPIO.setclock(4,9500000)
GPIO.output(4,1)
time.sleep(10)
GPIO.output(4,0)

These few lines of code will cause the Raspberry Pi to generate a frequency of 9.5 MHz on pin GPIO 4 for ten seconds and then turn off. In order to run the code, you will need to save the file first - I called mine "RPi_RF_Gen.py".

I connected pin GPIO 4  to the input to an Oscilloscope so that I could have a look at the generated waveform, however I found that there was a DC component in the signal, so I connected pin GPIO 4 to the Oscilloscope via a small 10 pF capacitor. The waveform is shown below:


 "Clock" signals are square waves, but the observed waveform is slightly distorted, although some of this distortion may be down to the oscilloscope's sample rate. The Python code was run again, but this time the output from pin GPIO 4 was connected to a Spectrum Analyzer, producing the following results:


The primary signal is at 9.5 MHz, but there are numerous harmonics right across the RF spectrum (which is typical of a square waveform). For some reason, the highest RF frequency I could generate with the Raspberry Pi was 9.5 MHz, even though the documentation relating to the modified RPi.GPIO library I installed stated that it was possible to generate frequencies up to 19 MHz.

Therefore, using the modified RPi.GPIO library, it is possible to very easily generate RF frequencies using the Raspberry Pi clock pin GPIO 4, however the wave forms generated are square waves and as such contain multiple strong harmonics, and there appears to be an upper limit  in frequency of 9.5 MHz when using this method.

7 comments:

  1. Sorry men please help me, I can't start gpio 4, I write the lines but the python showed "the channel is not set up a general purpose clock 0"

    Thanks

    ReplyDelete
  2. Hi,

    I'm using RPi 3 and I can't get GPIO 4 to output the clock frequency

    TNX in advance

    Shai

    ReplyDelete
  3. how many pins can we assign as clock by using gpioclockset()

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. Hi. I am writing an article on IOT and would like to use your image (the spectrum analyser for harmonics). It is ok with you?
    Thanks
    Bede

    ReplyDelete
  5. Attention all Pinterest users…here is a fantastic opportunity that I have just discovered. This company is willing to pay good money to people who know how to pin on Pinterest.

    If you have a Pinterest account and know how you use it, then this could be your lucky day. Get the full story here:

    https://socialsalerep.com?hyden8257

    ReplyDelete
  6. I just had to send you a quick message about this cool new company I have just come
    across. They have this awesome Viral Multiplier Technology that helps social media users
    like you make great money online.
    To find out what it’s all about, just click below: https://socialsalerep.com ?hyden4158

    ReplyDelete