2

I have a multiple choice question game, and I'm wondering how to hook up a piezo buzzer to make a sound when someone puts in an answer.

Basically, I need two functions - a sound for when they get it right, and a sound for when they get it wrong.

I have been unable to find how you set the frequency of a buzzer using GPIO pins.

I tried GPIOzero, but it appears that there aren't any ways to set the frequency. (Please tell me if there are)

I have now tried RPi.GPIO, but my code isn't working - no noise is being made.

I'm sorry as I'm very new to this - and I don't really have any clue what I'm doing.

Here's my current code:

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
GPIO.setup(26, GPIO.OUT)

p = GPIO.PWM(26, 100)

def Blink():
    for x in range(0,20):
        GPIO.output(26, True)
        p.start(100)
        p.ChangeDutyCycle(90)
        p.ChangeFrequency(400)
        sleep(2)
        p.stop()
    GPIO.cleanup()

Blink()
  • Why do you think you can drive the buzzer from the GPIO. Perhaps you can, perhaps you can't, it will depend on the buzzer. – joan Feb 5 '17 at 20:07
  • Again, I don't know what I'm doing - how would I hook up a buzzer to the Pi? – Nytron Feb 5 '17 at 20:10
  • 2
    I was trying to hint we need to know what buzzer you have. – joan Feb 5 '17 at 20:15
  • Yes, you might have the wrong buzzer. There's active and passive ones. Only one type will work, I think it's the active kind. – recantha Feb 5 '17 at 22:07
  • I'm using an active buzzer. (I have both available) – Nytron Feb 6 '17 at 2:26
0

So there are two types of buzzers, active and passive. Passive buzzers are the ones on which you can control the frequencies, thus making it ideal for coding songs. An active buzzer has only one frequency, and normally has a water proof coating at the bottom. When you want to make different sounds I would recommend using a passive buzzer. It doesn't exactly matter what GPIO pin you plug it into to control the frequencies. In order to change the frequencies, you would have to change your code. The following is code for something that sounds like an alarm. The frequencies are changing as the program runs thus allowing it to make a siren-like sound. I hope this helps! Also in your code, you need to define which pin the buzzer is in (the GPIO pin, if you were to google a numbered version it should let you know which pin to write ex. buzzerPin = "insert pin number here") You can refer to the code I put below along with the circuit picture... Type up the code again (Python) and see what happens, and then play around with it. Good luck! (I am also a beginner but got the hang of it pretty quick...)

enter image description here enter image description here enter image description here

  • 2
    Don't put code in images. Put code in code blocks as that allows the person who wrote the question to cut'n'paste the answer. – Dougie Aug 2 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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