Arduino Function Generator (Part 1)

I was looking around for an interesting Arduino project, and I came up with the idea of making a function generator (also called a signal generator). The reason I picked a function generator is that it gives us the chance of playing with some interesting circuits – and some interesting code…

Before we start with that – what is a function generator?

A function generator is a circuit that generates some kind of waveform. There are four main types of waveform – the square wave, triangular & saw-tooth waves, and the sine wave.

There’s a good article on function generators on wikipedia.

A dedicated function generator will cost a hundred pounds or more – but it would be very much more capable than anything we’ll build here; but this will give us a chance to look at a few interesting things.

The simplest waveform to get an Arduino to produce is a square wave. The square wave (as the name suggests) simply cycles between the HIGH and LOW logical levels.

An idealized square wave

(Actually it’s not really that simple at all – a square wave produced by an analogue oscillator is actually made up of a complex mixture of multiple harmonics – wikipedia has a description of this; but we won’t concern ourselves with this…)

The circuit we need to produce a square wave, is pretty much the most trivial circuit imaginable.

In fact all we need to do is connect whatever it is we’re driving, to one of the digital output pins of the arduino (I’m using pin 8 here); and the arduino’s ground.

Square Wave Circuit

The code is very simple, too…

void setup()
{ 
    pinMode(8, OUTPUT);
}

void loop() 
{
    // A total delay of 4 ms = 1/0.004 = 250 Hz… 
    digitalWrite(8, HIGH);
    delay(2); 
    digitalWrite(8, LOW); 
    delay(2); 
}

Creative Commons License


Square wave code by AJP is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.


If we want to drive something like a piezoelectric speaker we can connect it to the pin, and the arduino’s ground; and we’ll be able to hear a tone. Alternatively if we pick a low enough frequency we could hook up an LED to see it blink. In fact, if you do that, you’ll note that this setup and code is actually just the blink demo.

Note that we can also use delayMicroseconds() to enter smaller delay values – giving use the ability to produce higher frequencies than the 500Hz that we’d otherwise be limited to…

In fact it’s even easier than this – as Arduino has a function to generate a square wave. For example, to produce the same 250Hz square wave as we had before, we can use: tone(8, 250); in place of the digital writes…

Let’s see exactly what this square wave looks like – buy hooking it up to an oscilloscope…

Apart from the frequency of the wave, there are a couple of other main characteristic that we might want to modify: the amplitude (a measure of how much higher the HIGH level is, compared to the LOW), and the symetry of the wave – the so-called duty cycle. We’ll leave the amplitude aside until next time – as that’ll require us to do a little more work; but let’s look at the duty cycle of the wave.

The duty cycle is simply a measure of how much of the time our wave is at the HIGH level – compared with the total time of the cycle. So far all of our square waves have been symmetrical: with an equal amount of time spent in both states. This is known as a 50% duty cycle.

If we want to change that, we can’t use the tone() function – as that’s designed to produce a symmetrical wave. Instead we need to use the digitalWrite() and delay() functions. So to create a 25% duty cycle square wave at 250Hz – we’d use delays of 1 ms after the LOW, and 3ms after the LOW.

If we use an oscilloscope we can see the shape of the resulting wave…

250Hz 25% Square Wave

With the timebase set to 2ms per division, we can clearly see the 1ms width of the pulse, and the 3ms gap between the pulses.

What’s interesting is what happens if we try measuring the voltage with an instrument that’s less time-sensitive – such as a multimeter. A 250Hz signal with a 50% duty cycle, yields an average of 2.4V (which is exactly half of the 4.8V my meter shows the +5V output of the arduino to be). With a 25% duty cycle – we get 1.2V (one quarter of the HIGH voltage); and so on…

In fact, this concept may already be familiar to you – if you’ve used the analogWrite() function – we have just reimplemented the pulse width modulation (PWM) technique that Arduino uses to provide analog (or analog-like) voltages. We can show this in a couple of ways. Firstly we’ll measure the voltage across an LED during an analogue write with our oscilloscope; and then we’ll show that by adjusting the duty cycle of our code, we can dim an LED.

First lets look at a PWM voltage, giving us a 50% duty cycle (or an average output of around 2.4V).

Arduino PWM 50% Duct Cycle

As you can see the shape is identical to what we saw before in our own version – although the frequency here is different (the Arduino reference document states that the PWM frequency should be approximately 490Hz…

Now let’s try implementing our own version of PWM…

First here’s a schematic of the new circuit…

As you can see we’re simply driving an LED via pin 8 (not forgetting the current limiting resistor, of course); and we’re getting a voltage (between approximately 0V and the reference voltage) from the potentiometer, and feeding it into analog pin 2.

Now for the code.

int value=0; 
void setup() 
{ 
    pinMode(8, OUTPUT); 
    pinMode(2, INPUT); 
} 

void loop() 
{
    value=analogRead (2); 
    value=map(value, 0, 1023, 1, 19); 
    digitalWrite(8, HIGH); 
    delay(1); 
    digitalWrite(8, LOW); 
    delay(value); 
}

Creative Commons License


PWM demo code by AJP is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.


As you can see this is also very simple code. We call analogRead() to get the voltage – and then (for simplicity) we map it onto a range between 1 & 20 – and then use that as our millisecond delay value on the LOW part of the cycle – giving us a (theoretical) duty cycle of between 50% and 5%. I say a theoretical duty cycle – because in reality the time it takes the arduino to do the read and the map will actually throw off our timings. My multimeter shows a voltage of 2.16V rather than the expected 2.4V – dividing the reference voltage (4.8V) by the measured voltage (2.16V) gives us a a duty cycle of around 45%…

PWM Demo

As we can see from a more accurate measurement of the duty cycle, we see it’s around 46% – which implies that the time spent in the LOW state is actually around 1.08ms: hence approximately 80µS of additional time in the cycle spent at LOW whilst arduino executes the analog read and map functions). So if we were going to use this in reality – we’d probably need to find a more efficient way of determining the value for the second delay.

Or, more realistically, we could just use the analogWrite() function (which uses it’s own timer – independent of the main processing loop…

If we adjust the ‘scope to show the average voltage – we see that we get approximately the same voltage as the volt meter gave us…

Okay; that’s all for now. But be sure to come back soon for part two – when we’ll start to look at some ways to implement one of the more interesting waveforms: the sine wave.

One thought on “Arduino Function Generator (Part 1)

  1. Sir, I have to make a function generator capable of generating square wave upto 20KHZ. How can we increase the frequency range of this circuit and I need a schematic for it.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.