Microcontroller digital sound

Discussion in 'Programmer's Corner' started by TwoPlusTwo, Apr 6, 2011.

  1. TwoPlusTwo

    Thread Starter Member

    Oct 14, 2010
    I'm experimenting with an atmega32 microcontroller, trying to produce sound digitally (only 5V and 0V output). So far, I'm able to adjust the frequency of the square wave up and down with a potentiometer, and I have the output hooked up to a couple of active speakers.

    I would like to start manipulating the sound some more, maybe using PWM or program some simple filters of some kind. But I'm very new to this, so I was hoping someone here could point me in the right direction.

    So basically, are there some simple things I can program the controller to do, in order to produce different sounds?

    Edit: I'm programming in C.
  2. HallMark


    Apr 3, 2011
    As per my knowledge i think normally People are using PWM or DAC to generate the Sound.
  3. guitarguy12387

    Active Member

    Apr 10, 2008
    This is kinda vague. But you might include the word "synthesizer" in your searches. You can get some goofy sounds with just different waveforms and some digital effects (think echo or modulating delay line effects)
  4. TwoPlusTwo

    Thread Starter Member

    Oct 14, 2010
    Looking around, I found this synth on youtube. The way I understand the guy's description of it, he's not using a DAC, but is still able to generate different waveforms and filters just by manipulating the pattern of highs and lows.

    Now I'm trying to figure out exactly how he does it, but I realize I might be in over my head.

    I did some more searching and found out that creating a simple DAC with an R-2R ladder is pretty straightforward. Would it be easier to generate different waveforms if I added that to my set up?
  5. Markd77

    Senior Member

    Sep 7, 2009
    His synth isn't really doing much different to yours.
    If you use a DAC the most obvious thing you can do is alter the volume.
    I read the ATMEGA32 has 16MIPS so it could be possible to create proper sine or triangle waves at audio frequencies. If you could manage it with 1000 instructions per sample then you would have a 16kHz sample rate which would be good enough for bass to midrange frequencies. (CD audio is 44.1kHz sample rate with 16 bits resolution).
    R2R is described here. I read somewhere that it is only useful up to about 8 bits because normal tolerance resistors are not accurate enough above that. However 8 bit should sound fairly OK.
  6. guitarguy12387

    Active Member

    Apr 10, 2008
    If you're going through all that trouble, why not use a proper integrated DAC.

    Just go PWM and a class D audio amp if you want higher fi.
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    There is Roman Black's one-bit method, which has an air of being too good to be true, but he writes it up with perfect confidence. Would anyone care to say whether it definitely would or wouldn't work?

  8. TwoPlusTwo

    Thread Starter Member

    Oct 14, 2010
    This looks interesting. Definitely worth a try!
  9. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    I like that. "perfect confidence" haha. ;)

    It works, but the down side of course is that it needs quite a lot of data storage for sound playback, and the sound is low quality, often described as "scratchy".

    The BTc system was designed for simplicity, to add low quality speech to a PIC for simple things like a talking remote control that says "on and "off" etc assuming the device already has a PIC my system lets you add some crude speech to it.

    If you want good quality speech you can use larger eeprom and do 8bit playback with a 8bit DAC, or buy an ISO sound playback IC although they can be expensive.

    If you want nice sounding beeps, tones, notes etc then you can generate in PWM without sound storage issues. You can also filter the PWM through an RC filter, which makes it sound nicer and gives you some control of amplitude (volume) as a short PWM width will make a low amplitude and 50/50 PWM width will produce max amplitude.

    If you want to generate sinewaves or other special waveshapes I have simple C code here; http://www.romanblack.com/one_sec.htm#BDA that lets you make sinewaves etc from a lookup table and only needs one PIC PWM output.

    Hope that helps. :)
    TwoPlusTwo likes this.