ja.heller
Posts: 33
Joined: Thu May 01, 2014 1:52 pm

using beeper via GPIO and python

Postby ja.heller » Mon May 12, 2014 4:49 pm

Heyho guys;

I want to use my old beeper from my motherboard.
As I know it needs to get power via PWM.
I used this snippet of code:

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(25, GPIO.OUT)
GPIO.output(25, True)
time.sleep(10)
GPIO.cleanup()


Result --> speaker "clicks" (very silent)
Now I tried this python code for using PWM:

Code: Select all

import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
p = GPIO.PWM(25, 50)   
p.start(50)       
GPIO.cleanup()

Result --> nothing happens, there isn't any sound.
Should I change the Hertz value or should I change the percent duty cycle?
If yes to which amount should I change?

Kind regards;
scotty101
Posts: 2185
Joined: Fri Jun 08, 2012 6:03 pm

Re: using beeper via GPIO and python

Postby scotty101 » Mon May 12, 2014 7:43 pm

You should probably change the frequency.

50Hz isn't nearly high enough to produce an audible sound. Middle C is 261 Hz. Try a frequency in that range and report back.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter
User avatar
jojopi
Posts: 2961
Joined: Tue Oct 11, 2011 8:38 pm

Re: using beeper via GPIO and python

Postby jojopi » Mon May 12, 2014 8:12 pm

50Hz is a low but perfectly audible buzz.

The problem with the code is the lack of time.sleep() before GPIO.cleanup(), which means the note has zero length.
ja.heller
Posts: 33
Joined: Thu May 01, 2014 1:52 pm

Re: using beeper via GPIO and python

Postby ja.heller » Tue May 13, 2014 11:11 am

I've tried:

Code: Select all

import RPi.GPIO as GPIO
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
p = GPIO.PWM(25, 261)   
p.start(50)       
GPIO.cleanup()

result --> no sound output ;(.
GPIO Pins are the right ones. As I told you the first code (see above) without using PWM triggers a very silent "click" sound.

All the best;

Kind regards;
kbfirebreather
Posts: 67
Joined: Tue Nov 26, 2013 3:52 pm

Re: using beeper via GPIO and python

Postby kbfirebreather » Tue May 13, 2014 11:41 am

Code: Select all

...
p.start(50)
time.sleep(10)
GPIO.cleanup()


GPIO.cleanup() resets all the GPIO pins. Without the time.sleep() in there, it starts the frequency, then immediately resets the gpio pin. The sleep should give you a sound for 10 seconds.
ja.heller
Posts: 33
Joined: Thu May 01, 2014 1:52 pm

Re: using beeper via GPIO and python

Postby ja.heller » Tue May 13, 2014 11:48 am

Thanks a lot,
Now it works;

Code: Select all

import RPi.GPIO as GPIO
import time
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
p = GPIO.PWM(25, 261)   
p.start(50)
time.sleep(10)       
GPIO.cleanup()


Any idea how to increase the volume? It's not as much loud as I expected it to be.

All the best;
User avatar
GTR2Fan
Posts: 1601
Joined: Sun Feb 23, 2014 9:20 pm
Location: South East UK

Re: using beeper via GPIO and python

Postby GTR2Fan » Tue May 13, 2014 11:51 am

You might want to check that it's actually a high impedance piezo speaker. If it's a typical 'old school' PC beep loudspeaker then the impedance is way too low to drive directly from a GPIO pin without risking permanent damage to the Pi.
Pi2B Mini-PC/Media Centre: ARM=1GHz (+3), Core=500MHz, v3d=500MHz, h264=333MHz, RAM=DDR2-1200 (+6/+4/+4+schmoo). Sandisk Ultra HC-I 32GB microSD card on '50=100' OCed slot (42MB/s read) running Raspbian/KODI16, Seagate 3.5" 1.5TB HDD mass storage.