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
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.setmode(GPIO.BOARD)
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
p.ChangeFrequency(e)
time.sleep(speed) ## Wait
p.ChangeFrequency(f)
time.sleep(speed) ## Wait
p.ChangeFrequency(g)
time.sleep(speed) ## Wait
p.ChangeFrequency(a)
time.sleep(speed) ## Wait
p.ChangeFrequency(b)
time.sleep(speed) ## Wait
p.ChangeFrequency(C)
time.sleep(speed) ## Wait
p.ChangeFrequency(r)
time.sleep(speed) ## Wait
p.stop() # stop the PWM output
print "Done" ## When loop is complete, print "Done"
GPIO.cleanup()
iterations = 4
speed = 2
Blink(int(iterations),float(speed))
This answer is just a conglomeration of an Arduino example and basic LED stuff.