-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathcode.py
35 lines (28 loc) · 1.2 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# SPDX-FileCopyrightText: 2017 Limor Fried/ladyada for Adafruit Industries
# SPDX-FileCopyrightText: 2017 Phillip Burgess for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Adafruit Trinket/Gemma Example: Simple Theramin
Read the voltage from a Cadmium Sulfide (CdS) photocell voltage
divider and output a corresponding tone to a piezo buzzer
Photocell voltage divider center wire to GPIO #2 (analog 1)
and output tone to GPIO #0 (digital 0)
"""
import time
import analogio
import board
import pwmio
photocell_pin = board.A1 # CdS photocell connected to this ANALOG pin
speaker_pin = board.D0 # Speaker is connected to this DIGITAL pin
scale = 0.03 # Change this to adjust tone scale
# Initialize input/output pins
photocell = analogio.AnalogIn(photocell_pin)
pwm = pwmio.PWMOut(speaker_pin, variable_frequency=True, duty_cycle=0)
while True: # Loop forever...
# Read photocell analog pin and convert voltage to frequency
pwm.frequency = 220 + int(scale * float(photocell.value))
pwm.duty_cycle = 32767 # 50% duty cycle
time.sleep(0.4) # Play for 400 ms (adjust to your liking)
pwm.duty_cycle = 0 # Stop playing
time.sleep(0.05) # Delay 50 ms between notes (also adjustable)