Required Components
About the Piezo Buzzer
When voltage is applied to the piezo buzzerβ
The element inside deforms
β
Sound (air vibration) is produced
The operation is repeated.
Electronic Circuit
Connect to the GROVE port labeled "Digital".Program
Program to make the buzzer sound
BUZZER = 16
pinMode(BUZZER, 0)
digitalWrite(BUZZER, 1)
A program that plays musical scales.Note: https://tomari.org/main/java/oto.html
BUZZER = 16
pwm = PWM.new()
pwm.pin(BUZZER)
# Specify the starting channel number
pwm.start(2)
while true
# Set the cycle
# C4:261Hz, Multiple 16(4)
# 1 Γ· 261 Γ 10000000 = 38314
# 38314 Γ· 16 = 2394(0x95A)
pwm.cycle(0x95A,4)
sleep 1
pwm.cycle(0x84D,4)
sleep 1
pwm.cycle(0x76A,4)
sleep 1
pwm.cycle(0x6EF,4)
sleep 1
pwm.cycle(0x639,4)
sleep 1
pwm.cycle(0x58C,4)
sleep 1
pwm.cycle(0x4F3,4)
sleep 1
end
Task:
- When a door is left open for a certain period, make the buzzer sound. When the door is closed, turn off the buzzer.
REED_SW = 18
BUZZER = 16
pinMode(BUZZER, 0)
pinMode(REED_SW, 1)
pinPull(REED_SW, 1)
cnt = 0
while true
status = digitalRead(REED_SW)
if(status == 0)
if(cnt > 5)
digitalWrite(BUZZER, 1)
end
cnt += 1
else
digitalWrite(BUZZER, 0)
cnt = 0
end
end