Arduino Function Generator

Alright… We’ve put the work in, lets bring this one home. We’ve got a fairly reliable (if a little bit low res) sine wave generator, but we can make our Arduino do a lot more. Today I’d like to modify my Arduino based sine wave generator to output a variety of predefined waveforms.

To accomplish this I need two things. First I need to program a button to let me select a waveform. Then I’ll need to create tables for these additional waveforms and implement logic to pull values from the correct table.

The Circuit

Arduino Function Generator Circuit

The circuit for this one is essentially the same as the circuit from my last post. The only change I’ve made is the addition of a push button connected between pin 2 on the Arduino and ground. In the code I will set this pin to “pull up” meaning the pin is kept high until the button is pressed. When the button is pressed the pin is connected to ground which will force it low.

Polling Vs Interrupts

When setting up the button press I have two choices. We can either use polling to check for a button press or set up an interrupt. In polling we would add a check in the main loop so that each time the Arduino cycles through the loop it will check the status of the button. This option is very easy to implement but is not terribly efficient.

The analogy I often hear is to imagine you are waiting for a phone call. Polling would be the equivalent of disabling your ringer and checking the phone every 30 seconds to see if the call is coming in. There are a few obvious issues with this approach. What if the call comes in in between the times when you check? you would miss it. Further what if you have a homework assignment your trying to get done at the same time? You wouldn’t make much progress if you stopped every 30 seconds to check your phone.

A better option would be to turn your ringer on and set your phone to the side while you work on other things. This is what happens when we set an interrupt. We tell the Arduino to let us know when an event happens. When something does happen the Arduino takes note of what it’s working on, stops, and proceeds to take care of the instructions we left it for the event.

Setting up an Interrupt

There are a few steps to set up an interrupt for the button press. First I define the pin and create a variable for the pins state. Notice I use the keyword volatile to communicate to the Arduino that this value may change at any time.

#define SWITCH_PIN 2         //Push button to switch waveform
volatile int switch_state = HIGH;

Next comes the setup function for the Arduino. Here I need to set the pinMode. I set this pin as an INPUT_PULLUP so that the pin is held high until the button is pressed.

Also in the setup we will call the attachInterrupt function. This tells the Arduino how to handle this pin. This function takes three arguments. The first of these sets the pin we are attaching the interrupt to. The second gives the name of the function to be called when the interrupt happens. The third identifies the event at the pin which will trigger the interrupt, I have used FALLING here meaning the Arduino will trigger the event when the pin goes from high to low.

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), switch_pin_ISR, FALLING);
  
  get_sine_table();
}

Finally I need to implement the function mentioned in the setup (switch_pin_ISR). My goal here is to use a new variable (table) to track what table is being used. When the button is pressed I want this variable to increment by one which will tell the main loop to start pulling from the next waveform’s table (more on this later). Additionally I don’t want this variable to ever go beyond the number of tables I have so I will use the modulo operator. This ensures that if the variable reaches beyond the number of tables I have defined it will reset to zero.

int table = 0;
/* Identifies current waveform table
 * 0 - Sine
 * 1 - Right Saw
 * 2 - Left Saw
 * 3 - Triangle
 */


void switch_pin_ISR(){
  table = (table + 1) % 4;
  }

Tables Tables Tables

Now that I’ve built up the logic to handle the button control I’d better set up some tables to create the different waveforms. At the time of writing I have 4 waveforms I’m using. These are the sine we originally created, right and left biased saw waves and a triangle. I generate each of these during the set-up function.

int sine_table[100];
int rsaw_table[100];
int lsaw_table[100];
int triangle_table[100];


void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), switch_pin_ISR, FALLING);
  
  get_sine_table();
  get_rsaw_table();
  get_lsaw_table();
  get_triangle_table();
}

For each wave I have created a function to create the table. The logic behind these waveforms is fairly straightforward. For the right sine wave I created loop from 0 to N setting each value to i*(255/N). The left sine is identical except that I subtract the result of that equation from 255 to invert the result. The triangle is slightly more interesting, I used an if statement to separate the values before N/2 from those after. Those before I set equal to i*(255/(N/2)). Those after I set equal to 255- i*(255/(N/2)).

