# Arduino Function Generator (Part 3)

In my previous posts in this series I looked at a couple of ways to use an Arduino to generate analogue waveforms.

In this third part I look at a much simpler, IC-based digital to analog (DAC) circuit to provide the waveforms, and look at ways of changing the frequency of the output. # 555 Astable Oscillator

In my last few posts I’ve been writing about using an Arduino to generate waveforms. Part 3 of that series is still in the works (I’ve not forgotten, honestly!) – but this time I want to write about an alternative way to make a simple waveform: using good old analogue electronics, without the need for a microcontroller.

To do this we’ll use the ubiquitous NE555 timer chip (or just a 555 for short). The 555 is almost certainly the first IC you’ll play with if you’re learning analogue electronics at school or college – and for good reason, you can do all kinds of things with 555 timers – and whole books have been written on the subject. I’m not going to compete with any of those (Google, Amazon and Wikipedia will be your friends if you want to find out more). Or have a look at the datasheet

Using a 555 to produce a square wave is a really nice easy circuit to have a go at if you’re new to electronics: as it’s not too complicated to build – and it’s pretty easy to understand what’s going on.

There are a handful of different basic ways to use a 555 – known as monostable, bistable, and astable.  In a monostable configuration, the 555 will generate a single pulse (or a length determined by the configuration of the rest of the circuit), so it can be used, for example, as the basis of a simple electronic timer (such a circuit was my first ever electronics project – about twenty years ago!).  In a bistable circuit, the 555 can be used as a latched  flip-flop – turning on or off an output, depending on the state of two pulse inputs.

We’re interested in the astable configuration though – as that will let us generate a series of pulses in the form of our square wave.

Let’s start by looking at the circuit. As you can see it’s a pretty simple circuit – with just the 555 IC, two resistors, and two capacitors.  Additionally if we want to be able to vary the frequency, we can add a potentiometer (variable resistor) in series with R2, and to drive larger loads we can use a transistor switched by the output from pin 3 (as I’ve done here)… So how does it work?

First of we need to understand an RC (resistor – capacitor) network.  A capacitor charges and discharges at a rate proportional to the value of the resistance. This means that there’s an exponential relationship between the capacitor voltage and time… Specifically: When the voltage on the timing capacitor (C1) reaches 2/3 of the supply voltage (Vcc) the 555 triggers the flip-flop (via pin 2) and the capacitor starts to discharge, when it drops to 1/3 Vcc, the flip-flop flips again, and starts the capacitor charging again. When C1 is charging the RC is dependant on both R1 and R2; when it’s discharging only R2 comes into play because of the connection to pin 7 between the two resistors.

Don’t worry if you don’t understand exactly how the 555 flip-flop works (it’s slightly out of the scope of this post – but maybe I’ll write something about that if people are interested): the important thing is the that the time taken to perform these charge / discharge cycles is what determines the frequency of the square wave generated.

Specifically the frequency is the reciprocal of the time taken for the circuit to go high + the time taken to go low… So in our case the frequency should be: And, sure enough – here’s what the oscilloscope gives us… # Arduino Function Generator (Part 2)

Last time, we looked at some Arduino code that we could use to generate some square waves.

The problem with the setup we’ve been looking at so far, is that we can only produce signals of one amplitude – equivalent to the HIGH logic level.

In order to be able to produce any other waveforms we’ll need to be able to produce a variety of different output voltages. Although the PWM method we looked at last time gives us a way to do this, it’s not suitable for producing variable waveforms – as it’s time-based. We can see this, if we try to use PWM to produce a triangular waveform: and view the output with an oscilloscope.

```void setup()
{
pinMode(11, OUTPUT);
}

void loop()
{
for (int i=0; i<0; i--)
{
analogWrite(11, i);
delay(1);
}
}``` If we hook that up to our oscilloscope – it looks like this: It’s not too easy to see that as a still image – so here’s a video clip of the same thing…

That’s clearly not really what we wanted: so we need to do something differently.

The solution to this problem, is to use some kind of digital-to-analogue converter (or DAC). To begin with, let’s build our own… This circuit is an 8-bit DAC known as an R-2R resistor ladder network. Each of our eight bits contributes to the resultant output voltage. If all 8-bits are HIGH – then the output is approximately equal to the reference voltage… If we switch the most-signifincant bit to LOW – then we get approximately half of that voltage.

More precisely, for an 8-bit DAC if all 8-bits are HIGH – then we get 255/256th of the reference voltage. Switching the most-significant bit to LOW gives us 127/256th of the reference voltages. More generally for any bit value x, between 0 & 255, our DAC will us x/256th of the reference voltage.

There’s a short article on wikipedia explaining R-2R DACs in a little more detail, if you want to know more…

Okay, now that we’ve built our circuit – let’s do something with it.

We’ll start by producing a triangular waveform…

```void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}

void loop()
{
for (int i=0;i<255;i++)
{
PORTD=i;
}

for (int i=255;i>0;i--)
{
PORTD=i
}
}```  Note that here we’re using `PORTD` instead of setting the state of the individual output pins one at a time. This is much faster, and it (critically) ensures that all of the pins change at the same time – and that’s important given we want to switch smoothly though digital values to create a smoothly changing analogue voltage.

`PORTD` is port register D. Our use of it here works because it maps to the 0-7 digital pins (giving us 8-bits); and writing either a binary or decimal value to the register – we control all 8 pins in one operation. For example, if we assigned it the decimal value 123 (which equates to `B01111011`) would set pins 6, 5, 4, 3, 1, & 0 to HIGH… Now the problem is that pin 0 is used for communicating serial data – so using `PORTD` means that we can’t use any serial communications whilst code it running. It is, however, the only way to manipulate 8 output pins simultaneously (and we didn’t need serial communications for this application anyway).

We can modify our code very easily, to produce a saw-tooth waveform – by simply commenting out (or otherwise removing) the second of the two `for` loops.

Note that if we want to drive anything significant (even something as lowly as an LED) we need add a transistor into the circuit, like this: Finally, on to a sine wave. We’ll need to pre-compute some values for the output voltages over time (the arduino just isn’t quite fast enough to be able to this in real-time, in a situation quite as time-sensitive as producing a waveform).

A sine wave has a cycle of 2π radians – so to produce a sine wave with 256 time-steps per cycle, we just need to calculate y=sin((x/255)*2π) for each point value of x. Since the sine function gives us values between 1 and -1; and we want values between 0 and 255, the simplest way to do this is to multiply the `float` value by 128 – giving us a value between -128 & +128; and then add 128 – to give us the correct range.

We could do this off-line; but that’s a bit tedious – so we’ll get the arduino to do this for us, in the `setup()` phase, storing the results in a global array. Then all we need to do in our main loop, is cycle through the array.

```int sine;
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
float x;
float y;
for(int i=0;i<255;i++)
{
x=(float)i;
y=sin((x/255)*2*PI);
sine[i]=int(y*128)+128;
}
}

void loop()
{
for (int i=0;i<255;i++)
{
PORTD=sine[i];
delayMicroseconds(10);
}
}``` If we run that code, with our DAC, and have a look at that on our oscilloscope we see that we do indeed have something that looks quite a lot like a sine wave. That’s all for this part, next time we’ll have a look at varying the frequency of our waveforms; and look at a more compact and accurate way to do the digital-to-analogue conversion, using a dedicated IC…

# 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…

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. (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. 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);
}``` 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… 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). 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=map(value, 0, 1023, 1, 19);
digitalWrite(8, HIGH);
delay(1);
digitalWrite(8, LOW);
delay(value);
}``` 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%… 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.