Generating sound with a PIC microcontroller

Started by matlevo12, April 09, 2022, 12:57:43 PM

Previous topic - Next topic


Hi everyone.
A few years ago I modified a PIC code (that was used in a relay bypass, I modded it to be temporary/permanent depending on the amount of time the switch was pressed), and I thought I could use the little amount of knowledge from this experience to try and use a PIC in another project.

I'd like to generate sound using a pic. The sound doesn't have to be a nice synth, just a low or high pitch drone, intermittent or not, that would be added to a guitar pedal output (hence doesn't have to be amplified by itself).
By reading a bt I discovered that PWM might be a solution for me... but I seem to lack the basic knowledge and vocabulary to understand the articles and and other results of my online research.
Like pedal building/modding/repairing, I learned most of what I know by doing and failing, the step is just too high for me right now.

Would any of you awesome people know a bit more about this subject, could help me recommend tutorials or websites ?

Thanks a lot !

(I'm using a 12f675 pic with a pickkit 3 interface, using C language, btw)


PWM just means you're going to output alternating high and low values at a certain frequency. If you do it at a frequency in the hearing range it'll sound like a tone.

This video seems like a decent intro (after the first minute):

So the simplest way to get started is use a PWM pin on your uC and hook it up to a speaker (Edit: e.g. one of those little piezo speakers).


12F675 is a small chip, so you don't have a lot of space (Program Flash or RAM) so your options are fairly limited, but it's possible.

I used the 16F1847 for my voltage-controlled digital oscillator:

The codes is all assembly, so that won't help you much, but I'd hope you might find it inspiring - it *is* possible to use a PIC to generate sound, and even to do some processing with it. Have a listen to the sound samples, if nothing else.

The chip uses a 8-bit PWM output at (iirc) 125KHz as a basic audio DAC output. The PWM output needs filtering outside the chip to turn the PWM signal back into audio, but it works. Rather than use the PWM in this way, I'd recommend using a chip with the NCO module. This works in a similar way, but can be set up to produce a pulse-density-modulation (PDM) output, which has a higher frequency in general and moves the worst-case errors to the extreme values. This offers improvements in audio quality and/or simpler filtering off the chip. Another option is something like the 16F1765 which has a 10-bit DAC on the chip which can generate audio directly, with basically no filtering required.

If you come back with some specific questions, I might be able to answer them. I don't know of anywhere where you can really read about or learn this stuff - it's a bit too niche for that! No-one except for crazies like us thinks that PICs are worthwhile for audio generation!



Thanks a lot guys, that's really helpful.

I'll come back with more questions soon :)


For drones, you might consider a basic modulated PWM pulse digital output instead of attempting waveform generation. Each drone would be timer/counter generated with pitch set by the counter top value and pulse width varied by an LFO changing the output compare value. The LFO can be a simple up/down ramp value from another counter.
The raw PWM tone can have external bandpass filtering to make it more musical mixed into a guitar amp (100Hz-5Khz?)


+1 agree. Mark and Jim are right that if you can live with a simple square wave or pulse wave output, you can use the PWM module to generate it directly. Depending on the set-up, the frequency resolution can be pretty good and you can cover most of the audio range. The other advantage of this is that you don't need any kind of analog output - a simple digital IO pin is enough!


Just a quick message to say I'm not dead (on this topic at least), I've been busy fixing other stuff and... well with life, let's say, but I'll try that when I have the time. Thanks a lot, as always on this forum, for the help.