void get_sine_table(){
  for (int i = 0; i < N; i++){
    sine_table[i] = 127+127*sin(i*(2*3.14)/N);
  }
}

void get_rsaw_table(){
  for (int i = 0; i < N; i++){
    rsaw_table[i] = i*(255/N);
  }
}

void get_lsaw_table(){
  for (int i = 0; i < N; i++){
    lsaw_table[i] = 255 - i*(255/N);
  }
}

void get_triangle_table(){
  for (int i = 0; i < N; i++){
    if (i<(N/2)){
      triangle_table[i] = i*(255/(N/2));
      }
    else{
      triangle_table[i] = 255 - (i-N/2)*(255/(N/2));
      }
  }
}

If the logic of these functions seems unclear don’t worry. These kind of algorithms are not necessarily intuitive at first. My best advice is to work through them, take an N value of 5 and calculate the result for each i less than N. Remember that a value of 0 equates to 0V output and 255 equates to 5V output. Your results should mimic the desired waveform.

The Switch

The final piece of this puzzle comes in the main loop. We have tables for each waveform, a variable to track which table we’re using and an interrupt to modify that variable. Now we just have to read that variable and pull our PWM values from the correct table. Here I’ve used a switch statement. If you’ve programmed mostly in Python or Java you may not have encountered switches before. A switch is essentially a clean way to write a very ugly if else statement. We define a variable to evaluate and then give a list of “cases” for different values of that variable. Each case describes the actions to take if the variable equals that value. If you’d like to learn more about switch statements Geeks for Geeks have a great article about them.

void loop() {
  for (int i = 0; i < N; i++){
    sample_time = analogRead(ANALOG_0);
    sample_time = (sample_time/16)+2;

    switch (table) {
      case 0:
        analogWrite(PWM_PIN, sine_table[i]);
        break;

      case 1:
        analogWrite(PWM_PIN, rsaw_table[i]);
        break;

      case 2:
        analogWrite(PWM_PIN, lsaw_table[i]);
        break;

      case 3:
        analogWrite(PWM_PIN, triangle_table[i]);
        break;


      
      }
      
    delay(sample_time);
  }
}

All The Code

With that we’ve done it! We’ve turned our Sine Wave Generator into a proper function generator. There’s no reason to stop here though, I’ve only scratched the surface of the available waveforms. Additionally there are better filters and better algorithms begging to be explored. Now that I have this up and running I’d also like to spend some time in a future post exploring how to interface it with other oscillators to use it as an LFO. For today I’ll finish up by providing my code in full, Take care. I’ll see you all soon.


/*
 * www.SamVsSound.com
 * Arduino PWM Function Generator
 * 11/14/2020
 */

#define PWM_PIN 5            //PWM output at pin5
#define ANALOG_0 A0          //Input for sample_time
#define SWITCH_PIN 2         //Push button to switch waveform

#define PWM_FREQ  980        //Hz
#define N 100

int sine_table[100];
int rsaw_table[100];
int lsaw_table[100];
int triangle_table[100];

volatile int switch_state = HIGH;

int table = 0;
/*
 * 0 - Sine
 * 1 - Right Saw
 * 2 - Left Saw
 * 3 - Triangle
 */


int sample_time;

void get_sine_table();
void get_rsaw_table();
void get_lsaw_table();
void get_triangle_table();

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SWITCH_PIN), switch_pin_ISR, FALLING);
  
  get_sine_table();
  get_rsaw_table();
  get_lsaw_table();
  get_triangle_table();
}

void loop() {
  for (int i = 0; i < N; i++){
    sample_time = analogRead(ANALOG_0);
    sample_time = (sample_time/16)+2;

    switch (table) {
      case 0:
        analogWrite(PWM_PIN, sine_table[i]);
        break;

      case 1:
        analogWrite(PWM_PIN, rsaw_table[i]);
        break;

      case 2:
        analogWrite(PWM_PIN, lsaw_table[i]);
        break;

      case 3:
        analogWrite(PWM_PIN, triangle_table[i]);
        break;


      
      }
      
    delay(sample_time);
  }
}

void switch_pin_ISR(){
  table = (table + 1)%4;
  }

void get_sine_table(){
  for (int i = 0; i < N; i++){
    sine_table[i] = 127+127*sin(i*(2*3.14)/N);
  }
}

void get_rsaw_table(){
  for (int i = 0; i < N; i++){
    rsaw_table[i] = i*(255/N);
  }
}

void get_lsaw_table(){
  for (int i = 0; i < N; i++){
    lsaw_table[i] = 255 - i*(255/N);
  }
}

void get_triangle_table(){
  for (int i = 0; i < N; i++){
    if (i<(N/2)){
      triangle_table[i] = i*(255/(N/2));
      }
    else{
      triangle_table[i] = 255 - (i-N/2)*(255/(N/2));
      }
  }
}

Sine Waves on Arduino: Variation

Today Ill be building on my Arduino Sine Wave Generator project. My goal is simple, to add the ability to dynamically change the frequency of the wave being generated. I should be able to read the value from a potentiometer similar to what was used in my original Arduino PWM post and write that value into the delay variable to modify the timing.

Wiring it Up

Arduino Function Generator Wiring

The wiring for this one is pretty straight forward and nothing we haven’t seen before. On the left is a potentiometer connected between 5V and ground. The wiper of this potentiometer is connected to analog pin A0 on the Arduino. This pin will provide an input value between 0 and 1023 depending on the position of the pot.

On the right we have the digital pin 5 connected through the same low pass filter I’ve used in previous projects. This will take our PWM output and smooth it into the final waveform.

The Code

The code only needs some slight modification from my last post. We need to first define the analog pin and read its value using analogRead. This is where some choices need to be made though. I mentioned that the analogRead for the potentiometer gives us a value from 0 to 1023. If we were to write this directly to the sample_time variable we would be producing some extremely long sine waves. Since our sine table has 50 entries this would mean at the highest setting each wave would take 51.15 seconds (1023*50/1000) to complete. That might be a bit much. So we need to decide how large a range we want to use and process the input accordingly.

I chose to divide the input by 16, this gives me a frequency range from about 10Hz to 0.313Hz. In the code you’ll notice I added a hard limit at 10Hz by adding 2 to the input value. This ensures the delay time never drops below a range that the Arduino and our filter can handle.


#define PWM_PIN 5            //PWM output at pin5
#define ANALOG_0 A0          //Input for sample_time
#define PWM_FREQ  980        //Hz
#define N 50

int sine_table[50];
int sample_time;
void get_sine_table();

void setup() {
  get_sine_table();
}

void loop() {
  for (int i = 0; i < N; i++){
    sample_time = analogRead(ANALOG_0);
    sample_time = (sample_time/16)+2;
    analogWrite(PWM_PIN, sine_table[i]);
    delay(sample_time);
  }
}

void get_sine_table(){
  for (int i = 0; i < N; i++){
    sine_table[i] = 127+127*sin(i*(2*3.14)/N);
  }
}

And there you have it! We can now adjust our frequency on the fly. Sorry it’s a bit of a shorter post today life’s been pretty full of late. Still I think this marks a pretty substantial step forward in this project. In my next post I’ll be looking at implementing the ability to produce more waveforms beyond just the sine.

Sine Waves on Arduino

I’ve spent the last few weeks going over the fundamentals of PWM and implementing them on my Arduino Uno. Today it’s time to apply those basics to something a little more interesting. I want to get my Arduino outputting sine waves. I’m going to focus on creating a workable 10Hz sine wave and then in forthcoming posts I’ll look at manipulating the frequency and modelling more interesting wave-forms.

Know Thy Limits

Before I go to far into this project I want to talk a little bit about the limitations of the Arduino. The Arduino has a maximum PWM frequency of 980Hz which is quite low, especially when working in the audio space. As an example if I wanted a sin wave with a frequency of 100Hz I would only be able to use approximately 10 PWM cycles per sine wave. The fewer cycles you have per wave the more misshapen your output wave will be. I’ve had some success creating “relatively” clean 100Hz waves but going much higher than this results in the wave breaking down entirely.

