Rage Against The AVR ->(Stereo PWM Audio)

Can PWM from a simple micro-controller duplicate complex sounds? (Rage Against The Machine)


Using a pwm channel of a simple microcontroller, a capacitor and a resistor to duplicate sounds on the fly.

Wouldnt it be sweet if you could have your micro play-back complex things, over audio for an extremely low cost? Imagine replacing that one i/o line that used to drive an led, be able to reproduce the human voice and exclaim "Hello world",

This can be useful in projects where you would like to add some human interface, but an lcd would be too bulky / to slow.

if you dont know what pwm is check out this link, and lo, know you know.

Part 1: Getting The Goods
Part Site Cost
AVR Mega series microcontroler (i used the m48) digikey, mouser, futurelec, samples $0.00 -3.00
A computer with a relativley fast serial port
(usb to serial works) $0.00-15.00
Cheap Audio Amp
Stereo with analog input $0-15
Software tools:
Program Name Link Operating System Compatibility
Switch http://www.nch.com.au/switch/ Windows, mac, linux
Python (pyserial) http://pyserial.sourceforge.net/ everything under the sun
Bascom avr http://www.mcselec.com/ Windows, mac, linux

Part 2: Creating a sample waveform.
StepDescriptorImage / Media
0: Sample Video:
[Hosted by big brother]
The sound output is actually more interesting than the way it sounds on google video. (i will host an uncompressed video file eventiually) (done)
1:  Visit http://www.research.att.com/~ttsweb/tts/demo.php Use the provided page  to convert text to speech.  save / download the file for later.  Here's a link to a simple sound file in wav format. it declares "hello, my name is mike. I am a micro controller. Using just 2 pins i can create stereo sound. Talk is cheap .
note, i used the mike audio format. Mike is a male voice, and should have a lower frequency range, which is easier to reproduce on an 8khz system.
2: next up, i downloaded the wav. and opened up switch. set the output to .raw and select advanced options at the bottom. select 8 bit unsigned (this means the samples are converted to numbers 0-255). also set the sample rate to 8khz (this means, its only samples 8000 times per second, cd quality is like 44khz). if your micro has 2 pwm channels, use stereo. else use mono.
Output your file. rename it as a text file. (.txt) in the following frame is mike.txt. note that these values are in binary, not ascii. you wont see 0, 10, 12, 14 you will see a mishmosh of firefox /IE  trying to interpret it as unicode.  Link to mike.txt
4: crank up python. make sure youve got pyserial installed.
5. write the following to parse the data and spit it out over the serial port: (python)
 as allways, set your com port right. some hardware doesnt support funky baud rates like 161828 baud
import serial
import sys
s = serial.Serial("COM3", 161828)
f = open(sys.argv[1], "rb");
s.write(f.read( ));
(attached link)
6. Create the following circuit. this isnt everything, but its the basics for the headphone system. as shown, it uses just 2 pwm outputs and 2 rc filter's set at the 8khz breakpoint. (1/2pi *r*c) = 8000
7. bascom file (firmware for the avr).
this was compiled in bascom avr, version 1.11.87
compile this and program it into your avr.

link if youve never used bascom and a sample electronics programmer
$regfile = "m48def.dat"
$crystal = 8000000
$baud = 161828
Dim Var1 As Byte
Dim Var2 As Byte
Config Timer1 = Counter , Edge = Falling , Capture Edge = Falling , Prescale = 256
    Input Var1
    Input Var2
    Pwm1a = Var1
    Pwm1b = Var2
Goto Main
8. Sample Audio (recorded from system into mic input)
9. How it works (to be written up)
Transfering the stereo file 161828 baud equates to about 64 kbps of actual audio (start and stop bits non-included). the rc filter is set at a breakpoint of about 8khz. to calculate the values to use, use 1/(2pi r*c)=8000 also the decoupling caps used were pretty big (47uf). you kinda need them so there is no dc bias for the input to your amplifier. because this is stereo, i used 2 rc filters with the same characteristics.
10. How well it works

(second higher quality video hosted here)
Here is an excerpt of rage against the machine, sampled at 8khz, and converted into 8bit audio.
As you can see, although the sound is definatley recognizable, its not mp3 quality by any means. fortunatley, it doesnt need to be. From a young age, moden people have been 'decoding' speech from garbled audio (and you thought your bad cell reception was useless). This allows 8khz audio to work a lot better than expected.
Example Video #2 
Hosted by big brother
11. What else is this useful for? Say you used flash memory, or an sd card. you could store plenty of pre-recorded responses. These could do anything from tell you that your engine temperature is too hot (for a microcontroller embedded in a car) or simply if you want to make your own pre-recorded holiday card



The wittness camera, http://www.riccibitti.com/witnesscam/abstract.htm
Bascom avr manual, http://www.mcselec.com/
Andrew Armenia, http://www.asquaredlabs.com/

Rensselaer Polytechnic Institute 
Electrical & Electrical Power