Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 9:30 am

Hi guys, i was wondering.. is there anyone who has written a code in python to sound a Buzzer?

I got an old buzzer and a piezo, i've learnt that only one GPIO is able to reproduce PWM signals, but I don't know how to make it sound. Can you please tell me how to wire it and how to config a PWM software in python?

Thank you!
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 9:51 am

The software side of PWM can be done on any channel using RPi.GPIO. Details can be found here:
http://code.google.com/p/raspberry-gpio ... i/Examples

This will work on any channel on the RPi.

Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 10:54 am

Using this code

Code: Select all

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)

p = GPIO.PWM(12, 50)  # channel=12 frequency=50Hz
p.start(0)
try:
    while 1:
        for dc in range(0, 101, 5):
            p.ChangeDutyCycle(dc)
            time.sleep(0.1)
        for dc in range(100, -1, -5):
            p.ChangeDutyCycle(dc)
            time.sleep(0.1)
except KeyboardInterrupt:
    pass
p.stop()
GPIO.cleanup()
I receive an error:

Code: Select all

pi@raspberrypi ~ $ sudo python beep.py
Traceback (most recent call last):
  File "beep.py", line 6, in <module>
    p = GPIO.PWM(21, 1000)# channel=12 frequency=50Hz
AttributeError: 'module' object has no attribute 'PWM'
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 11:03 am

Make sure you are using the latest version of RPi.GPIO. To check, look at the value of GPIO.VERSION in Python after you have imported the module. It needs to be 0.5.2a or greater for PWM support.

To upgrade:

Code: Select all

$ sudo apt-get update
$ sudo apt-get dist-upgrade

keybeeper
Posts: 26
Joined: Wed Aug 08, 2012 10:15 am

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 11:13 am

Transposed digits 12 to 21 ??
No Microsoft products were used in the creation of this message

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 11:27 am

I could be wrong, terminology is often used loosely, but a buzzer doesn't need any complex software. If you turn it on it will make a noise.

A piezzo on the other hand requires a frequency output, not a PWM output. A PWM output will let it make a noise, but no amount of fiddling with the duty cycle will have much effect and it is unnecessary complexity. Added to which, piezzos are generally resonant at around 2kHz, so you're unlikely to get much out of them at 50Hz.

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 11:41 am

rurwin wrote:I could be wrong, terminology is often used loosely, but a buzzer doesn't need any complex software. If you turn it on it will make a noise.

A piezzo on the other hand requires a frequency output, not a PWM output. A PWM output will let it make a noise, but no amount of fiddling with the duty cycle will have much effect and it is unnecessary complexity. Added to which, piezzos are generally resonant at around 2kHz, so you're unlikely to get much out of them at 50Hz.
You can use ChangeFrequency() instead of ChangeDutyCycle(). I would suggest a constant duty cycle of 50% in this case.
You will need to adjust the values for range() with suitable frequency values - have fun experimentng! Go too high and it might upset the pet dog...

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 11:52 am

A silly idea - you could potentially get the piezzo buzzer to play a tune if you get carried away!

Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 1:09 pm

When i write Sudo apt-get install python3-3RPi.GPIO

it installs gpio_0.3.1a-1_armel.deb, Why?

I've already done:

Code: Select all

$ sudo apt-get update
$ sudo apt-get dist-upgrade
EDIT: Solved, i downloaded and installed manually the package
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

User avatar
croston
Posts: 703
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: PWM Buzzer with GPIO in Python

Sun Apr 14, 2013 1:20 pm

It looks like you are using softfloat Wheezy (armel) rather than Raspbian (armhf). The packages in softfloat Wheezy are not as up to date for RPi as with hardfloat Raspbian. You will have to either change to Raspbian (recommended) or download, build and install RPi.GPIO from source.

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: PWM Buzzer with GPIO in Python

Thu May 14, 2015 4:10 am

A simple python 3 code to play buzzer tunes can be found here:
http://www.linuxcircle.com/2015/04/12/h ... -with-pwm/

Try if you could transcribe Mario Bro theme song :D
www.linuxcircle.com

Return to “General discussion”