One nice thing about the sine wave is that the changes in voltage through the wave are continuous. This means we can push our samples much closer than the normal settling time discussed in my last post. However, the idea of creating an full audio oscillator with this method is an impossibility. An LFO or envelope generator though are still realistic goals.

If you are interested in creating an audio oscillator using your Arduino I would recommend looking into off-board DACs (Digital to Analog Converters) which use serial communication with the board to provide much higher output speeds and resolutions.

The Filter

Low Pass Filter With RC=0.0047

To get started I set up an RC Low Pass Filter. After the experiments in my last post I settled on an RC value of 0.005 (or 0.0047 more accurately). This should give me a good balance between stability and low settling time. I used a 47K ohm resistor and a 0.1uF capacitor to accomplish this.

The Sine Wave

Sine Wave Equation with DC Offset

I want to have a quick look at the sine wave equation before I start coding. There are a number of variables which we can use to adjust the waves properties. The first thing to note is the variable n. If you’ve worked with continuous sine waves you may have seen this formula as a function of t. Where t represents time (a continuous variable), n represents discreet samples. This means n will always be a whole number (0, 1, 2, …) which lends itself to working in a digital environment.

The next variable we have to look at is w which represents the angular velocity (also called normalized frequency depending on your background). This represents how quickly your sine wave will move through a full cycle. We can determine the value for w with the following formula:

In the above formula N represents the total number of samples it will take to traverse a full cycle of the sine wave.

Next up is A which is known as the amplitude or scaling factor of the wave and C which represents the offset. A typical sine wave varies between 1 and -1 centered at 0. However, on the Arduino the duty cycle of a PWM signal is set by an integer between 0 and 255. To get our sine wave to fill this range we first multiply the sine wave by 127 (A). This creates a wave which varies between -127 and 127. Next we add the offset of 127 (C) to the output which gives us our final sine values between 0 and 254.

Choosing Your Frequency

Two factors affect the frequency of the sine wave. N (Discussed above) is the first. If you are moving through your samples at a constant rate the more samples you have the longer it will take. The trade off here is as you might imagine, the more samples you have the cleaner your sine wave will look.

Sample time (given in ms) is the other. This is the time the Arduino will wait between each step of the sine wave. This again is something of a balancing act as you must allow enough time for your signal to stabilize at each new voltage level.

Taken together we can create the following formula to determine our final frequency:

Discreet Frequency Calculation

You may recognize that this is essentially the calculation to find frequency from the period (f = 1/p). In this equation N*(sample time) represents the period (the number of samples * the time it takes for each sample). Since sample time is in ms we use 1000 as a conversion factor.

The Code

Now that we’ve got everything set up it’s time to write the code. I’ll start out by declaring the necessary variables:

int PWM_out = 5;           //PWM output at pin5

int sample_time = 5;       //ms
int N = 20;                //int
int duty_cycle;            //unitless

int i;                     //Counter variable

Here you can see I’ve set the sample_time to 5 and N to 20. Using the formula I showed earlier this should give me a frequency of 10Hz (1000/(5*20) = 1000/100 = 10Hz).

For this program I don’t need to run any kind of set-up. That means I can leave the set-up block blank:

void setup() {
}

Next comes our main loop where all the magic happens:

void loop() {
  for (i = 0; i < N; i++){
    duty_cycle = 127+127*sin(i*(2*3.14)/N);
    analogWrite(PWM_out, duty_cycle);
    delay(sample_time);
  }
}

Lets go through this one in a bit more detail. First I set up a for loop which will iterate through the code N times incrementing i each time.

Next I calculate the duty_cycle value using the sine wave equation. Notice I have placed the angular velocity equation directly inside the sine equation (2*pi/N) to simplify the line. From there we write that duty cycle value to the PWM_out pin (pin5) using analogWrite.

Finally there is a delay command. The delay waits for the amount of time defined by sample_time before beginning the next iteration of the loop.

10 Hz Sine Wave From Arduino

And there we have it. As you can see from my oscilloscope output the wave is far from perfect, nonetheless we’ve created an analog sine wave using a digital microcontroller! This is no small accomplishment and I can’t wait to build on it further. I encourage you all to set up this code and play around. Try changing the variables and values to see how each impacts the design. I’ll be back soon with more!