Making sounds

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
jamesb
Posts: 13
Joined: Tue Nov 29, 2016 3:31 am

Re: Making sounds

Post by jamesb » Mon Dec 12, 2016 12:31 am

Thanks @dhylands and @pythoncoder. I ended up using the hardware SPI, the clock output was good enough for what I needed.

User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: Making sounds

Post by ernitron » Tue Dec 13, 2016 6:36 pm

dhylands wrote:You may want to checkout: http://forum.micropython.org/viewtopic. ... 72&p=12291
I just translated into esp/machine language your pyb_test. The rest is pretty portable...
It's a pity the 1000Hz limit of the PWM on ESP.
Besides I hope I've made it right. (The only audible song is TopGun Theme but it was fun!)

Code: Select all

# Port for ESP8266
from rtttl import RTTTL
import songs
import time

from machine import Pin, PWM

buz_tim = PWM(Pin(5))

def play_tone(freq, msec):
    print('freq = {:6.1f} msec = {:6.1f}'.format(freq, msec))
    if freq > 0:
        buz_tim.freq(int(freq))
        buz_tim.duty(50)
    time.sleep_ms(int(msec * 0.9))
    buz_tim.duty(0)
    time.sleep_ms(int(msec * 0.1))

def play_song(s):
    tune = RTTTL(songs.find(s))
    for freq, msec in tune.notes():
        play_tone(freq, msec)

allsongs = [ 'Super Mario - Main Theme', 'SMBtheme', 'SMBwater', 'SMBunderground', 'Picaxe', 'The Simpsons', 'Indiana', 'TakeOnMe', 'Entertainer', 'Muppets', 'Xfiles', 'Looney', '20thCenFox', 'Bond', 'MASH', 'StarWars', 'GoodBad', 'TopGun', 'A-Team', 'Flinstones', 'Jeopardy', 'Gadget', 'Smurfs', 'MahnaMahna', 'LeisureSuit', 'MissionImp', ]

for s in allsongs:
    print('Playing ', s)
    play_song(s)


User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: Making sounds

Post by ernitron » Fri Dec 16, 2016 11:45 am

Following this and other discussions that show that a higher frequency can be achieved in toggling pin, I was just wondering why there is the 1khz limit on PWM for ESP8266. It is only the current implementation or there are other issues?
Thanks

Post Reply