puqeko / freqy

Analog sine wave generator for the Arduino Uno.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generating a sine wave on the Arduino Uno

Arduino pins are limited to an output of either 0 V or 5 V. However, we can use PWM and a simple filter to produce analog signals. Specifically, freqy.h uses Timer 2 to generate a PWM wave with sinusoidally varying duty cycle. Passing the output signal through a low pass filter will yeild a sine wave signal.

Setup

Download the repository and open freqy.ino with the Ardunio IDE. The example program generates a signal on pin 11. The 440 Hz signal must be filtered out. Use a capacitor and resistor to construct a simple low pass filter as shown below.

Low pass filter

Values for C and R could be 3.3 nF and 10 kΩ respectivly. The idea is to ground the ~31 kHz sqaure wave PWM frequency leaving the average of the duty cycle. Since the duty cycle is sinusoidally varing, so is the filtered signal. You can use the formula

Low pass filter cutoff frequency equation

to select values for R and C. The cutoff frequency, fc, should be somewhere between the PWM frequency and the desired sine wave output frequency.

Connect the output to a small speaker or view the waveform using an oscilloscope. You may notice small spikes along the waveform at a frequency of about ~31 kHz. This is due to transient switching behaviour on the Arduino. Higher order filters can be used to obtain better results.

Functions

  • init_frequency() - This sets up Timer 2 and configures Pin 11 as an output. Call it once at the begining of setup().
  • set_output_freqy(outputFrequency) - Set the desired sine wave frequency (in Hz). This can be called at any time. The default is around 490 Hz.

Useful Resources

About

Analog sine wave generator for the Arduino Uno.

License:MIT License


Languages

Language:Objective-C 62.9%Language:C++ 34.1%Language:C 3.0%