Page 1 of 2

Making sounds

Posted: Sat Jun 18, 2016 10:50 am
by deshipu
A piezo speaker is a cheap and popular peripheral for teaching beginning electronics. It's fun to have your microcontroller board beep, then play simple songs, then act as an instrument. It's also nice for improving feedback on user interface in more advanced projects.

I'm preparing a workshop now, and one thing I wanted to include was playing simple songs. I thought that I can use the PWM object for this -- after all you can specify frequency in it. Turns out that the resolution of that frequency at acoustic range is very bad -- so bad, in fact, that most notes on a scale get rounded to the same frequency.

Now I'm wondering if there is some other way to play that song. Bit-banging is of course one option, and shouldn't be problematic with a 80Mhz CPU. Timers is another option. Any other ideas?

Re: Making sounds

Posted: Sat Jun 18, 2016 11:22 am
by yeti
Maybe abusing SPI or I2C clock yields a better frequency range?

Re: Making sounds

Posted: Mon Jun 20, 2016 1:58 pm
by yeti
Is your piezo speaker really just a speaker or does it contain an own oscillator and will beep on its own when powered?

Re: Making sounds

Posted: Wed Jun 22, 2016 7:35 am
by Peter.Kenyon
I hooked up the ghi Tunes module to the pyboard pwm and it seems to be fine,
Here is some code (in C#) that parse the rttl format of which there are many songs available
https://www.ghielectronics.com/communit ... /entry/839

Re: Making sounds

Posted: Wed Jun 22, 2016 9:23 am
by deshipu
PyBoard's PWM doesn't seem to have that 1000Hz limit.

Re: Making sounds

Posted: Wed Jun 22, 2016 7:45 pm
by Photon Peddler
If there was an I2S module for Micro Python, you could play short samples from RAM, similar to what this project does: https://github.com/espressif/ESP8266_MP3_DECODER

It re-purposes the I2S data output as a 5-bit PWM output, on pin GPIO3/RX0, or uses a real I2S DAC.

Re: Making sounds

Posted: Wed Jun 22, 2016 10:10 pm
by deshipu
Nice! Thanks, I will look into that, maybe it's possible to make a micropython module out of it.

Re: Making sounds

Posted: Mon Dec 05, 2016 8:20 pm
by jamesb
Given the PWM output max freq of 1000Hz, with duty cycle down below 1%, just wondering if there is any way to get a square wave of higher frequency. Should be possible to get 50% at 100000Hz I would've thought, but I don't know if there are any hooks in the OS to do this?

Re: Making sounds

Posted: Tue Dec 06, 2016 9:01 am
by pythoncoder
@jamesb Pins can be toggled directly - there was something of a contest here http://forum.micropython.org/viewtopic.php?f=2&t=1349 to see how fast a Pyboard pin can be toggled. They got well beyond the audio range even before invoking "advanced" techniques.

Re: Making sounds

Posted: Tue Dec 06, 2016 11:28 pm
by dhylands