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: