Skip to content

Commit 5eaa7a1

Browse files
authored
Merge pull request #2615 from adafruit/piezo_driver
adding examples for piezo driver
2 parents e8a2fee + b88c0a9 commit 5eaa7a1

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Piezo_Driver_Examples/Arduino_Piezo_Driver_Example/.feather_rp2040.test.only

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#define PIEZO_PIN 5 // Pin connected to the piezo buzzer.
6+
7+
int toneFreq[] = {262, 294, 330, 349, 392, 440, 494, 523};
8+
int toneCount = 8;
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
Serial.println("Piezo Tone Example");
13+
}
14+
15+
void loop() {
16+
17+
for (int i=0; i < toneCount; i++) {
18+
Serial.print("Playing frequency: ");
19+
Serial.println(toneFreq[i]);
20+
tone(PIEZO_PIN, toneFreq[i]);
21+
delay(250); // Pause for half a second.
22+
noTone(PIEZO_PIN);
23+
delay(50);
24+
}
25+
Serial.println();
26+
delay(1000);
27+
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import pwmio
8+
9+
piezo = pwmio.PWMOut(board.D5, duty_cycle=0, frequency=440, variable_frequency=True)
10+
11+
while True:
12+
for f in (262, 294, 330, 349, 392, 440, 494, 523):
13+
piezo.frequency = f
14+
piezo.duty_cycle = 65535 // 2 # On 50%
15+
time.sleep(0.25) # On for 1/4 second
16+
piezo.duty_cycle = 0 # Off
17+
time.sleep(0.05) # Pause between notes
18+
time.sleep(0.5)

0 commit comments

Comments
 (0)