Best way to create sine wave

Hi team!

I am trying to create a 325Hz 2V peak to peak sine wave to drive an 8 ohm speaker. My photon is currently doing quite a bit of signal acquisition and managing wifi and bluetooth communications so I really want to reduce processing load as much as possible. I have considered the option of connecting the speaker to the the DAC pin through a audio power amplifier but I am worried that trying to create a continuous 325Hz sine wave will require many interrupt calls in the code to continuously set the new analog write value.

Any ideas on how this can be done more efficiently with minimal load on the photon itself?

Yes, depending on how smooth you want the wave, it could require a lot of interrupts. If you had 100 steps, that would be 32,500 interrupts per second, or 30 microsecond period, which is a lot but possible with a hardware timer. You can go down to about 10 microseconds between interrupts, so you could probably go up to 300 steps, at the expense of additional overhead.

However I would go in a completely different direction and use something like this. It’s US$ 2.85 (including shipping) and can play audio files completely on its own. It has a 24-bit DAC and a built-in audio amplifier. A bunch of us in the forum have used this board and it works great!

1 Like

Doesn’t the DAC on this part have DMA?

I don’t have the datasheet where I am right now, and don’t have the time to go get it.

Yes, I forgot that the STM32Fxxx supports DMA on the DAC. I’ll take a look at that later and see if it’s possible.

So I went and pulled the RM for the part from my home machine.

The DMA looks like it’s externally triggered only, so that somehow a signal would have to be routed to the DAC/DMA controller to indicate a new sample should be loaded. I can see how this could be done if you had full access to the chip, but I didn’t look much further and I don’t know if the photon module supports such connections.

It will be interesting to see how this one works out.


The DAC DMA can be triggered via a timer: it looks like timers 6, 8, 7, 5, 2 and 4 can be used for a trigger, but the data manual for the STM32F205xx is a little unclear. It states “A DAC DMA request is generated when an external trigger (but not a software trigger) occurs while the DMAENx bit is set.”

So it looks like if you set the timer to tick off at the proper rate for the number of samples you have in your buffer to get your 325Hz sine wave, you should be OK. You just want enough time resolution in your samples to give you smooth sound.

The document I pulled this from is “en.CD00225773.pdf” which I got from ST’s web site. (I don’t recall if Particle has published this.)


Also, to sdinnu, what time resolution do you want on this?

325Hz is a period of a little over 3msec, so you have to decide how many samples (steps as rickkas7 mentioned) you want so you can decide how much buffer space you need.

Just something for you to think about.

Time resolution is a huge factor in terms of the load on the Photon. I honestly dont know what time resolution I need since the speaker only mentions that it needs a 325Hz 2Vpp sine wave to drive it (the speaker emits a specific noise with that drive signal that I need to meet). I definitely have hesitations of putting the processing load of having that many interrupts fire on the Photon since its already doing a ton of time specific signal acquisition.

If DMA on the DAC pin could would, that would reduce the processor load but I would still need the memory to store the sine wave in high samples so it can be pulled from DMA correct?

Would it be smarter for me to create a sine wave oscillator circuit that I can drive just through a digital pin? I only need to make this one sound signal. Anyone have experience here?

Not sure what you mean about the load on the photon. If you do it in DMA, you point the DMA at the location you stored the DAC values, tie it to a timer that runs at the desired rate (based on your 325Hz and how fine the time resolution is your data represents). The load on the photon doesn’t have to be much.

Yes, for DMA, you would have to store the samples in an addressable location. Presumably that could be on-chip flash or you could generate it at startup in RAM.

I wouldn’t think an oscillating circuit would be hard to do, but I am not an analog engineer.

I found this really quickly: