code.py
1 # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import array 7 import math 8 import audiocore 9 import board 10 import audiobusio 11 12 tone_volume = 0.1 # Increase this to increase the volume of the tone. 13 frequency = 440 # Set this to the Hz of the tone you want to generate. 14 length = 8000 // frequency 15 sine_wave = array.array("h", [0] * length) 16 for i in range(length): 17 sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 -1)) 18 19 # For Feather M0 Express, ItsyBitsy M0 Express, Metro M0 Express 20 audio = audiobusio.I2SOut(board.D1, board.D0, board.D9) 21 # For Feather M4 Express 22 # audio = audiobusio.I2SOut(board.D1, board.D10, board.D11) 23 # For Metro M4 Express 24 # audio = audiobusio.I2SOut(board.D3, board.D9, board.D8) 25 sine_wave_sample = audiocore.RawSample(sine_wave) 26 27 while True: 28 audio.play(sine_wave_sample, loop=True) 29 time.sleep(1) 30 audio.stop() 31 time.sleep(1)