I've found Piezo example for Rpi with specific libraries in Java and C (wiring) -- are there any for Python? I've seen PyPiezo but it seems to be for specific hardware.

1 Answer 1


Someone who knows a lot more than I may post a super example that blows this out of the water -- but basically, one can use PWM to control a Piezo on Pi, e.g.:

import RPi.GPIO as GPIO 
import time 

GPIO.setup(7, GPIO.OUT) 
GPIO.setup(15, GPIO.OUT) 

c = 261
d = 294
e = 329
f = 349
g = 392
a = 440
b = 493
C = 423
r = 1
p = GPIO.PWM(15, 100)
def Blink(numTimes, speed):
    for i in range(0,numTimes): 
        print "Iteration " + str(i+1) 
        GPIO.output(7, True) 
        GPIO.output(15, True) 
        time.sleep(speed) ## Wait
        p.start(100)             # start the PWM on 100  percent duty cycle  
        p.ChangeDutyCycle(90)   # change the duty cycle to 90%  
        p.ChangeFrequency(c)  # change the frequency to 261 Hz (floats also work)  
        time.sleep(speed) ## Wait
        p.ChangeFrequency(d)  # change the frequency to 294 Hz (floats also work)  
        time.sleep(speed) ## Wait
        time.sleep(speed) ## Wait
        time.sleep(speed) ## Wait
        time.sleep(speed) ## Wait
        time.sleep(speed) ## Wait
        time.sleep(speed) ## Wait
        time.sleep(speed) ## Wait
        time.sleep(speed) ## Wait
        p.stop()                # stop the PWM output  

    print "Done" ## When loop is complete, print "Done"

iterations = 4
speed = 2


This answer is just a conglomeration of an Arduino example and basic LED stuff.

Your Answer

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

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