Skip to content

Instantly share code, notes, and snippets.

@harryjubb
Created July 9, 2021 22:41
Show Gist options
  • Save harryjubb/8374c4bf825720cd25dad7b57fedadf0 to your computer and use it in GitHub Desktop.
Save harryjubb/8374c4bf825720cd25dad7b57fedadf0 to your computer and use it in GitHub Desktop.
Raspberry Pi: Piezo Twinkle Twinkle Little Star
import RPi.GPIO as GPIO
from time import sleep
# Higher
notes = {
# 'A': 880,
'B': 988,
'C': 1047,
'D': 1175,
'E': 1318,
'F': 1397,
'G': 1568,
'A': 1760,
}
# Lower
notes = {
# 'A': 220,
'B': 247,
'C': 262,
'D': 294,
'E': 330,
'F': 349,
'G': 392,
'A': 440,
}
note_length = 2
crotchet = note_length * (1/4)
minim = note_length * (1/2)
tune = [
('C', crotchet),
('C', crotchet),
('G', crotchet),
('G', crotchet),
('A', crotchet),
('A', crotchet),
('G', minim),
('F', crotchet),
('F', crotchet),
('E', crotchet),
('E', crotchet),
('D', crotchet),
('D', crotchet),
('C', minim),
]
GPIO.setmode(GPIO.BOARD)
speaker = 12
GPIO.setup(speaker, GPIO.OUT)
p = GPIO.PWM(speaker, 1000)
try:
while 1:
for note, length in tune:
p.ChangeFrequency(notes[note])
p.start(50)
sleep(length)
p.stop()
sleep(0.1)
sleep(2)
except KeyboardInterrupt:
pass
p.stop()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment