analog audio in to PWM audio out

i just finished an arduino sketch that allows decent audio processing with just a few resistors and capacitors. the arduino sketch is up here:

it takes in audio on ADC0 and uses a dual pwm to create 16b audio out. you can process the audio in-between and make a low-fi effects pedal. it can be used for just analog out as well, and is much higher quality than using analogwrite (although more complicated too).

there is a much more in-depth tutorial on pwm in general up here:

Hi I have tried your code on my Arduino Mega2560, it doesn't work. Are the code for Atmega328 and Atmega2560 different? Any hint to convert the code to Arduino Mega2560?

the ports are different between the mega and uno (what pins map to what functions), so those need to be changed.

HI… (sorry for being thick headed here) :slight_smile:

but would this be used for AUDIO playback? (like a .wav file?) or just for use like, say… a voice changer/voice effects type of project?


it can output .wav, but there isnt an SD card reader setup for it yet. so if you have something that can read SD cards, you can use the PWM to output it. this is what the simpleSD does.

This is interesting. What might you use this for? Are there audio examples?


SimpleSD.. as in the SimpleSDAudio library?

(I have played with that before)

I guess Im missing a 'purpose' this can be used for then??

Was a wrong to assume it could be used for some sort of real-time voice changer/FX platform??

yes, its primarily for doing guitar/voice effects on the cheap. but, the pwm side is just a generic setup routine to configure the pwm for various data rates and bit depths, so that can be used for any audio application.

Would be nice to see a full sketch that does some audio effects.

i have a sketch up here:

sorry, no sound samples yet.

This is nice sketch.. :) but how could i do the circuit for guitar signal?

if you take a look at the circuit here:

you just need to put a guitar preamp before it. a simple opamp circuit with gain of 100 would be fine.

I have this sketch bread boarded with the analogue in circuit from instructable here.Thank you GUest, Brilliant!

All working a bit noisy but I am fine tuning resistor values and caps.

This is the project I am working on to use it with,

Just going to do a quick video and then ´´once more unto the breach´´

AdC/DaC highway to ped(Hell)

Update on the hardware side of this project. Links to all the helpfull and informative information sourced so far.

I need to address the Resistor Values and also the reference ground voltage and also one of the capacitor values in this circuit.

I am encouraged with progress to date the next stage of the project is interfacing the guitar signal with Pure Data and adding additional controls as set out in the first video so some more reading on Pduino.

These are really just bench notes again on progress to date still unresolved are the 5 way selector switch hack for the Pickup replacer selection so very much a work in progress.

hi g_u_e_s_t, is there any option to increase the output frequency further? I require PWM signals with 384kHz.

donny18: hi g_u_e_s_t, is there any option to increase the output frequency further? I require PWM signals with 384kHz.

Guest means that the original poster has deleted his account. So he will not give an answer. This post is from 2013 and you should not have resurrected it.

The PWM frequency can be increased by altering registers in the processor, but that will not help you much because the limit is on the input side.

The A/C described in Will not work like the author thinks it will. He says

And be sure to use 1% resistors for 14bits and higher PWMs.

This is total rubbish. One percent is one part in 100 and is more like the sort of accuracy you need for 7 bits. For every extra bit you need to double the accuracy of these resistors so for 14 bits your potential divider has to be made from 0.0000001% something that is impossible to get in fixed resistors.

0.01% will get you 13 bits, and that's perfectly possible to find (in a very limited range of values), but very expensive!

However the stray resistances in the circuit and output drivers for pins will tend to eat away at the accuracy anyway if expecting such accuracy

When using two resistors for the divider shown, the only accuracy requirement is relative between the two resistors, and that corresponds to 8 bits, not the full 16 of the signal. One resistor is subdividing the steps of the other one, not providing full-range accuracy.

What has to be 16 bit accurate is the PWM wave timing, so a quartz crystal is required for the system clock, not a ceramic resonator.