Making sounds
Making sounds
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?
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
Maybe abusing SPI or I2C clock yields a better frequency range?
Re: Making sounds
Is your piezo speaker really just a speaker or does it contain an own oscillator and will beep on its own when powered?
-
- Posts: 70
- Joined: Wed Oct 14, 2015 5:07 pm
Re: Making sounds
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
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
PyBoard's PWM doesn't seem to have that 1000Hz limit.
-
- Posts: 7
- Joined: Tue Jul 08, 2014 5:30 pm
- Location: Connecticut, USA
Re: Making sounds
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.
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
Nice! Thanks, I will look into that, maybe it's possible to make a micropython module out of it.
Re: Making sounds
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?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Making sounds
@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.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Making sounds
You may want to checkout: http://forum.micropython.org/viewtopic. ... 72&p=12291