DIY – Waveform Generator using AVR Microcontroller

Summary of DIY – Waveform Generator using AVR Microcontroller


This article details the design of a waveform generator using an ATMega32 microcontroller and an 8-bit DAC0808. The system converts digital signals into analog waveforms like square, sine, triangular, staircase, and saw-tooth waves. A current-to-voltage converter utilizing an LM741 operational amplifier is employed to transform the DAC's current output into a usable voltage signal. The circuit is controlled via push buttons connected to PORTA, with digital outputs sent to PORTC.

Parts used in the Waveform Generator:

  • AVR Development Board
  • ADC – DAC card
  • Digital Storage Oscilloscope (DSO)
  • Power Supply
  • Connecting Wires
  • CRO Probes
  • General purpose PCB
  • AVR Microcontroller - ATMega32
  • 8-bit - DAC0808
  • Operational Amplifier – LM741
  • Push Button Switches
  • Resistors and Capacitors

An 8-bit DAC and a microcontroller can make square, triangle, sawtooth and staircase waves. The ATmega32 writes the sample values to a port; the DAC0808 turns them into a current; an op amp turns the current into a voltage. This page explains the circuit, the arithmetic and a compiled C program with four push buttons to select the waveform.

IN SHORT
  • PORTC drives the eight data inputs of the DAC0808, and push buttons on PA0 to PA3 pick the waveform.
  • The DAC output is a current, so an op amp (LM741 in the original circuit) converts it to a voltage.
  • Each sample is one port write plus a delay; the delay sets the frequency.
  • The circuit needs +5 V, +12 V and -12 V supplies; the compiled program is 322 bytes.
8 bit
resolution
PORTC
data to DAC
4
waveforms
322 B
compiled size
Chain: ATmega32 port C, DAC0808 eight bit, op amp current to voltage, output waveform
From digital samples to a voltage

The circuit

Part Connection
Push buttons PA0 to PA3 to ground, using the internal pull-ups
PORTC (PC0 to PC7) DAC0808 inputs A1 to A8, most significant bit on A1
DAC0808 reference 5 kilohm resistors on the reference pins, as in the original build
DAC0808 output Inverting input of the op amp, with a 5 kilohm feedback resistor
Supplies +5 V for the ATmega32 and DAC, -12 V for the DAC Vee pin, +12 V and -12 V for the op amp

The data bit order, reference pins and output polarity are details to confirm against the DAC0808 datasheet before building; we did not build the circuit.

The arithmetic

An 8-bit DAC has 256 levels, so a full-scale output of about 5 V gives steps of roughly 5 / 256 = 19.5 millivolts. In the program every sample takes about 20 microseconds (the delay) plus a little loop overhead.

Wave Samples per cycle Approximate frequency
Square 128 (64 high, 64 low) about 390 Hz
Triangle 128 (64 up, 64 down) about 390 Hz
Sawtooth 64 about 780 Hz
Staircase 5 steps of 16 delays, 80 delay times about 625 Hz

The values assume the 20 microsecond delay alone; the real frequency is a little lower because of loop overhead, so measure with an oscilloscope and adjust STEP_US.

The program

/* Waveform generator: ATmega32, DAC0808 on PORTC, four push buttons on PA0..PA3 (active low).
   PA0 square, PA1 triangle, PA2 sawtooth, PA3 staircase. Hold none to keep the last wave.
   Build: avr-gcc -Os -mmcu=atmega32 -DF_CPU=16000000UL -o wave.elf waveform_dac0808.c
   Compiled only; the circuit was not built. */
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>

#define STEP_US 20          /* time per output sample */

static void wave_square(void)
{
    PORTC = 255; for (uint8_t i = 0; i < 64; i++) _delay_us(STEP_US);
    PORTC = 0;   for (uint8_t i = 0; i < 64; i++) _delay_us(STEP_US);
}

static void wave_triangle(void)
{
    for (uint16_t v = 0; v < 255; v += 4) { PORTC = (uint8_t)v; _delay_us(STEP_US); }
    for (int16_t v = 255; v > 0; v -= 4)  { PORTC = (uint8_t)v; _delay_us(STEP_US); }
}

static void wave_sawtooth(void)
{
    for (uint16_t v = 0; v < 256; v += 4) { PORTC = (uint8_t)v; _delay_us(STEP_US); }
}

static void wave_staircase(void)
{
    for (uint8_t s = 0; s < 5; s++) {
        PORTC = (uint8_t)(s * 64 > 255 ? 255 : s * 64);
        for (uint8_t i = 0; i < 16; i++) _delay_us(STEP_US);
    }
}

int main(void)
{
    uint8_t mode = 0;
    DDRC = 0xFF;            /* DAC data lines */
    DDRA = 0x00;            /* buttons as inputs */
    PORTA = 0x0F;           /* pull-ups on PA0..PA3 */
    for (;;) {
        uint8_t k = (uint8_t)(~PINA) & 0x0F;
        if (k & 1) mode = 0; else if (k & 2) mode = 1; else if (k & 4) mode = 2; else if (k & 8) mode = 3;
        switch (mode) {
        case 0: wave_square(); break;
        case 1: wave_triangle(); break;
        case 2: wave_sawtooth(); break;
        default: wave_staircase(); break;
        }
    }
}

The loop reads the buttons and keeps the last choice if none is pressed. For a sine wave, replace the loops with a lookup table (see our 1 kHz sine source page). A plain software loop like this is easy but ties up the CPU; a timer interrupt gives a steadier frequency.

Check the supply wiring. A reversed -12 V rail can damage the DAC or the op amp; build the supplies first and test them with a meter.

Quick answers

Why not use PWM?
PWM needs a filter and gives lower resolution at high frequency; a DAC is simpler for fast edges.
Can I make a sine wave?
Yes, with a table of 8-bit sine values written to PORTC at a steady rate.
How do I raise the frequency?
Reduce STEP_US or the number of samples, accepting coarser steps.

Downloads and references

Share this project
X
LinkedIn
Reddit
WhatsApp

Quick Solutions to Questions related to Waveform Generator:

  • How are different waveforms generated?
    Different analog waveforms are generated by interfacing a DAC that converts digital inputs from the microcontroller into corresponding analog outputs.
  • What component converts digital input to analog output?
    The 8-bit DAC0808 converts the digital inputs given by the microcontroller into corresponding analog outputs.
  • Why is an Op-Amp required in this circuit?
    An Op-Amp LM741 is used as a current to voltage converter because the DAC output is a current equivalent of the digital input.
  • Which ports are used for input and output?
    PORTA is used as input and PORTC is used as output.
  • How are the push buttons connected?
    Four push button switches are connected to PORTA pins PA0 to PA3.
  • What supplies are required for the complete circuit?
    The circuit requires three different supplies: +5 V, +12 V, and -12 V.
  • How is the reference current Iref generated?
    The Vref pin is connected to Vcc through a 5 K Resistor and the –Vref pin is connected to ground through a 5 K Resistor.
  • What generates the final voltage output?
    A 5 K load Resistor connected in the output of the LM741 generates the voltage output.
  • Can this setup generate a Sine Wave?
    Yes, the project can generate Square Wave, Sine Wave, Triangular Wave, Staircase Wave, and Saw-tooth Wave.
  • What happens if the circuit needs more detail?
    For detailed information on the circuit diagram, one should click on the Circuit Diagram Tab.

About The Author

Author avatar

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Stay in the loop

New AVR projects, tutorials and schematics, every week.

Subscribe Browse projects

Scroll to Top