Making sounds

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Making sounds

Post by deshipu » Sat Jun 18, 2016 10:50 am

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?

User avatar
yeti
Posts: 11
Joined: Fri Jul 03, 2015 1:10 am

Re: Making sounds

Post by yeti » Sat Jun 18, 2016 11:22 am

Maybe abusing SPI or I2C clock yields a better frequency range?

User avatar
yeti
Posts: 11
Joined: Fri Jul 03, 2015 1:10 am

Re: Making sounds

Post by yeti » Mon Jun 20, 2016 1:58 pm

Is your piezo speaker really just a speaker or does it contain an own oscillator and will beep on its own when powered?

Peter.Kenyon
Posts: 70
Joined: Wed Oct 14, 2015 5:07 pm

Re: Making sounds

Post by Peter.Kenyon » Wed Jun 22, 2016 7:35 am

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

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Making sounds

Post by deshipu » Wed Jun 22, 2016 9:23 am

PyBoard's PWM doesn't seem to have that 1000Hz limit.

Photon Peddler
Posts: 7
Joined: Tue Jul 08, 2014 5:30 pm
Location: Connecticut, USA

Re: Making sounds

Post by Photon Peddler » Wed Jun 22, 2016 7:45 pm

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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Making sounds

Post by deshipu » Wed Jun 22, 2016 10:10 pm

Nice! Thanks, I will look into that, maybe it's possible to make a micropython module out of it.

jamesb
Posts: 13
Joined: Tue Nov 29, 2016 3:31 am

Re: Making sounds

Post by jamesb » Mon Dec 05, 2016 8:20 pm

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?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Making sounds

Post by pythoncoder » Tue Dec 06, 2016 9:01 am

@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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Making sounds

Post by dhylands » Tue Dec 06, 2016 11:28 pm


Post Reply