0

The wiring seemed pretty trivial as did the required code.

I have the following buzzer module: http://www.ebay.com/itm/KY-006-Passive-Buzzer-Module-Arduino-AVR-PIC-/370886693686?pt=LH_DefaultDomain_0&hash=item565a91d336

I'm trying to produce any sound at all but so far it's not going all that great. Here is my wire setup:

RPi            |    Buzzer
----------------------------------
GND (Pin 06)   | GND (-) (First Pin)
5V (Pin 04)    | VCC (Middle Pin)
BCM18 (Pin 18) | S (Last Pin - Marked S)

And my code:

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)


try:
  while True:
    GPIO.output(18, True)
    time.sleep(1/5)
    GPIO.output(18, False)
except KeyboardInterrupt:
  GPIO.cleanup()

However I don't get even a beep out of it. I have 7 of these modules that I got from my class mate they used them in class. What am I missing here. I also tried using GPIO.BOARD and GPIO Pin 14 but still nothing no sound/beep.

2 Answers 2

3

Couldn't find very much on this item, however according to Amazon.com, the unit doesn't have an internal oscillator.

Amazon : KY-006 buzzer : "No oscillation source,need square wave(frequency 2K-5K ) to drive Audion 9012 drive; Work Voltage: 3.3-5V"

You would need to put out a square wave (between 2kHz and 5kHz) on your output pin.

0

You can generate a square wave on GPIO pin.

def beep(repeat):
   for i in range(0, repeat):
      for pulse in range(60): # square wave loop
         GPIO.output(18, True)
         time.sleep(0.001)     # high for .001 sec
         GPIO.output(18, False)      
         time.sleep(0.001)     # low for .001 sec
      time.sleep(0.02)        # add a pause between each cycle
beep(4)

Note that linux is NOT a realtime operating system, so you can't expect the sleep time to be exactly 0.001 seconds. It will not be accurate, because of which you'll NOT get a steady tune, instead the tune will fluctuate in frequency. If you try to do same in microcontroller (e.g. arduino), the frequency will not fluctuate. So to conclude, you cannot make beautiful melodies, but you can use this snippet to get a beep !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.