Function generator

While testing stepper drivers for my Reprap clone I had to generate variable frequency signal to one of the inputs. Seems easy enough, whipped up one from 555 timer and two potentiometers. It looked ugly but it did its job. One pot adjusted the low time and another the high signal. Thanks to diode configuration it could achieve any duty cycle. From making it I learned that 78% of 555 jitter problems come from bad potentiometers.

Schematic of my atmega based function generator

Function generator schematic

As another one of my slacking off from big projects I started constructing this function generator. I considered abut a dozen different schematics. With smart function generator ICs, 555 timers, pure analog op-amp designs and so on. But as always MCUs are just 20x cheaper and easier to use than any other design. The final schematic is very easy - it consists of Atmega88 (I had an extra one, use something cheaper), 20MHz crystal (not necessary, frequency will be inaccurate anyway) and resistor ladder DAC. I used two outputs. One from Timer1 PWM output and another from DAC. One could easily another potentiometer and get two PWM outputs with different duty cycle, but I decided not to because it seemed unnecessary. I planned to generate negative voltage and use opamp to offset the DAC output by 2.5V but I was to lazy for that. I can always add this later if I need.

Function generator front panel

Function generator front panel

The main attraction with this project was mechanical design. I bough pretty banana connectors and made it proper front plate. It has power switch and led, pot for frequency, pot for duty, two switches for timer prescaler, two function outputs and two power outputs (one is external 12V output).

Function generator pcb and backside

Another protoboard build

The code is also pretty trivial, reads ADCs and digital inputs and switches timers accourdingly. The only noticeable thing is the play with prescalers. Atmega88 timer1 has five prescalers: 1, 8, 64, 256, 1024. These numbers seem logical but they aren't. I wanted to encode four of these numbers to two switches. One switch is 1X/8X and another is 1X/8X. But 8*64 is 512, and there is no such prescaler. WTF atmel.. giving us prescalers 20, 23, 26, 28 and 210. Also because the code is so slow the maximum usable speed is 100kHz. I could fix that with moving the code around a bit, probably, but whatever, it works.

The testing and setup went almost painlessly. I hooked up the 78L05 the wrong way for the first time but everything survived 7V or whatever it got. Testing showed that the frequency potentiometer is logarithmic for some reason, probably I had one logarithmic one in drawer. It works well on frequency ranges 40Hz to 100kHz, and duty 5-95%. The whole thing is powered from my solar-battery configuration like all other self made lab equipment I have.

Get The Code, Get The Schematic (in KiCad)

Leave a Reply

Your email address will not be published. Required fields are marked *