adding examples for piezo driver
Adding Arduino and CircuitPython example for the piezo driver guide
This commit is contained in:
parent
e8a2feeee6
commit
474cb9566a
2 changed files with 46 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#define PIEZO_PIN 5 // Pin connected to the piezo buzzer.
|
||||
|
||||
int toneFreq[] = {262, 294, 330, 349, 392, 440, 494, 523};
|
||||
int toneCount = 8;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Piezo Tone Example");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
for (int i=0; i < toneCount; i++) {
|
||||
Serial.print("Playing frequency: ");
|
||||
Serial.println(toneFreq[i]);
|
||||
tone(PIEZO_PIN, toneFreq[i]);
|
||||
delay(250); // Pause for half a second.
|
||||
noTone(PIEZO_PIN);
|
||||
delay(50);
|
||||
}
|
||||
Serial.println();
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import time
|
||||
import board
|
||||
import pwmio
|
||||
|
||||
piezo = pwmio.PWMOut(board.D5, 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 = 65535 // 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)
|
||||
Loading…
Reference in a new issue