Arduino: Audio Frequency Generator

Back in good ol’ 2012 (the year the world was supposed to end), I posted some code for a simple Arduino controlled low-frequency oscillator (LFO). It has made its way into some very interesting projects over the years, but recently I was asked in the comments if the code could be modified to have a wider frequency output up into the kHz range. I thought it was a good question, so I made the effort to see what the limitations could be.

How It Works

So, the original LFO employs timers on an ATMega328 (or ATTiny85 depending on which version of the code) to generate the audio output. By using a wave table of a basic sine wave stored in an array, one timer (TIMER0) winds up setting the sample rate by grabbing each sample from the array at a configured interval, and the other timer (TIMER2) creates a PWM output on the selected pin (pin 3 in the code). So by setting an 8-bit value on OCR0A, the sample rate can be adjusted which adjusts the frequency on the output of TIMER2. The datasheet for the ATMega328 gives the following formula for the frequency of the output compare register.

If we take this formula and divide it by the number of samples, we get the output frequency. That means that the output frequency can also be scaled by reducing the number of samples available. So, I made a big output table in LibreOffice and saw that an OCR0A value of 128 was twice the frequency from 255. At 255, I could reduce the number of output samples by half and sweep over the same sample rates but get an increase in output frequency. This introduced a method of frequency scaling. The trade-off, however, is that the sine wave on the output is much less sinusoidal being that it has lower resolution, and that introduces higher order harmonics thus changing the timbre of the sine wave.

Another thing to keep in mind is that the sample rate from the output PWM gets very low and even into the audio range itself. This means that some kind of audio filter is needed on the output. I put a first order low-pass filter using a 2.2k series resistor and a 10 uF capacitor to ground. This worked surprisingly well, though some of the sampling noise shows up on the output at very low frequencies. Being an 8-bit audio signal, this isn’t much to worry about in the grand scheme of things since. I also found that the noise on the output could be lowered by implementing a simple moving average filter in the code on the 10-bit ADC input of the rate control if using a potentiometer to set the frequency. With the timer prescalar set to 64, we should get 64 clock ticks before the next sample is grabbed, so this left some room for some calculations to happen in between sample changes.

It’s all pretty experimental, but it seems to work alright. The frequency range is from around 1 Hz up to 14 kHz (more or less). If you’re using a pot to control the rate, use a linear one as the frequency output gets very logarithmic towards the top of the range.

The Code

// Project: Arduino Audio Frequency Signal Generator
// Version: 1.0
// Author: Abram Morphew
// Date: 2021.10.12

#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))

uint8_t sineTable256[] = {

uint8_t sineTable128[] = {
  37,42,47,52,57,62,67,73,79,85,90,97,103,109,115,121 };
uint8_t sineTable64[] = {

uint8_t sineTable32[] = {

uint8_t sineTable16[] = {

uint8_t sineTable8[] = {

uint8_t sineTable4[] = {

uint8_t sineTable2[] = {

uint8_t tWave = 128;
uint8_t sWave = 255;
uint8_t ruWave = 128;
uint8_t rdWave = 128;
uint8_t rWave = 128;
uint8_t inc = 1;
uint8_t r = 0;

int   n = 32;        // number of averages for ADC input (should be a power of 2: 32 max)
int   ocr[32] = {};
int   i = 0;
int   t = 0;    // time delay index for rate sweep
int   sweep = 1023;    // sweep
int   rate = 0;
int   waveform;
byte  d = HIGH;
byte  down = LOW;    // increase freq if LOW, decrease freq if HIGH
byte  rateSelectPin = 1;
byte  waveSelectPin = 0;

void setup() {
  pinMode(waveSelectPin, INPUT);
  pinMode(rateSelectPin, INPUT);
  pinMode(3, OUTPUT);
  OCR0A = 128;

void loop() {
  // -- Waveform Selection
  waveform = map(analogRead(waveSelectPin),0,1023,1,7);
  //waveform = 1;
  // -- Frequency Selection with ADC pin 1
  if (r >= n) r = 0;
  ocr[r] = map(analogRead(rateSelectPin),0,1023,1015,3);
  // -- ADC input averaging
  for(uint8_t c = 0; c < n; c++) { rate += ocr[c]; }  
  rate = floor(rate / n);
  // Uncomment for frequency sweep
  if (t >= 2048) {
    t = 0;
  if (sweep <= 4) {
    sweep = 1023;
  rate = sweep;
  //----------------------- */

  // rate scaling with increasing frequency
  if (rate < 128) {
    inc = 128;
    rate = rate * 2;
  } else if ((128 <= rate) && (rate < 256)) {
    inc = 64;
    rate = rate;
  } else if ((256 <= rate) && (rate < 384)) {
    inc = 32;
    rate = rate - 128;
  } else if ((384 <= rate) && (rate < 512)) {
    inc = 16;
    rate = rate - 256;
  } else if ((512 <= rate) && (rate < 640)) {
    inc = 8;
    rate = rate - 384;
  } else if ((640 <= rate) && (rate < 768)) {
    inc = 4;
    rate = rate - 512;
  } else if ((768 <= rate) && (rate < 896)) {
    inc = 2;
    rate = rate - 640;
  } else if ((896 <= rate) && (rate < 1024)) {
    inc = 1;
    rate = rate - 768;
  OCR0A = rate;

  if(i >= (256 / inc)) i = 0;
  switch(waveform) {
    case 1:
      OCR2B = sine(i, inc);      
    case 2:
      OCR2B = triangle(i);
    case 3:
      OCR2B = square(i);
    case 4:
      OCR2B = rampUp(i);
    case 5:
      OCR2B = rampDown(i);
    case 6:
      OCR2B = rand(i);
    case 7:
      OCR2B = white(i);

void setupTimer() {
/*--- TIMER2 CONFIG ---*/
  sbi(TCCR2B, CS20);
  cbi(TCCR2B, CS21);
  cbi(TCCR2B, CS22);
 /*--- TIMER0 CONFIG ---*/ 

  sbi(TCCR0A, COM0A1);
  cbi(TCCR0A, COM0A0);
  cbi(TCCR0A, WGM00);
  sbi(TCCR0A, WGM01);
  cbi(TCCR0A, WGM02);


int sine(int i, int inc) {
  if (inc == 1) {
    return sineTable256[i];
  } else if (inc == 2) {
    return sineTable128[i];
  } else if (inc == 4) {
    return sineTable64[i];
  } else if (inc == 8) {
    return sineTable32[i];
  } else if (inc == 16) {
    return sineTable16[i];
   } else if (inc == 32) {
    return sineTable8[i];
  } else if (inc == 64) {
    return sineTable4[i];
  } else if (inc == 128) {
    return sineTable2[i];

int triangle(int i) {
  if(tWave >= 255) d = LOW;
  if(tWave <= 0) d = HIGH;
  if(d == HIGH) tWave = tWave + inc;
  if(d == LOW) tWave = tWave + inc;
  return tWave; 

int rampUp(int i) {
  ruWave = ruWave + inc;
  if(ruWave > 255) ruWave = 0; 
  return ruWave;

int rampDown(int i) {
  rdWave = rdWave + inc;
  if(rdWave < 0) rdWave = 255;
  return rdWave;

int square(int i) {
  if(i >= (128 / inc)) sWave = 255;
  if(i < (128/inc)) sWave = 0;
  return sWave;

int rand(int i) {
  if(i == (rWave / inc)) rWave = random(255);
  return rWave;

int white(int i) {
  return random(255);

7MHz Crystal Oscillator Design

After constructing a 40m wire dipole that works with my SDR setup, I needed to start working on a transmission system. At the heart of virtually any RF system lies a stable oscillator, and crystal oscillators are ubiquitous in many low-power (QRP) rigs simply because they are so stable. After some rough math and a lot of simulations in LTSpice, I came up with this design to give me somewhere around 7 dBm of power.

The first stage is simple Colpitts oscillator topology with a “bent” 7.03 MHz crystal resonator. The 30 pF variable capacitor (C1) provides around 2 kHz of tuning. The output stage is a common-collector (Q4) and emitter-follower (Q3) with a negative feedback loop. I forget exactly where I saw this configuration, but I thought I would try it out and see if it worked. As you might have guessed, the output is loaded with higher order harmonics resulting in a waveform that doesn’t resemble a sine wave at all. I made sure to include a simple second-order low-pass Butterworth filter on the output to filter the output.

Pictured above is the ugly constructed version of the oscillator in all it’s dead-bug style. I built it on a scrap piece of double-sided FR4 and overall it’s performance came out fairly close to what LTSpice had predicted. I got around 6 dBm of output and the second harmonic is around 29 dB down (around -23 dBm). That’s not the cleanest of signals, but it’s about right for the filter. Below is the output shown on my HP 8595E spectrum analyzer.

For the next phase, I’ll likely be adding control of the oscillator via an ATTiny chip. This will give me the ability to automate on-off keying of the device turning it into a simple CW beacon. One thing that could be improved is the current draw (~40 mA) from the emitter-follower at Q3. Basically, it’s a class A amplifier so it’s not the most efficient design in the world, but it provides enough power for driving an ADE-1 and could run off a small solar panel if I wanted to use it in the field.

2m AM Exciter

Here’s a project that I’ve been working on for a class recently. This is a very basic 2m AM Transmitter that operates from 144.25 to 144.31 MHz. It does put out a low powered RF signal (around 5 mW PEP) that has been copied over 1 mile from the transmitter. This is an ongoing project that will likely evolve into something else, so I should get time to do some more in-depth posts as time goes on.

simple phase shift oscillator…


a practice in designing an oscillator. this particular circuit employs a 2N2222A transistor on 6 volts. i used this page to design the circuit. there were many reasons for this experiment, but the most immediate was to act as a simple CW practice oscillator. the final circuit looks something like the following.


Square-wave Oscillator Meets PT2399…

Square-wave Oscillator with PT2399… from abram on Vimeo.

Very short video of the waveform captured using Xoscpe of the PT2399 applied to the LM386 square-wave oscillator. Sorry for the bad focusing. I was in a rush. I just needed a visual capture for later analysis.

[audio:|titles=i just broke the gameboy]

…and here’s a clip of the aforementioned wave from from around the same time.

LM386 Square-wave Oscillator

Here’s a small LM386-based square-wave oscillator built from the following schematic. I replaced the 30k resistor with a 50k Potentiometer from my stash of parts which then, of course, acts as a pitch controller. The following audio is some track recordings made in Ableton 8.0 with a little filter on one track and some reverb the air-raid siren sounding noise.