Skip to content

Buzzer Wireling Python Tutorial

buzzer product picture

This Wireling uses the AST7525MATRQ buzzer to give you the ability to add a variety of tones to your next project!

Technical Details

 AST7527MATRQ Specs

  • Oscillation frequency 2700Hz
  • Sounds pressure level 85 dB/min at 10cm

    Power Requirements

    • Voltage: 3.0V - 5.5V 
    • Current:  100mA (Max Rating).

    Pins Used

    • A5/SCL - I²C Serial Clock line
    • A4/SDA - I²C Serial Data line 

    Dimensions

    • 10mm x 10mm (.394 inches x .394 inches)
    • Max Height (from the lower bottom of Wireling to upper top Wireling Connector): 6.25mm (0.25inches)
    • Weight: 1 gram (.04 ounces)

    Materials

    Hardware

    Required Software

    • Python 3 (Python 2 is not supported!)
    • All Python packages mentioned in the Pi Hat setup tutorial (tinycircuits_wireling, Adafruit-Blinka, adafruit-circuitpython-ads1x15, and adafruit-circuitpython-busdevice)
    • Buzzer Python Example

    Hardware Assembly

    Plug your Wireling into the port you plan on using! The default in the included program is port 0.

    If you want to use a different port, you just need to change the port value in the program mentioned later.


    Programming

    For this program there is no python package necessary that is specific to the Buzzer Wireling. Instead, there is an example that includes a variety of options/sounds to get an idea of how you can program the noise-maker.

    buzzer_wireling.py
    # TinyCircuits Buzzer Wireling Example Program
    # This example was edited to be compatible with TinyCircuits hardware
    # Original Source by Dipto Pratyaksa: http://www.linuxcircle.com/2015/04/12/how-to-play-piezo-buzzer-tunes-on-raspberry-pi-gpio-with-pwm/
    # Modified by: Laveréna Wienclaw for TinyCircuits
    # Last updated: Jan 7, 2020
    
    
    import RPi.GPIO as GPIO      # import the GPIO library
    import time                  # import the time library
    import tinycircuits_wireling # import wireling library 
    
    wireling = tinycircuits_wireling.Wireling()
    
    class Buzzer(object):
     def __init__(self):
      self.buzzer_pin = wireling.getPin(0) # set to match port(0-3)
      GPIO.setup(self.buzzer_pin, GPIO.IN)
      GPIO.setup(self.buzzer_pin, GPIO.OUT)
      print("buzzer ready")
    
     def __del__(self):
      class_name = self.__class__.__name__
      print (class_name, "finished")
    
     def buzz(self,pitch, duration):   # create the function and feed it the pitch and duration)
    
      if(pitch==0):
       time.sleep(duration)
       return
      period = 1.0 / pitch     # in physics, the period (sec/cyc) is the inverse of the frequency (cyc/sec)
      delay = period / 2       # calcuate the time for half of the wave
      cycles = int(duration * pitch)   # the number of waves to produce is the duration times the frequency
    
      for i in range(cycles):          # start a loop from 0 to the variable calculated above
       GPIO.output(self.buzzer_pin, True)   # set pin high
       time.sleep(delay)                    # wait with pin high
       GPIO.output(self.buzzer_pin, False)  # set pin low
       time.sleep(delay)                    # wait with pin low
    
     def play(self, tune):
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(self.buzzer_pin, GPIO.OUT)
      x=0
    
      print("Playing tune ",tune)
      if(tune==1):
        pitches=[262,294,330,349,392,440,494,523, 587, 659,698,784,880,988,1047]
        duration=0.1
        for p in pitches:
          self.buzz(p, duration)    # feed the pitch and duration to the function
          time.sleep(duration *0.5)
        for p in reversed(pitches):
          self.buzz(p, duration)
          time.sleep(duration *0.5)
    
      elif(tune==2):
        pitches=[262,330,392,523,1047]
        duration=[0.2,0.2,0.2,0.2,0.2,0,5]
        for p in pitches:
          self.buzz(p, duration[x]) 
          time.sleep(duration[x] *0.5)
          x+=1
      elif(tune==3):
        pitches=[392,294,0,392,294,0,392,0,392,392,392,0,1047,262]
        duration=[0.2,0.2,0.2,0.2,0.2,0.2,0.1,0.1,0.1,0.1,0.1,0.1,0.8,0.4]
        for p in pitches:
          self.buzz(p, duration[x])  
          time.sleep(duration[x] *0.5)
          x+=1
    
      elif(tune==4):
        pitches=[1047, 988,659]
        duration=[0.1,0.1,0.2]
        for p in pitches:
          self.buzz(p, duration[x])  
          time.sleep(duration[x] *0.5)
          x+=1
    
      elif(tune==5):
        pitches=[1047, 988,523]
        duration=[0.1,0.1,0.2]
        for p in pitches:
          self.buzz(p, duration[x])  
          time.sleep(duration[x] *0.5)
          x+=1
    
      GPIO.setup(self.buzzer_pin, GPIO.IN)
    
    if __name__ == "__main__":
      a = input("Enter Tune number 1-5:")
      buzzer = Buzzer()
      buzzer.play(int(a))
      GPIO.cleanup() # Resets any ports used in current program
    

    Run Program

    Navigate in the terminal to the directory with the file and type:

    python3 buzzer_wireling.py
    

    Downloads


    Contact Us

    If you have any questions or feedback, feel free to email us or make a post on our forum. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.

    Thanks for making with us!