code.py
1 # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 """ 4 CircuitPython I2S Tone playback example. 5 Plays a tone for one second on, one 6 second off, in a loop. 7 8 Remove this line and all of the following docstring content before submitting to the Learn repo. 9 10 Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of 11 a proper I2S pin combination, run the pin combination script found here: 12 https://adafru.it/i2s-pin-combo-finder 13 14 Update the following pin names to a viable pin combination: 15 * BIT_CLOCK_PIN 16 * WORD_SELECT_PIN 17 * DATA_PIN 18 """ 19 import time 20 import array 21 import math 22 import audiocore 23 import board 24 import audiobusio 25 26 audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN) 27 28 tone_volume = 0.1 # Increase this to increase the volume of the tone. 29 frequency = 440 # Set this to the Hz of the tone you want to generate. 30 length = 8000 // frequency 31 sine_wave = array.array("h", [0] * length) 32 for i in range(length): 33 sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1)) 34 sine_wave_sample = audiocore.RawSample(sine_wave) 35 36 while True: 37 audio.play(sine_wave_sample, loop=True) 38 time.sleep(1) 39 audio.stop() 40 time.sleep(1)