RTTTL - Ring Tone Text Transfer Language driving a Piezo Speaker

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
dhylands
Posts: 3247
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

RTTTL - Ring Tone Text Transfer Language driving a Piezo Speaker

Post by dhylands » Sun Jul 24, 2016 5:54 am

I've put together an RTTTL parser and a sample which drives a piezo speaker.

The RTTTL parser can be found here: https://github.com/dhylands/upy-rtttl
I put together a quick video (so you can hear it playing a tune): https://www.youtube.com/watch?v=TadV2AEvfww

The pyb_test.py file has the function which plays a single tone, and rttlt.py has the RTTTL parser.

User avatar
mcauser
Posts: 413
Joined: Mon Jun 15, 2015 8:03 am

Re: RTTTL - Ring Tone Text Transfer Language driving a Piezo Speaker

Post by mcauser » Thu May 31, 2018 12:01 am

Works great with my pyboard + Seeed Grove buzzer!
There's millions of RTTTL examples a short Google search away.

You can even play them online:
https://adamonsoon.github.io/rtttl-play/

I used this tool to help figure out the frequencies I wanted to play:
http://www.szynalski.com/tone-generator/

Trapsis
Posts: 2
Joined: Fri Jan 11, 2019 10:15 pm

Re: RTTTL - Ring Tone Text Transfer Language driving a Piezo Speaker

Post by Trapsis » Tue Feb 19, 2019 12:30 pm

Very cool! Just what I'm looking for. I'm trying to adapt your code to work on an ESP32 board using the machine.PWM library. I'm still debugging it but there is not much that needed to change. It doesn't get through a whole song before failing. Here is the code if you are curious:

from rtttl import RTTTL
import songs
import time
from machine import Pin, PWM

speaker_pin = 14 # Speaker is connected to this DIGITAL pin

# Initialize input/output pins
tone = PWM(Pin(speaker_pin, Pin.OUT), freq=0, duty=512)

def play_tone(freq, msec):
#print('freq = {:6.1f} msec = {:6.1f}'.format(freq, msec))
if freq > 0:
tone = PWM(Pin(speaker_pin, Pin.OUT), freq=int(freq), duty=512)
time.sleep(msec*0.001) # Play for a number of msec
tone.deinit() # Stop playing
time.sleep(0.05) # Delay 50 ms between notes

tune = RTTTL(songs.find('Super Mario - Main Theme'))

for freq, msec in tune.notes():
play_tone(freq, msec)

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

Re: RTTTL - Ring Tone Text Transfer Language driving a Piezo Speaker

Post by dhylands » Tue Feb 19, 2019 5:01 pm

You probably don't want this line:

Code: Select all

tone = PWM(Pin(speaker_pin, Pin.OUT), freq=int(freq), duty=512)
inside the play_tone function.

You should initialize the PWM object once (outside the function) and only change the frequency inside the play_tone function.

Trapsis
Posts: 2
Joined: Fri Jan 11, 2019 10:15 pm

Re: RTTTL - Ring Tone Text Transfer Language driving a Piezo Speaker

Post by Trapsis » Thu Feb 21, 2019 2:41 pm

Thanks for the advice. This works better and will get through the tune ok, but the timing seems off. I think it is the rests (pauses) that are not working well (sorry, code block not formatted. still noob here I guess :) )

Code: Select all

from rtttl import RTTTL
import songs
import time
from machine import Pin, PWM

speaker_pin   = 14  # Speaker is connected to this DIGITAL pin

# Initialize input/output pins
tone = PWM(Pin(speaker_pin, Pin.OUT), freq=0, duty=0)

def play_tone(freq, msec):
    print('freq = {:6.1f} msec = {:6.1f}'.format(freq, msec))
    if freq > 0:
        tone.duty(10)
        tone.freq(int(freq))
	time.sleep(msec*0.001)  # Play for a number of msec
    tone.duty(0)            # Stop playing
    time.sleep(0.01)        # Delay 50 ms between notes

tune = RTTTL(songs.find('Super Mario - Main Theme'))

for freq, msec in tune.notes():
    play_tone(freq, msec)

tone.deinit() 

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

Re: RTTTL - Ring Tone Text Transfer Language driving a Piezo Speaker

Post by dhylands » Thu Feb 21, 2019 3:19 pm

You have the time.sleep(msec*0.001) inside the if. Try unindenting that line.

Post Reply