To use with the Metro M4 Express, ItsyBitsy M4 Express or the Feather M4 Express, you must comment out the
piezo = pulseio.PWMOut(board.A2, duty_cycle=0, frequency=440, variable_frequency=True) line and uncomment
the piezo = pulseio.PWMOut(board.A1, duty_cycle=0, frequency=440, variable_frequency=True) line. A2 is not a
supported PWM pin on the M4 boards!
If you have simpleio library loaded into your /lib folder on your board, we have a nice little helper that makes a tone
for you on a piezo with a single command.
To use with any of the M0 boards, no changes to the following code are needed.
To use with the Metro M4 Express, ItsyBitsy M4 Express or the Feather M4 Express, you must comment out the
simpleio.tone(board.A2, f, 0.25) line and uncomment the simpleio.tone(board.A1, f, 0.25) line. A2 is not a
supported PWM pin on the M4 boards!
As you can see, it's much simpler!
Remember: To "comment out" a line, put a # and a space before it. To "uncomment" a line, remove the # +
space from the beginning of the line.
import time
import board
import pulseio
# For the M0 boards:
piezo = pulseio.PWMOut(board.A2, duty_cycle=0, frequency=440, variable_frequency=True)
# For the M4 boards:
# piezo = pulseio.PWMOut(board.A1, duty_cycle=0, frequency=440, variable_frequency=True)
while True:
for f in (262, 294, 330, 349, 392, 440, 494, 523):
piezo.frequency = f
piezo.duty_cycle = 65536 // 2 # On 50%
time.sleep(0.25) # On for 1/4 second
piezo.duty_cycle = 0 # Off
time.sleep(0.05) # Pause between notes
time.sleep(0.5)
import time
import board
import simpleio
while True:
for f in (262, 294, 330, 349, 392, 440, 494, 523):
# For the M0 boards:
simpleio.tone(board.A2, f, 0.25) # on for 1/4 second
# For the M4 boards:
# simpleio.tone(board.A1, f, 0.25) # on for 1/4 second
time.sleep(0.05) # pause between notes
time.sleep(0.5)