Can I Implement a PLL on an Arduino?
A PLL is a very complex device which is not something that can easily be created purely in code.

Many microcontrollers include a PLL as part of their oscillator systemm which allows them to run internally at a higher speed than their external crystal.  This is not a programmable peripheral that you can use for your own things, but an integral part of the workings of the microcontroller.

While it may be possible to write one in software the results would be very somewhat limited. The best you could hope for is to use timers to measure an incoming frequency and use the results to output another frequency through some means, maybe PWM.

To create a real PLL you would be best off using either a dedicated PLL chip (which you can buy off the shelf) or use some form of programmable logic like a CPLD that has a built in PLL block.

Some microcontrollers, most notably the Cypress PSoC range, have programmable logic blocks built in that may be more suited to your task than an Arduino.

A typical digital PLL looks something like this:



Implementing all that in code would be both tricky and somewhat slow on a little 8-bit MCU with no floating point support.  However, the basic principal of what a PLL can be summarised in one simple sentence:


  The frequency coming in determines the frequency going out.


The PLL, or Phase Locked Loop is just one method of achieving that desired result.

Another method, which is not using any form of PLL, is purely algorithmic and consists of just three things:



Three simple blocks, all of which the Arduino is, to a certain extent, capable of performing.

The first block, the Frequency Detector, works out what the frequency of the incoming signal is.  That could either be by counting the number of pulses that arrive over a certain time frame, or measuring the time between successive pulses.  Which method you use (Frequency Counting or Frequency Measuring) is really down to what frequency ranges you are using.  Frequency Counting is better for higher frequencies, whereas Frequency Measuring is better for low frequencies.  There are many examples on the web of how to do both of those using timers on the Arduino.

The Decision Making is basically working out what your output frequency wants to be according to your input frequency.  This could be as simple as "Output = input * 2", for instance, or something more complex like "Between these frequencies I want to output this frequency, between these frequencies this other frequency, etc".  It's entirely up to you.

The third block just takes the decided-upon output frequency and generates it.  That could be done using the hardware PWM generation of the Arduino.

All this, though, will be limited to relatively low frequencies (tens of kHz maximum really) though with clever coding you could push it up slightly higher.

One thing that really makes this method differ from a PLL is that you don't get the "PL" portion of it - that is, the phase of the incoming frequency is lost, and the output frequency is running entirely with its own phase.  There is little or nothing you can do to synchronise the phases of the input and output like you get with a full PLL.