ATTiny25-based function generator

One of my very first forays into programming microcontrollers came when I needed a square-wave generator.  It was super simple, based on a Tiny25, only had sixteen discrete frequencies (each frequency was double the previous), and was housed in a rough wood container I made for it.  Now, I’ve made something better.

An ATTiny25 still forms the core of it, but it’s a whole lot better. The frequency range is 256 steps, increasing exponentially from approximately 1HZ to 40KHz. It outputs simultaneous sine, triangle, and square waves, with adjustable duty cycle/triangle wave shape. It has independent amplitude control on each output. And it’s all on one palm-sized, single-sided PCB.

The output isn’t perfect–it’s only 6-bit, although the firmware originally was 8-bit and can easily be reverted. The output filter could probably be improved (it’s currently a simple RC filter). The maximum frequency is limited by 1) the speed of the built-in PLL on the microcontroller (64MHz), which I use for the PWM clock and 2) how fast I can load the timer with the proper values for generating PWM. I have it down to 20 cycles + ISR overhead. And it updates the output 48 times per cycle maximum (and fewer at high frequencies, so the triangle and sine waves aren’t as accurate up there).

But it works pretty well. Here’s an o-scope screenshot (yes, someday I need to hook up the o-scope to the computer):

I got enough parts for several prototypes, and since it works, I’ve put together some kits in the store. You can find those here.

Also, if you’re interested in making improvements (and I’m sure there are plenty to be made), here is the source code, the schematic, and the board layout.

17 Responses to “ATTiny25-based function generator”

  1. benryanau says:

    Love it! Been looking for a decent mini sig genny for ages..
    Question: Where’s the kit? There’s no product at that link.. if the price and postage is right, I’ll buy one!

  2. admin says:

    Sorry about that–I had no idea this many people would want a kit, and I ran out of stock almost immediately. I’ve ordered a whole big batch now, and should have more stock on hand in a couple weeks.

  3. benryanau says:

    Do you think the genny could generate a sqaure ware with a very short duty cycle? Almost a pulse generator.
    A few milliseconds at most, between 1khz to 40khz.
    I’m looking to use it to drive a mosfet bank as part of a lead-acid battery desulphator.
    I’ll get one for the toolbox anyway, but I hope it’ll work for the project too!
    Cheers

  4. admin says:

    With the current code, the best you’ll get is a ~2% duty cycle, and at higher frequencies, the minimum duty cycle (as a percentage of the total waveform) will increase, since there are fewer steps per cycle at the higher frequencies.

    I’d have to do some testing, but I think the shortest pulse you’ll get out of this is 1/8 of a 40KHz waveform. That’s, uh, (ugh, it’s early, brain not working) 25uS/8 ~= 3uS. I think. So yeah, I think it’ll work if you’re looking for a pulse just a few milliseconds long.

  5. admin says:

    I just tested it, and the shortest pulse it generates is about 2.6uS, so it should be fine for your desulfator.

  6. benryanau says:

    Awesome, thanks mate. What’s the peak to peak maximum output voltage of the genny? What kind of current can it source.. what impedances can it be used with? (pardon my questions..)

  7. admin says:

    Max peak-to-peak voltage is Vcc. Maximum output current for the sine and triangle saves is that of the MCP6002, and depends on supply voltage. For Vcc=5.5V, it’s 23mA. For the square wave, the maximum current is the same as the ATTiny’s max, which I believe is 40mA.

    As for impedances, you’re pretty much good with whatever. This isn’t at a high enough frequency to worry about wave propagation down a wire.

  8. benryanau says:

    Appreciate the reply. Looks great, I’ll pop an order in this weekend.. once again, top effort on the genny 🙂

  9. benryanau says:

    Oh, quick one.. what I forgot to check was.. is the output current-limited? eg will a dead short cause any damage. Sorry for all the questions 🙂

  10. admin says:

    The sine wave and triangle wave outputs should be tolerant of a continuous short, but not the square wave output.

  11. benryanau says:

    Ah. What about a brief short on the square wave? That’s the one I’ll be using. And will it pop the micro?

  12. admin says:

    The square wave output is connected directly from the microcontroller to the terminal, so you can definitely damage the chip. If you’re worried about it, just stick a resistor in line.

  13. benryanau says:

    Enough questions 🙂

  14. blakjakd says:

    The schematic is different than the picture. Did you reduce the potentiometers from 5 to 2, and the picture is just old?
    Just making sure the board layout and schematic will work if I build this.

  15. admin says:

    Good catch! The schematic that was linked was the first version, which didn’t have a power LED or amplitude control, and indeed only had two pots (for frequency and duty cycle control).

    I’ll post the right schematic as soon as I find the file.

  16. blakjakd says:

    Thank you, would hate to use an outdated version!
    -John

  17. admin says:

    Thanks for the reminder. I’ve updated the links to point at the correct files.